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密碼加鹽實例的詳細內(nèi)容,更多關于SpringBoot Security密碼加鹽的資料請關注腳本之家其它相關文章!
相關文章
Java MongoDB數(shù)據(jù)庫連接方法梳理
MongoDB作為一種介于關系型數(shù)據(jù)庫和非關系型數(shù)據(jù)庫之間的產(chǎn)品,它可以提供可擴展的高性能的數(shù)據(jù)存儲解決方案,近些年來受到了開發(fā)者的喜愛2022-08-08Java 內(nèi)存模型中的happen-before關系詳解
這篇文章主要為大家介紹了Java 內(nèi)存模型中的happen-before關系示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10spring boot實現(xiàn)自動輸出word文檔功能的實例代碼
這篇文章主要介紹了spring boot實現(xiàn)自動輸出word文檔功能的實例代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04