SpringBoot Security密码加盐实例
更新时间:2023年02月08日 10:02:41 作者:IT小马哥
这篇文章主要为打击介绍了SpringBoot Security密码加盐实例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
修改加密和验证方法
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密码加盐的资料请关注脚本之家其它相关文章!

微信公众号搜索 “ 脚本之家 ” ,选择关注
程序猿的那些事、送书等活动等着你
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 reterry123@163.com 进行投诉反馈,一经查实,立即处理!
相关文章
spring boot实现自动输出word文档功能的实例代码
这篇文章主要介绍了spring boot实现自动输出word文档功能的实例代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2021-04-04
最新评论