Spring Security即將棄用WebSecurityConfigurerAdapter配置類

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

相關的issues已經被處理並關閉

對此對此網友大呼“學着學着就被棄用了”。既然馬上要棄用了,總要有個過渡方案或者新玩法吧。

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

版本需要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 等攻擊的可能。以下示例僅僅作爲演示,請勿使用在生產環境。是不是又學到了呢?

舊玩法:

@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來實現。

AuthenticationManager新舊玩法對比

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

舊玩法

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

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

上面是通過WebSecurityConfigurerAdapter開啓的是本地配置。開啓全局配置需要覆寫其authenticationManagerBean()方法並標記爲Bean:

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

新玩法

本地配置通過HttpSecurity.authenticationManager實現:

@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()方法,只需要定義一個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();
    }

當然還可以通過自定義GlobalAuthenticationConfigurerAdapter並注入Spring IoC來修改AuthenticationManagerBuilder,不限制數量,但是要注意有排序問題。相關的思維導圖:

最後

很多技術方案都不是直接更改的,是會有一個變化的過程,只要你緊追變化,其實也就沒有變化。

關注公衆號:Felordcn 獲取更多資訊

個人博客:https://felord.cn

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章