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?使用斷言讓你的代碼在上線前就通過“體檢”(最新整理)
斷言是一種編程技巧,用于在代碼中插入檢查點,驗證程序的狀態(tài)是否符合預期,如果斷言失敗,程序會拋出一個錯誤,幫助你快速發(fā)現(xiàn)和修復bug,本文給大家介紹Spring?Boot?斷言:讓你的代碼在上線前就通過“體檢”,感興趣的朋友一起看看吧2025-03-03
java根據(jù)方法名稱取得反射方法的參數(shù)類型示例
利用java反射原理調用方法時,常先需要傳入方法參數(shù)數(shù)組才能取得方法。該方法參數(shù)數(shù)組采用動態(tài)取得的方式比較合適2014-02-02
基于Spring-cloud-gateway實現(xiàn)全局日志記錄的方法
最近項目在線上運行出現(xiàn)了一些難以復現(xiàn)的bug需要定位相應api的日志,通過nginx提供的api請求日志難以實現(xiàn),于是在gateway通過全局過濾器記錄api請求日志,本文給大家介紹基于Spring-cloud-gateway實現(xiàn)全局日志記錄,感興趣的朋友一起看看吧2023-11-11

