最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot Security密碼加鹽實例

 更新時間:2023年02月08日 10:02:41   作者:IT小馬哥  
這篇文章主要為打擊介紹了SpringBoot Security密碼加鹽實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

修改加密和驗證方法

    /**
     * 生成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 結(jié)果
     */
    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 {
        // 可以在此處覆寫整個登錄認(rèn)證邏輯
        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 {
    /**
     * 自定義用戶認(rèn)證邏輯
     */
    @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密碼加鹽實例的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot Security密碼加鹽的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Spring?Boot?使用斷言讓你的代碼在上線前就通過“體檢”(最新整理)

    Spring?Boot?使用斷言讓你的代碼在上線前就通過“體檢”(最新整理)

    斷言是一種編程技巧,用于在代碼中插入檢查點(diǎn),驗證程序的狀態(tài)是否符合預(yù)期,如果斷言失敗,程序會拋出一個錯誤,幫助你快速發(fā)現(xiàn)和修復(fù)bug,本文給大家介紹Spring?Boot?斷言:讓你的代碼在上線前就通過“體檢”,感興趣的朋友一起看看吧
    2025-03-03
  • 實例解析Java關(guān)于static的作用

    實例解析Java關(guān)于static的作用

    只要是有學(xué)過Java的都一定知道static,也一定能多多少少說出一些作用和注意事項。文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 用Java實現(xiàn)全國天氣預(yù)報的api接口調(diào)用示例

    用Java實現(xiàn)全國天氣預(yù)報的api接口調(diào)用示例

    查詢天氣預(yù)報在APP中常用的一個常用功能,本文實例講述了java調(diào)用中國天氣網(wǎng)api獲得天氣預(yù)報信息的方法。分享給大家供大家參考。
    2016-10-10
  • spring消息轉(zhuǎn)換器使用詳解

    spring消息轉(zhuǎn)換器使用詳解

    這篇文章主要為大家詳細(xì)介紹了spring消息轉(zhuǎn)換器的使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Java8中的 Lambda表達(dá)式教程

    Java8中的 Lambda表達(dá)式教程

    這篇文章主要介紹了 Java8中的 Lambda表達(dá)式教程,需要的朋友可以參考下
    2017-02-02
  • Java中2個對象字段值比較是否相同

    Java中2個對象字段值比較是否相同

    本文主要介紹了Java中2個對象字段值比較是否相同,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • SpringBoot 自動配置原理及源碼解析

    SpringBoot 自動配置原理及源碼解析

    SpringBoot 在項目啟動的時候封裝了創(chuàng)建對象的方法,無需我們手動配置,接下來通過本文給大家介紹SpringBoot 自動配置原理解析及源碼展示,感興趣的朋友一起看看吧
    2021-06-06
  • java根據(jù)方法名稱取得反射方法的參數(shù)類型示例

    java根據(jù)方法名稱取得反射方法的參數(shù)類型示例

    利用java反射原理調(diào)用方法時,常先需要傳入方法參數(shù)數(shù)組才能取得方法。該方法參數(shù)數(shù)組采用動態(tài)取得的方式比較合適
    2014-02-02
  • 基于Spring-cloud-gateway實現(xiàn)全局日志記錄的方法

    基于Spring-cloud-gateway實現(xiàn)全局日志記錄的方法

    最近項目在線上運(yùn)行出現(xiàn)了一些難以復(fù)現(xiàn)的bug需要定位相應(yīng)api的日志,通過nginx提供的api請求日志難以實現(xiàn),于是在gateway通過全局過濾器記錄api請求日志,本文給大家介紹基于Spring-cloud-gateway實現(xiàn)全局日志記錄,感興趣的朋友一起看看吧
    2023-11-11
  • JavaWeb開發(fā)入門第一篇必備知識講解

    JavaWeb開發(fā)入門第一篇必備知識講解

    JavaWeb開發(fā)入門第一篇主要內(nèi)容介紹的是必備知識、基礎(chǔ)知識、搭建JavaWeb應(yīng)用開發(fā)環(huán)境,感興趣的小伙伴們可以參考一下
    2016-04-04

最新評論

德化县| 巫山县| 红安县| 昭苏县| 道真| 革吉县| 绵竹市| 武宁县| 孝昌县| 萨迦县| 新干县| 汕尾市| 行唐县| 桓台县| 成都市| 化州市| 礼泉县| 静海县| 普陀区| 巧家县| 德州市| 盐池县| 织金县| 奎屯市| 徐汇区| 滨海县| 芜湖县| 通化市| 大庆市| 施秉县| 辽宁省| 临漳县| 加查县| 射阳县| 康定县| 论坛| 定陶县| 富蕴县| 天全县| 南皮县| 招远市|