SpringBoot Security密码加盐实例
作者:IT小马哥 发布时间:2023-06-08 17:06:48
标签: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方法
}
来源:https://cloud.tencent.com/developer/article/2198817?areaSource=104001.5&traceId=GqEYuQLOTzxj-OnSA3Lf6


猜你喜欢
- C# SynchronizationContext及Send和Post使用1、(SynchronizationContext)同步上下文的作
- 前言对于多线程,大家应该很熟悉。但是,大家了解线程池吗?今天,我将带大家全部学习关于线程池的所有知识。目录1. 简介2. 工作原理2.1 核
- C#中List<T>中泛型T如果是一个对象的话,则利用Find函数返回的将是这个对象的指针,对其返回对象的属性进行操作,也会影响
- 这篇文章主要介绍了如何使用SpEL表达式实现动态分表查询,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的
- 现在许多流行的软件中都有欢迎界面,今天就介绍一下欢迎界面的制作,由于界面涉及到页面的滑动,因此要采用ViewPager,sdk在4.0一下的
- 同线程回收对象上一小节剖析了从recycler中获取一个对象, 这一小节分析在创建和回收是同线程的前提下, recycler是如何进行回收的
- 方法1:C#Label1.Text = Request.Form["txtName"].ToString();vb.ne
- 编辑 项目目录/.idea/workspace.xml添加标签后,保存。重启idea即可。<component name="
- 背景事情是酱紫的,阿星的上级leader负责记录信息的业务,每日预估数据量是15万左右,所以引入sharding-jdbc做分表。上级lea
- 介绍Java桥梁模式(也称桥接模式)(Bridge Pattern)是一种设计模式,它将抽象和实现分离,使它们可以独立地变化.它通过一个大类
- 回文数判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。示例 1:输入: 121输出: true示例&
- Compose中我们应该怎么使用地图呢?像之前我们在xml里面创建MapView,都是在Activity里面,管理MapView生命周期,和
- 目录1.项目gitthub地址链接: https://github.com/baisul/generateCode.git切换到master
- 本文实例分析了Java接口默认方法带来的问题。分享给大家供大家参考,具体如下:一 点睛Java 8中,如果一个类实现两个或多个接口,即“变相
- 文章描述在前面两篇写完了对于GIF动态图片的分割和合成,这一篇来写下将视频文件分割成一帧帧图片的方法。开发环境.NET Framework版
- 在activity级下使用this表示contextkotlin中取消了xxxActivity.this的用法,所以我们可以在activit
- 在C#的继承中尝尝会用到相关的修饰词:override和new。这两个修饰符都可以在新的子类中,重写同名的父类方法。
- 方式一:if语句控制// 例如:Column( mainAxisAlig
- 工作需要,经常需要实现api接口,但每次都是大同小异,我就考虑是否可以将这种重复性的工作配置化。我就写一个模板api,然后所有的HTTP请求
- 像javascript中有eval()来执行动态代码,c#中是没有的,于是自己动手丰衣足食,先来代码using System;using S