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

解決Spring security5.5.7報(bào)錯(cuò)Encoded password does not look like BCrypt異常

 更新時(shí)間:2024年08月14日 09:35:40   作者:kevingavinhu  
這篇文章主要介紹了解決Spring security5.5.7出現(xiàn)Encoded password does not look like BCrypt異常問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

背景        

一個(gè)老項(xiàng)目,由于2022年爆發(fā)了spring bean和spring core的漏洞,將springboot從1.5.4升級(jí)到2.5.14版本,修復(fù)了spring的漏洞。

同時(shí)spring security也需要同步升級(jí),升級(jí)過程中出現(xiàn)了一系列錯(cuò)誤。

故做一個(gè)記錄在此。

問題:

登錄權(quán)限系統(tǒng)時(shí),出現(xiàn)Encoded password does not look like BCrypt異常錯(cuò)誤;同時(shí)報(bào)出clientSecret不匹配的問題。

解決方案

統(tǒng)一采用PasswordEncoderFactories.createDelegatingPasswordEncoder()去獲取到密碼加密器PasswordEncoder

(1)新版本的Spring Security對(duì)客戶端的密鑰進(jìn)行了加密處理,配置中需要使用PasswordEncoderFactories.createDelegatingPasswordEncoder().encode進(jìn)行加密;

(2)登錄成功后的handler,則需要采用PasswordEncoderFactories.createDelegatingPasswordEncoder().matches(clientSecret,clientDetails.getClientSecret())判斷密鑰是否匹配,而不是采用原有舊版的未加密的密鑰進(jìn)行equal進(jìn)行比較字符串。

BCryptPasswordEncoder介紹

Spring Security 中提供了 BCryptPasswordEncoder用于用戶密碼的加密和驗(yàn)證,這里講解一下該 PasswordEncoder 的實(shí)現(xiàn)邏輯.

首先 BCryptPasswordEncoder 使用了 BCrypt 算法來對(duì)密碼實(shí)現(xiàn)加密和驗(yàn)證。由于 BCrypt本身是一種 單向Hash算法,因此它和我們?nèi)粘S玫?MD5一樣,通常情況下是無法逆向解密的。

在 BSD系統(tǒng)中 BCrypt 算法主要用來替代 md5 加密算法,它使用了一種可變版本的Blowfish流密碼算法。通過多次加鹽和隨機(jī)數(shù),因此這套加密算法被廣泛用于許多系統(tǒng)的密碼加密當(dāng)中。

然而每次加密的結(jié)果是不一樣的,如果采用兩次加密的結(jié)果進(jìn)行equal比較,那是得不到真實(shí)的true結(jié)果的。

具體代碼改動(dòng)

  • 注冊(cè)一個(gè)bean,覆蓋原有的PasswordEncoder
	/**
	 * 加密方式,spring security5.5.7升級(jí)后,默認(rèn)采用BCryptPasswordEncoder
	 * @return
	 */
	@Bean
	public PasswordEncoder passwordEncoder() {
		return PasswordEncoderFactories.createDelegatingPasswordEncoder();
	}
  • 項(xiàng)目啟動(dòng)時(shí),加載的認(rèn)證服務(wù)器配置的修改
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
    /**
     * 引入BCrypt強(qiáng)哈希加密和解密工具
     */
    @Autowired
    private PasswordEncoder passwordEncoder;
	
	/*
     * 客戶端配置改動(dòng)
     *
     * @param clients
     * @throws Exception
     */
    
     @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
 
        InMemoryClientDetailsServiceBuilder builder = clients.inMemory();
        //spirng boot 1.5.* 升級(jí)到spring boot 2.0以上,當(dāng)再次訪問授權(quán)服務(wù)器時(shí)出現(xiàn)Encoded password does not look like BCrypt異常,需要passwordEncoder.encode
        if (ArrayUtils.isNotEmpty(securityProperties.getOauth2().getClients())) {
            for (OAuth2ClientProperties config : securityProperties.getOauth2().getClients()) {
                //設(shè)置clientid
                builder.withClient(config.getClientId())
                        // 設(shè)置clientsecret,需要加密配置
                        .secret(passwordEncoder.encode(config.getClientSecret()))
                        // 設(shè)置令牌過期時(shí)間,單位秒,默認(rèn)7200,定義在OAuth2ClientProperties
                        .accessTokenValiditySeconds(config.getAccessTokenValidateSeconds())
                        // 允許的授權(quán)模式
                        .authorizedGrantTypes("refresh_token", "authorization_code", "password")
                        // 設(shè)置刷新令牌的過期時(shí)間,單位秒,這里設(shè)置為60天
                        .refreshTokenValiditySeconds(5184000)
                        // 配置oauth能獲取的權(quán)限,是一個(gè)數(shù)組
                        .scopes("all", "write", "read");
            }
        }
}
  • 構(gòu)造用戶登錄信息
@Component
public class MyUserDetailsService implements UserDetailsService {
    /**
	 * 注入加密器
	 */
    @Autowired
	private PasswordEncoder passwordEncoder;
	/**
	 * 搜索用戶信息,構(gòu)造登錄用戶
	 * @param username
	 * @return
	 * @throws UsernameNotFoundException
	 */
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		//其他數(shù)據(jù)庫查詢等邏輯省略........
		logger.info("表單登錄用戶名:" + username);
		
//進(jìn)行權(quán)限系統(tǒng)登錄,密碼前面需要加上加密的方式,調(diào)用createDelegatingPasswordEncoder后默認(rèn)會(huì)加上{bcrypt}在密碼前面
		String password = passwordEncoder.encode("123456");
		user.setLastLoginTime(nowTime);
		sysPublicUserRepository.save(user);
		logger.info("保存登錄時(shí)間:" + nowTime);
		User user1 = new User(userId, password,
				true, accountNonExpired, true, true,
				AuthorityUtils.commaSeparatedStringToAuthorityList(user.getRoles()));
 
		return user1;
	}
}
  • 登錄成功后SuccessHandler的校驗(yàn)
@Component("authenticationSuccessHandler")
public class authenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
 
	/**
	 * 注入密碼器
	 */
	@Autowired
	private PasswordEncoder passwordEncoder;
	/**
	 * 此方法是用于在request中取得ClientDetails和新建tokenRequest,并用這兩個(gè)參數(shù)來生成OAuth2AccessToken
	 */
	@Override
	public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
			Authentication authentication) throws IOException, ServletException {
		//..............省略代碼
		//..............
		
		//客戶端的密鑰,從header中取出
		String clientSecret = extractAndDecodeHeader(header, request);
		
		ClientDetails clientDetails = clientDetailsService.loadClientByClientId(clientId);
		PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
		if (clientDetails == null) {
			throw new UnapprovedClientAuthenticationException("clientId對(duì)應(yīng)的配置信息不存在:" + clientId);
		} else if (!passwordEncoder.matches(clientSecret,clientDetails.getClientSecret())) { //關(guān)鍵是這里不能用equal匹配了
			throw new UnapprovedClientAuthenticationException("clientSecret不匹配:" + clientId);
		}
		//............
		//后續(xù)token的其他處理
		//............
	}
}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Mybatis 如何批量刪除數(shù)據(jù)的實(shí)現(xiàn)示例

    Mybatis 如何批量刪除數(shù)據(jù)的實(shí)現(xiàn)示例

    這篇文章主要介紹了Mybatis 如何批量刪除數(shù)據(jù)的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Java 基礎(chǔ)之修飾符關(guān)鍵詞整理

    Java 基礎(chǔ)之修飾符關(guān)鍵詞整理

    這篇文章主要介紹了Java 基礎(chǔ)之修飾符關(guān)鍵詞整理的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • 關(guān)于MyBatis中映射對(duì)象關(guān)系的舉例

    關(guān)于MyBatis中映射對(duì)象關(guān)系的舉例

    這篇文章主要介紹了關(guān)于MyBatis中映射對(duì)象關(guān)系的舉例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 淺談如何優(yōu)雅地停止Spring Boot應(yīng)用

    淺談如何優(yōu)雅地停止Spring Boot應(yīng)用

    這篇文章主要介紹了淺談如何優(yōu)雅地停止Spring Boot應(yīng)用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • MyBatis基礎(chǔ)支持DataSource實(shí)現(xiàn)源碼解析

    MyBatis基礎(chǔ)支持DataSource實(shí)現(xiàn)源碼解析

    這篇文章主要為大家介紹了MyBatis基礎(chǔ)支持DataSource實(shí)現(xiàn)源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • Spring使用Setter完成依賴注入方式

    Spring使用Setter完成依賴注入方式

    這篇文章主要介紹了Spring使用Setter完成依賴注入方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • spring-session自定義序列化方式

    spring-session自定義序列化方式

    這篇文章主要介紹了spring-session自定義序列化方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 揭秘springboot中Redisson?可重入鎖的實(shí)現(xiàn)原理

    揭秘springboot中Redisson?可重入鎖的實(shí)現(xiàn)原理

    本文探究基于?Redisson?的可重入鎖原理,通過使用?hash?數(shù)據(jù)結(jié)構(gòu)?+?Lua?腳本實(shí)現(xiàn)可重入的分布式鎖,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2026-02-02
  • Java詳解AVL樹的應(yīng)用

    Java詳解AVL樹的應(yīng)用

    AVL樹是高度平衡的二叉樹,它的特點(diǎn)是AVL樹中任何節(jié)點(diǎn)的兩個(gè)子樹的高度最大差別為1,本文主要給大家介紹了Java如何實(shí)現(xiàn)AVL樹,需要的朋友可以參考下
    2022-07-07
  • 在Spring Boot中集成RabbitMQ的實(shí)戰(zhàn)記錄

    在Spring Boot中集成RabbitMQ的實(shí)戰(zhàn)記錄

    本文介紹SpringBoot集成RabbitMQ的步驟,涵蓋配置連接、消息發(fā)送與接收,并對(duì)比兩種定義Exchange與隊(duì)列的方式:手動(dòng)聲明(適合復(fù)雜路由)和注解綁定(適合快速開發(fā)),感興趣的朋友跟隨小編一起看看吧
    2025-06-06

最新評(píng)論

高雄市| 博白县| 都江堰市| 南阳市| 海晏县| 海城市| 饶阳县| 东城区| 雅江县| 莎车县| 普格县| 德保县| 章丘市| 广汉市| 资讯 | 营山县| 九龙县| 平塘县| 余庆县| 股票| 贵阳市| 万荣县| 宁蒗| 衡阳市| 高雄县| 九江县| 永福县| 东乌珠穆沁旗| 察哈| 修水县| 垣曲县| 闽侯县| 关岭| 德州市| 榕江县| 青浦区| 宿州市| 泽库县| 赤峰市| 临安市| 永平县|