自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Spring Security 即將棄用配置類(lèi) WebSecurityConfigurerAdapter

開(kāi)發(fā) 架構(gòu)
Spring Security是一個(gè)功能強(qiáng)大且高度可定制的身份驗(yàn)證和訪問(wèn)控制框架。提供了完善的認(rèn)證機(jī)制和方法級(jí)的授權(quán)功能。是一款非常優(yōu)秀的權(quán)限管理框架。

用過(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ì)了不少呢?


責(zé)任編輯:武曉燕 來(lái)源: 碼農(nóng)小胖哥
相關(guān)推薦

2023-04-10 11:41:15

2022-01-26 11:08:07

Kubernetes容器dockershim

2024-02-02 09:08:32

2015-07-23 18:17:08

Windows 10微軟功能

2024-09-30 10:11:04

2020-12-03 18:29:30

KubernetesDocker容器

2024-10-17 14:14:29

2021-08-06 06:51:16

適配器配置Spring

2021-04-23 07:33:10

SpringSecurity單元

2021-08-29 18:36:57

項(xiàng)目

2022-02-16 23:58:41

Spring過(guò)濾器驗(yàn)證碼

2020-12-18 07:34:48

Spring配置文件

2022-04-18 07:42:31

配置機(jī)制Spring

2022-06-03 09:41:03

DockerKubernetes容器

2022-04-08 16:14:21

FedoraBIOS32 位操作系統(tǒng)

2023-09-22 16:54:42

GNOME 45系統(tǒng)

2021-06-04 05:21:19

KubernetesDocker容器

2024-06-27 13:45:21

2025-04-27 03:00:00

Spring技術(shù)接口

2023-10-12 07:46:02

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)