SpringBoot Security密码加盐实例

 更新时间:2023年02月08日 10:02:41   作者:IT小马哥  
这篇文章主要为打击介绍了SpringBoot Security密码加盐实例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

Java技术迷

修改加密和验证方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 * 生成BCryptPasswordEncoder密码
 *
 * @param password 密码
 * @param salt 盐值
 * @return 加密字符串
 */
public static String encryptPassword(String password,String salt) {
    BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
     return passwordEncoder.encode(password + salt);
}
/**
 * 判断密码是否相同
 *
 * @param rawPassword     真实密码
 * @param encodedPassword 加密后字符
 * @param salt 盐值
 * @return 结果
 */
public static boolean matchesPassword(String rawPassword, String encodedPassword,String salt) {
    BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
    return passwordEncoder.matches(rawPassword + salt, encodedPassword);
}

自定义 DaoAuthenticationProvider

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
29
30
31
32
33
34
35
36
37
38
39
40
import com.maruifu.common.core.domain.model.LoginUser;
import com.maruifu.common.utils.DateUtils;
import com.maruifu.common.utils.SecurityUtils;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.Authentication;
/**
 * 身份验证提供者
 * @author maruifu
 */
public class JwtAuthenticationProvider extends DaoAuthenticationProvider {
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        // 可以在此处覆写整个登录认证逻辑
        return super.authenticate(authentication);
    }
    /**
     * 重写加盐后验证逻辑
     * @param userDetails
     * @param authentication
     * @throws AuthenticationException
     */
    @Override
    protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
        if (authentication.getCredentials() == null) {
            this.logger.debug("Failed to authenticate since no credentials provided");
            throw new BadCredentialsException(this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
        } else {
            String presentedPassword = authentication.getCredentials().toString();
            LoginUser loginUser =  (LoginUser)userDetails ;
            if (!SecurityUtils.matchesPassword(presentedPassword, userDetails.getPassword(), DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,loginUser.getUser().getCreateTime()))) {
                this.logger.debug("Failed to authenticate since password does not match stored value");
                throw new BadCredentialsException(this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
            }
        }
    }
}

注册到ProciderManager中

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
29
30
31
32
33
34
35
36
import com.maruifu.framework.security.handle.JwtAuthenticationProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
/**
 * spring security配置
 *
 * @author maruifu
 */
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfig1 extends WebSecurityConfigurerAdapter {
    /**
     * 自定义用户认证逻辑
     */
    @Autowired
    private UserDetailsService userDetailsService;
    /**
     * 解决 无法直接注入 AuthenticationManager
     * 重写 加盐后验证逻辑
     *
     * @return
     */
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean(){
        JwtAuthenticationProvider provider=new JwtAuthenticationProvider();
        provider.setUserDetailsService(userDetailsService);
        ProviderManager manager=new ProviderManager(provider);
        return manager;
    }
    ......省略configure方法
}

以上就是SpringBoot Security密码加盐实例的详细内容,更多关于SpringBoot Security密码加盐的资料请关注脚本之家其它相关文章!

蓄力AI

微信公众号搜索 “ 脚本之家 ” ,选择关注

程序猿的那些事、送书等活动等着你

原文链接:https://cloud.tencent.com/developer/article/2198817?areaSource=104001.5&traceId=GqEYuQLOTzxj-OnSA3Lf6

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 reterry123@163.com 进行投诉反馈,一经查实,立即处理!

相关文章

  • 收集的一些常用java正则表达式

    收集的一些常用java正则表达式

    收集的一些常用java正则表达式,需要的朋友可以参考一下
    2013-02-02
  • Spring通过Java配置集成Tomcat的方法

    Spring通过Java配置集成Tomcat的方法

    这篇文章主要介绍了Spring通过Java配置集成Tomcat的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-04-04
  • Java基础教程之HashMap迭代删除使用方法

    Java基础教程之HashMap迭代删除使用方法

    这篇文章主要给大家介绍了关于Java基础教程之HashMap迭代删除使用方法的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Java具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-05-05
  • java中Servlet Cookie取不到值原因解决办法

    java中Servlet Cookie取不到值原因解决办法

    这篇文章主要介绍了java中Servlet Cookie取不到值原因解决办法的相关资料,需要的朋友可以参考下
    2017-06-06
  • Java MongoDB数据库连接方法梳理

    Java MongoDB数据库连接方法梳理

    MongoDB作为一种介于关系型数据库和非关系型数据库之间的产品,它可以提供可扩展的高性能的数据存储解决方案,近些年来受到了开发者的喜爱
    2022-08-08
  • Java 内存模型中的happen-before关系详解

    Java 内存模型中的happen-before关系详解

    这篇文章主要为大家介绍了Java 内存模型中的happen-before关系示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • SpringBoot整合Shiro思路(最新超详细)

    SpringBoot整合Shiro思路(最新超详细)

    这篇文章主要介绍了SpringBoot整合Shiro思路(最新超详细),本文内容比较长,通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • spring boot实现自动输出word文档功能的实例代码

    spring boot实现自动输出word文档功能的实例代码

    这篇文章主要介绍了spring boot实现自动输出word文档功能的实例代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-04-04
  • 关于Spring Boot对jdbc的支持问题

    关于Spring Boot对jdbc的支持问题

    这篇文章主要介绍了关于Spring Boot对jdbc的支持问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-04-04
  • Spring Boot自定义监控指标的详细过程

    Spring Boot自定义监控指标的详细过程

    这篇文章主要介绍了Spring Boot如何自定义监控指标 ,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-03-03

最新评论