Spring Security 即將棄用配置類(lèi) WebSecurityConfigurerAdapter
用過(guò)WebSecurityConfigurerAdapter的都知道對(duì)Spring Security十分重要,總管Spring Security的配置體系。但是馬上這個(gè)類(lèi)要廢了,你沒(méi)有看錯(cuò),這個(gè)類(lèi)將在5.7版本被@Deprecated所標(biāo)記了,未來(lái)這個(gè)類(lèi)將被移除。
相關(guān)的issues已經(jīng)被處理并關(guān)閉
對(duì)此對(duì)此網(wǎng)友大呼“學(xué)著學(xué)著就被棄用了”。既然馬上要啟用了,總要有個(gè)過(guò)渡方案或者新玩法吧。
早在2021年3月份胖哥就寫(xiě)了一篇文章,把新玩法給明明白白說(shuō)清楚了,如果你看了的話,肯定不會(huì)學(xué)廢棄技術(shù)。這里把整套的替代方案再搞一遍,可別再學(xué)過(guò)時(shí)技術(shù)了。
版本需要Spring Security 5.4.x及以上。
HttpSecurity新舊玩法對(duì)比
舊玩法:
@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新舊玩法對(duì)比
使用WebSecurity.ignoring()忽略某些URL請(qǐng)求,這些請(qǐng)求將被Spring Security忽略,這意味著這些URL將有受到 CSRF、XSS、Clickjacking 等攻擊的可能。以下示例僅僅作為演示,請(qǐng)勿使用在生產(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,請(qǐng)考慮通過(guò)HttpSecurity.authorizeHttpRequests的permitAll來(lái)實(shí)現(xiàn)。
AuthenticationManager新舊玩法對(duì)比
AuthenticationManager配置主要分為全局的(Global )、本地的(Local)。
舊玩法:
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication();
}
}
上面是通過(guò)WebSecurityConfigurerAdapter開(kāi)啟的是本地配置。開(kāi)啟全局配置需要覆寫(xiě)其authenticationManagerBean()方法并標(biāo)記為Bean:
@Bean(name name="myAuthenticationManager")
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
新玩法:
本地配置通過(guò)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類(lèi)型的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)然還可以通過(guò)自定義GlobalAuthenticationConfigurerAdapter并注入Spring IoC來(lái)修改AuthenticationManagerBuilder,不限制數(shù)量,但是要注意有排序問(wèn)題。相關(guān)的思維導(dǎo)圖:
最后
很多技術(shù)方案都不是直接更改的,是會(huì)有一個(gè)變化的過(guò)程,只要你緊追變化,其實(shí)也就沒(méi)有變化。這一篇是不是學(xué)會(huì)了不少呢?