SpringSecurityカスタムユーザーパスワード検証

ユーザーのパスワードをフォアグラウンドで入力した後、暗号化してユーザー名の関連付けと比較する必要があるため、AuthenticationProviderの実装クラスは処理のために書き直されます。

@Component
 パブリック クラス MyAuthenticationProvider AuthenticationProviderを

    実装します{ 
    @Autowired private ISysUserService iSysUserService; 
    @Autowired 
    プライベートPasswordEncorder passwordEncorder; 

    @Override 
    public Authentication authenticate(Authentication authentication)throws AuthenticationException { 
        String username = authentication.getName(); 
        String presentPassword = (String)authentication.getCredentials(); 
        UserDetails userDeatils = null ;
//ユーザー名に基づいてユーザー情報を取得します SysUser sysUser
= this .iSysUserService.getUserByName(username); if (StringUtils.isEmpty(sysUser)){ throw new BadCredentialsException( "Username does not exist" ); } else { userDeatils = new User( username、sysUser.getPassword()、AuthorityUtils.commaSeparatedStringToAuthorityList( "USER" ));
//データベースに保存されているカスタム暗号化ルール、ユーザー名、入力パスワード、ソルト値は暗号化されます String encodingPassword
= PasswordUtil.encrypt(username、presentPassword 、sysUser.getSalt()); if(authentication.getCredentials()== null ){ throw new BadCredentialsException( "Login name or password error" ); } else if(!this .passwordEncorder.matches(encodedPassword、userDeatils.getPassword())){ throw new BadCredentialsException( "不正なログインまたはパスワード " ); } else { UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(userDeatils、authentication.getCredentials()、userDeatils.getAuthorities()); result.setDetails(authentication.getDetails()); 結果を 返す; } } } @Override public boolean supports(Class <?> authentication){ return true ; } }
次に、SecurityConfigurationで有効にします
@Override 
protected void configure(AuthenticationManagerBuilder auth)は例外をスローします{
auth.authenticationProvider(this.myAuthenticationProvider);
}

おすすめ

転載: www.cnblogs.com/brucebai/p/12680494.html