java

springsecrity入门

rzk · 12月14日 · 2021年本文共2822个字 · 预计阅读10分钟144次已读

UserDetailsService

需要传入用户名,不然会报用户名为空的异常
UsernameNotFoundException用户名没有发现异常,在loadUserByUsername中是需要通过自己的逻辑从数据库中取值,如果通过用户名没有查询到对应的数据,应该抛出UsernameNotFoundException,系统就知道用户名没有查询到

springsecrity入门

回参是一个UserDetails

public interface UserDetails extends Serializable {
    // ~ Methods
    // ========================================================================================================

    /**
     * Returns the authorities granted to the user. Cannot return <code>null</code>.
     *
     * @return the authorities, sorted by natural key (never <code>null</code>)
     */
    Collection<? extends GrantedAuthority> getAuthorities();//权限

    /**
     * Returns the password used to authenticate the user.
     *
     * @return the password
     */
    String getPassword();//密码

    /**
     * Returns the username used to authenticate the user. Cannot return <code>null</code>.
     *
     * @return the username (never <code>null</code>)
     */
    String getUsername();//用户名

    /**
     * Indicates whether the user's account has expired. An expired account cannot be
     * authenticated.
     *
     * @return <code>true</code> if the user's account is valid (ie non-expired),
     * <code>false</code> if no longer valid (ie expired)
     */
    boolean isAccountNonExpired();//用户帐户是否已过期

    /**
     * Indicates whether the user is locked or unlocked. A locked user cannot be
     * authenticated.
     *
     * @return <code>true</code> if the user is not lock睿共享ed, <code>false</code> otherwise
     */
    boole睿共享an isAccountNonLocked();//用户是否被锁定或解锁

    /**
     * Indicates whether the user's credentials (password) has expired. Expired
     * credentials prevent authentication.
     *
     * @return <code>true</code> if the user's credentials are valid (ie non-expired),
     * <code>false</code> if no longer valid (ie expired)
     */
    boolean isCredentialsNonExpired();//用户的凭据(密码)是否已过期

    /**
     * Indicates whether the user is enabled or disabled. A disabled user cannot be
     * authenticated.
     *
     * @return <code>tr睿共享ue</code> if the user is enabled, <code>false</code> otherwise
     */
    boolean isEnabled();是否启动
}

UserDetails 的实现类是一个user

具体参数这是springsecrity实现的一个类

    private String password;
    private final String username;
    private final Set<GrantedAuthority> authorities;
    private final boolean accountNonEx睿共享pired;
    private final boolean accountNonLocked;
    private final boolean credentialsNonExpired;
    private final boolean enabled;

PasswordEncoder密码解析器

功能就是加密密码和解析密码

Spring Security要求容器中必须有PasswordEncoder实例.所以当自定义登录逻辑时要求必须给容器注入
PaswordEncoder 的bean又悌。

接口介绍

• encode():把参数按照特定的解析规则迸行解析。
• matches():验证从存储中荻取的编码空码与编码后提交的原始密码是否匹配。如果击码匹配,则返回
true;如果不匹配,则返回false。第一个参数表示需要被解析的密码。第二个参数表示存储的空码。
• upgradeEncodingO :如果解析的空码能够再次进行解析且达到更安全的结果则返回true,否则返回false.

PasswordEncoder
encode(CharSequence): String

matches(CharSequence, String): boolean
验证从存储器中获得的经过编码的密码是否与提交的原始密码匹配。
如果密码匹配则返回true,如果不匹配则返回false。 存储的密码本身永远不会被解码。
默认返回false。

upgradeEncoding(String): boolean

密码示例

        BCryptPasswordEncoder encode =睿共享 new BCryptPasswordEncoder();
        //登录密码
        String loginpw = "123";
        System.out.println(loginpw);
        //数据库密码
        String userpw = encode.encode("123");
        System.out.println(userpw);
        //第一个参数是原密码
        //第二个参数就是用 PasswordEncoder 调用 encode(CharSequence rawPassword) 编码过后保存在数据库的密码。
        boolean matches = encode.matches(loginpw, userpw);
        System.out.println(matches);

可以看到userpw每次生成都可以和loginpw相匹配

springsecrity入门

springsecrity入门

0 条回应