SpringBoot Security密码加盐实例

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

修改加密和验证方法

    /**
     * 生成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

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中

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密码加盐的资料请关注脚本之家其它相关文章!

相关文章

  • Spring Boot 使用断言让你的代码在上线前就通过“体检”(最新整理)

    Spring Boot 使用断言让你的代码在上线前就通过“体检”(最新整理)

    断言是一种编程技巧,用于在代码中插入检查点,验证程序的状态是否符合预期,如果断言失败,程序会抛出一个错误,帮助你快速发现和修复bug,本文给大家介绍Spring Boot 断言:让你的代码在上线前就通过“体检”,感兴趣的朋友一起看看吧
    2025-03-03
  • 实例解析Java关于static的作用

    实例解析Java关于static的作用

    只要是有学过Java的都一定知道static,也一定能多多少少说出一些作用和注意事项。文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-04-04
  • 用Java实现全国天气预报的api接口调用示例

    用Java实现全国天气预报的api接口调用示例

    查询天气预报在APP中常用的一个常用功能,本文实例讲述了java调用中国天气网api获得天气预报信息的方法。分享给大家供大家参考。
    2016-10-10
  • spring消息转换器使用详解

    spring消息转换器使用详解

    这篇文章主要为大家详细介绍了spring消息转换器的使用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • Java8中的 Lambda表达式教程

    Java8中的 Lambda表达式教程

    这篇文章主要介绍了 Java8中的 Lambda表达式教程,需要的朋友可以参考下
    2017-02-02
  • Java中2个对象字段值比较是否相同

    Java中2个对象字段值比较是否相同

    本文主要介绍了Java中2个对象字段值比较是否相同,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-04-04
  • SpringBoot 自动配置原理及源码解析

    SpringBoot 自动配置原理及源码解析

    SpringBoot 在项目启动的时候封装了创建对象的方法,无需我们手动配置,接下来通过本文给大家介绍SpringBoot 自动配置原理解析及源码展示,感兴趣的朋友一起看看吧
    2021-06-06
  • java根据方法名称取得反射方法的参数类型示例

    java根据方法名称取得反射方法的参数类型示例

    利用java反射原理调用方法时,常先需要传入方法参数数组才能取得方法。该方法参数数组采用动态取得的方式比较合适
    2014-02-02
  • 基于Spring-cloud-gateway实现全局日志记录的方法

    基于Spring-cloud-gateway实现全局日志记录的方法

    最近项目在线上运行出现了一些难以复现的bug需要定位相应api的日志,通过nginx提供的api请求日志难以实现,于是在gateway通过全局过滤器记录api请求日志,本文给大家介绍基于Spring-cloud-gateway实现全局日志记录,感兴趣的朋友一起看看吧
    2023-11-11
  • JavaWeb开发入门第一篇必备知识讲解

    JavaWeb开发入门第一篇必备知识讲解

    JavaWeb开发入门第一篇主要内容介绍的是必备知识、基础知识、搭建JavaWeb应用开发环境,感兴趣的小伙伴们可以参考一下
    2016-04-04

最新评论