欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Security 密碼驗證動態(tài)加鹽的驗證處理方法

 更新時間:2021年06月11日 09:52:24   作者:布吉_島  
小編最近在改造項目,需要將gateway整合security在一起進行認證和鑒權,今天小編給大家分享Spring Security 密碼驗證動態(tài)加鹽的驗證處理方法,感興趣的朋友一起看看吧

本文個人博客地址:https://www.leafage.top/posts/detail/21697I2R

最近幾天在改造項目,需要將gateway整合security在一起進行認證和鑒權,之前gateway和auth是兩個服務,auth是shiro寫的一個,一個filter和一個配置,內(nèi)容很簡單,生成token,驗證token,沒有其他的安全檢查,然后讓對項目進行重構。

先是要整合gateway和shiro,然而因為gateway是webflux,而shiro-spring是webmvc,所以沒搞成功,如果有做過并成功的,請告訴我如何進行整合,非常感謝。

那整合security呢,因為spring cloud gateway基于webflux,所以網(wǎng)上很多教程是用不了的,webflux的配置會有一些變化,具體看如下代碼示例:

import io.leafage.gateway.api.HypervisorApi;
import io.leafage.gateway.handler.ServerFailureHandler;
import io.leafage.gateway.handler.ServerSuccessHandler;
import io.leafage.gateway.service.JdbcReactiveUserDetailsService;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.core.userdetails.ReactiveUserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.server.SecurityWebFilterChain;
import org.springframework.security.web.server.authentication.HttpStatusServerEntryPoint;
import org.springframework.security.web.server.authentication.ServerAuthenticationFailureHandler;
import org.springframework.security.web.server.authentication.ServerAuthenticationSuccessHandler;
import org.springframework.security.web.server.authentication.logout.HttpStatusReturningServerLogoutSuccessHandler;
import org.springframework.security.web.server.csrf.CookieServerCsrfTokenRepository;

/**
 * spring security config .
 *
 * @author liwenqiang 2019/7/12 17:51
 */
@EnableWebFluxSecurity
public class ServerSecurityConfiguration {

    // 用于獲取遠程數(shù)據(jù)
    private final HypervisorApi hypervisorApi;

    public ServerSecurityConfiguration(HypervisorApi hypervisorApi) {
        this.hypervisorApi = hypervisorApi;
    }

    /**
     * 密碼配置,使用BCryptPasswordEncoder
     *
     * @return BCryptPasswordEncoder 加密方式
     */
    @Bean
    protected PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    /**
     * 用戶數(shù)據(jù)加載
     *
     * @return JdbcReactiveUserDetailsService 接口
     */
    @Bean
    public ReactiveUserDetailsService userDetailsService() {
        // 自定義的ReactiveUserDetails 實現(xiàn)
        return new JdbcReactiveUserDetailsService(hypervisorApi);
    }

    /**
     * 安全配置
     */
    @Bean
    SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
        http.formLogin(f -> f.authenticationSuccessHandler(authenticationSuccessHandler())
                .authenticationFailureHandler(authenticationFailureHandler()))
                .logout(l -> l.logoutSuccessHandler(new HttpStatusReturningServerLogoutSuccessHandler()))
                .csrf(c -> c.csrfTokenRepository(CookieServerCsrfTokenRepository.withHttpOnlyFalse()))
                .authorizeExchange(a -> a.pathMatchers(HttpMethod.OPTIONS).permitAll()
                        .anyExchange().authenticated())
                .exceptionHandling(e -> e.authenticationEntryPoint(new HttpStatusServerEntryPoint(HttpStatus.UNAUTHORIZED)));
        return http.build();
    }

    /**
     * 登陸成功后執(zhí)行的處理器
     */
    private ServerAuthenticationSuccessHandler authenticationSuccessHandler() {
        return new ServerSuccessHandler();
    }

    /**
     * 登陸失敗后執(zhí)行的處理器
     */
    private ServerAuthenticationFailureHandler authenticationFailureHandler() {
        return new ServerFailureHandler();
    }

}

上面的示例代碼,是我開源項目中的一段,一般的配置就如上面寫的,就可以使用了,但是由于我們之前的項目中的是shiro,然后有一個自定義的加密解密的邏輯。

首先說明一下情況,之前那一套加密(前端MD5,不加鹽,然后數(shù)據(jù)庫存儲的是加鹽后的數(shù)據(jù)和對應的鹽(每個賬號一個),要登錄比較之前對密碼要獲取動態(tài)的鹽,然后加鹽進行MD5,再進行對比,但是在配置的時候是沒法獲取某一用戶的鹽值)

所以上面的一版配置是沒法通過驗證的,必須在驗證之前,給請求的密碼混合該賬號對應的鹽進行二次加密后在對比,但是這里就有問題了:

  1. security 框架提供的幾個加密\解密工具沒有MD5的方式;
  2. security 配置加密\解密方式的時候,無法填入動態(tài)的賬號的加密鹽;

對于第一個問題還好處理,解決方式是:自定義加密\解密方式,然后注入到配置類中,示例如下:

import cn.hutool.crypto.SecureUtil;
import com.ichinae.imis.gateway.utils.SaltUtil;
import org.springframework.security.crypto.codec.Utf8;
import org.springframework.security.crypto.password.PasswordEncoder;
import java.security.MessageDigest;

/**
 * 自定義加密解密
 */
public class MD5PasswordEncoder implements PasswordEncoder {

    @Override
    public String encode(CharSequence charSequence) {
        String salt = SaltUtil.generateSalt();
        return SecureUtil.md5(SecureUtil.md5(charSequence.toString()) + salt);
    }

    @Override
    public boolean matches(CharSequence charSequence, String encodedPassword) {
        byte[] expectedBytes = bytesUtf8(charSequence.toString());
        byte[] actualBytes = bytesUtf8(charSequence.toString());
        return MessageDigest.isEqual(expectedBytes, actualBytes);
    }

    private static byte[] bytesUtf8(String s) {
        // need to check if Utf8.encode() runs in constant time (probably not).
        // This may leak length of string.
        return (s != null) ? Utf8.encode(s) : null;
    }

}

第二個問題的解決辦法,找了很多資料,也沒有找到,后來查看security的源碼發(fā)現(xiàn),可以在UserDetailsService接口的findByUsername()方法中,在返回UserDetails實現(xiàn)的時候,使用默認實現(xiàn)User的UserBuilder內(nèi)部類來解決這個問題,因為UserBuilder類中有一個屬性,passwordEncoder屬性,它是Fucntion<String, String>類型的,默認實現(xiàn)是 password -> password,即對密碼不做任何處理,先看下它的源碼:

1623227092.png

再看下解決問題之前的findByUsername()方法:

@Service
public class UserDetailsServiceImpl implements ReactiveUserDetailsService {

    @Resource
    private RemoteService remoteService;

    @Override
    public Mono<UserDetails> findByUsername(String username) {
        return remoteService.getUser(username).map(userBO -> User.builder()
                .username(username)
                .password(userBO.getPassword())
                .authorities(grantedAuthorities(userBO.getAuthorities()))
                .build());
    }

    private Set<GrantedAuthority> grantedAuthorities(Set<String> authorities) {
        return authorities.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toSet());
    }

}

那找到了問題的解決方法,就來改代碼了,如下所示:

新增一個代碼處理方法

private Function<String, String> passwordEncoder(String salt) {
    return rawPassword -> SecureUtil.md5(rawPassword + salt);
}

然后添加builder鏈

@Service
public class UserDetailsServiceImpl implements ReactiveUserDetailsService {

    @Resource
    private RemoteService remoteService;

    @Override
    public Mono<UserDetails> findByUsername(String username) {
        return remoteService.getUser(username).map(userBO -> User.builder()
                .passwordEncoder(passwordEncoder(userBO.getSalt())) //在這里設置動態(tài)的鹽
                .username(username)
                .password(userBO.getPassword())
                .authorities(grantedAuthorities(userBO.getAuthorities()))
                .build());
    }

    private Set<GrantedAuthority> grantedAuthorities(Set<String> authorities) {
        return authorities.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toSet());
    }

    private Function<String, String> passwordEncoder(String salt) {
        return rawPassword -> SecureUtil.md5(rawPassword + salt);
    }
}

然后跑一下代碼,請求登錄接口,就登陸成功了。

1623227505(1).png

以上就是Spring Security 密碼驗證動態(tài)加鹽的驗證處理的詳細內(nèi)容,更多關于Spring Security密碼驗證的資料請關注腳本之家其它相關文章!

相關文章

  • java開發(fā)https請求ssl不受信任問題解決方法

    java開發(fā)https請求ssl不受信任問題解決方法

    這篇文章主要介紹了java開發(fā)https請求ssl不受信任問題解決方法,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • Java中的static關鍵字你了解多少

    Java中的static關鍵字你了解多少

    這篇文章主要為大家詳細介紹了Java中的static關鍵字,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • Kotlin教程之基本數(shù)據(jù)類型

    Kotlin教程之基本數(shù)據(jù)類型

    這篇文章主要介紹了Kotlin教程之基本數(shù)據(jù)類型的學習的相關資料,需要的朋友可以參考下
    2017-05-05
  • SpringBoot實現(xiàn)redis延遲隊列的示例代碼

    SpringBoot實現(xiàn)redis延遲隊列的示例代碼

    延時隊列場景在我們?nèi)粘I(yè)務開發(fā)中經(jīng)常遇到,它是一種特殊類型的消息隊列,本文就來介紹一下SpringBoot實現(xiàn)redis延遲隊列的示例代碼,具有一定的參考價值,感興趣的可以了解一下
    2024-02-02
  • java如何實現(xiàn)多線程的順序執(zhí)行

    java如何實現(xiàn)多線程的順序執(zhí)行

    多線程是java的一種重要技術,但是多線程的運行是沒有絕對的順序的,那么java如何實現(xiàn)多線程的順序執(zhí)行,下面就一起來了解一下
    2021-05-05
  • Java中線程用法總結

    Java中線程用法總結

    這篇文章主要介紹了Java中線程用法,實例總結了java中線程的常見使用技巧,需要的朋友可以參考下
    2015-06-06
  • 使用FeignClient設置動態(tài)Url

    使用FeignClient設置動態(tài)Url

    這篇文章主要介紹了使用FeignClient設置動態(tài)Url方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • JAVA基于Arrays.sort()實現(xiàn)數(shù)組升序和降序

    JAVA基于Arrays.sort()實現(xiàn)數(shù)組升序和降序

    這篇文章主要介紹了JAVA基于Arrays.sort()實現(xiàn)數(shù)組升序和降序,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-06-06
  • mybatis如何批量添加一對多中間表

    mybatis如何批量添加一對多中間表

    這篇文章主要介紹了mybatis如何批量添加一對多中間表,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Java中大數(shù)據(jù)推薦算法使用場景分析

    Java中大數(shù)據(jù)推薦算法使用場景分析

    在Java中實現(xiàn)大數(shù)據(jù)推薦算法時,通常會使用一些開源的機器學習庫,如Apache Mahout、Weka、DL4J(DeepLearning4j,用于深度學習)或者Spark MLlib(用于在Spark集群上運行),這篇文章主要介紹了Java中可以用的大數(shù)據(jù)推薦算法,需要的朋友可以參考下
    2024-06-06

最新評論