当前位置:   article > 正文

Spring Security重写验证密码-前端传入加密过后的密码_additionalauthenticationchecks

additionalauthenticationchecks

前言

前端登录传值 加密过后的密码。后端需要先将密码解密 再进行密码验证。 
  • 1

一、DaoAuthenticationProvider?

Spring Security 获取用户信息之后进行密码验证的方法(additionalAuthenticationChecks)在这个类中
  • 1

在这里插入图片描述

二、解决方案

1.将additionalAuthenticationChecks方法进行重写,先对加密的密码进行 解密 然后再执行密码校验的逻辑。
2.修改security配置,添加自己的身份验证类。
  • 1
  • 2
//将additionalAuthenticationChecks方法进行重写,先对加密的密码进行 解密 然后再执行密码校验的逻辑
@Slf4j
public class DecodePwdAuthenticationProvider extends DaoAuthenticationProvider {

    public DecodePwdAuthenticationProvider(UserDetailsServiceImpl userDetailsService){
        setUserDetailsService(userDetailsService);
    }


    @Override
    protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
        if (authentication.getCredentials() == null) {
            this.logger.debug("Authentication failed: no credentials provided");
            throw new BadCredentialsException(this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
        } else {
            String presentedPassword = authentication.getCredentials().toString();

            //Base64解密
            presentedPassword = new String(Base64.getDecoder().decode(presentedPassword));
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            if (!passwordEncoder.matches(presentedPassword, userDetails.getPassword())) {
                this.logger.debug("Authentication failed: password does not match stored value");
                throw new BadCredentialsException(this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
            }
        }
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

//修改security配置,添加自己的身份验证类
@Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(new DecodePwdAuthenticationProvider(userDetailsService));
        auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/370503
推荐阅读
相关标签
  

闽ICP备14008679号