栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

前言
前端登录传值 加密过后的密码。后端需要先将密码解密 再进行密码验证。 
一、DaoAuthenticationProvider?
Spring Security 获取用户信息之后进行密码验证的方法(additionalAuthenticationChecks)在这个类中

二、解决方案
1.将additionalAuthenticationChecks方法进行重写,先对加密的密码进行 解密 然后再执行密码校验的逻辑。
2.修改security配置,添加自己的身份验证类。
//将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"));
            }
        }
    }

}
//修改security配置,添加自己的身份验证类
@Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(new DecodePwdAuthenticationProvider(userDetailsService));
        auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1028499.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号