Spring Security即將棄用WebSecurityConfigurerAdapter配置類

用過WebSecurityConfigurerAdapter的都知道對Spring Security十分重要,總管Spring Security的配置體系。但是馬上這個(gè)類要廢了,你沒有看錯(cuò),這個(gè)類將在5.7版本被@Deprecated所標(biāo)記了,未來這個(gè)類將被移除。

相關(guān)的issues已經(jīng)被處理并關(guān)閉

對此對此網(wǎng)友大呼“學(xué)著學(xué)著就被棄用了”。既然馬上要棄用了,總要有個(gè)過渡方案或者新玩法吧。

早在2021年3月份胖哥就寫了一篇文章,把新玩法給明明白白說清楚了,如果你看了的話,肯定不會(huì)學(xué)廢棄技術(shù)。這里把整套的替代方案再搞一遍,可別再學(xué)過時(shí)技術(shù)了。

版本需要Spring Security 5.4.x及以上。

HttpSecurity新舊玩法對比

舊玩法:

@Configuration
static class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .antMatcher("/**")
            .authorizeRequests(authorize -> authorize
                    .anyRequest().authenticated()
            );
    }
}

新玩法:

@Bean
SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    return http
            .antMatcher("/**")
            .authorizeRequests(authorize -> authorize
                    .anyRequest().authenticated()
            )
            .build();
}

原理去看這一篇文章。

WebSecurity新舊玩法對比

使用WebSecurity.ignoring()忽略某些URL請求,這些請求將被Spring Security忽略,這意味著這些URL將有受到 CSRF、XSS、Clickjacking 等攻擊的可能。以下示例僅僅作為演示,請勿使用在生產(chǎn)環(huán)境。是不是又學(xué)到了呢?

舊玩法:

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(WebSecurity web) {
        // 僅僅作為演示
        web.ignoring().antMatchers("/ignore1", "/ignore2");
    }

}

新玩法:

@Configuration
public class SecurityConfiguration {

    @Bean
    public WebSecurityCustomizer webSecurityCustomizer() {
        // 僅僅作為演示
        return (web) -> web.ignoring().antMatchers("/ignore1", "/ignore2");
    }

}

如果你需要忽略URL,請考慮通過HttpSecurity.authorizeHttpRequestspermitAll來實(shí)現(xiàn)。

AuthenticationManager新舊玩法對比

AuthenticationManager配置主要分為全局的(Global )、本地的(Local)。

舊玩法

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication();
    }
}

上面是通過WebSecurityConfigurerAdapter開啟的是本地配置。開啟全局配置需要覆寫其authenticationManagerBean()方法并標(biāo)記為Bean:

       @Bean(name name="myAuthenticationManager")
       @Override
       public AuthenticationManager authenticationManagerBean() throws Exception {
           return super.authenticationManagerBean();
       }

新玩法

本地配置通過HttpSecurity.authenticationManager實(shí)現(xiàn):

@Configuration
public class SecurityConfiguration {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
            .authorizeHttpRequests((authz) -> authz
                .anyRequest().authenticated()
            )
            .httpBasic(withDefaults())
            .authenticationManager(new CustomAuthenticationManager());
    }

}

全局配置擺脫了依賴WebSecurityConfigurerAdapter.authenticationManagerBean()方法,只需要定義一個(gè)AuthenticationManager類型的Bean即可:

    @Bean
    AuthenticationManager ldapAuthenticationManager(
            BaseLdapPathContextSource contextSource) {
        LdapBindAuthenticationManagerFactory factory = 
            new LdapBindAuthenticationManagerFactory(contextSource);
        factory.setUserDnPatterns("uid={0},ou=people");
        factory.setUserDetailsContextMapper(new PersonContextMapper());
        return factory.createAuthenticationManager();
    }

當(dāng)然還可以通過自定義GlobalAuthenticationConfigurerAdapter并注入Spring IoC來修改AuthenticationManagerBuilder,不限制數(shù)量,但是要注意有排序問題。相關(guān)的思維導(dǎo)圖:

最后

很多技術(shù)方案都不是直接更改的,是會(huì)有一個(gè)變化的過程,只要你緊追變化,其實(shí)也就沒有變化。

關(guān)注公眾號:碼農(nóng)小胖哥,獲取更多資訊

個(gè)人博客:https://felord.cn

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容