成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Spring Security 即將棄用配置類 WebSecurityConfigurerAdapter

開發 架構
Spring Security是一個功能強大且高度可定制的身份驗證和訪問控制框架。提供了完善的認證機制和方法級的授權功能。是一款非常優秀的權限管理框架。

用過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.authorizeHttpRequests的permitAll來實現。

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,不限制數量,但是要注意有排序問題。相關的思維導圖:

最后

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


責任編輯:武曉燕 來源: 碼農小胖哥
相關推薦

2022-01-26 11:08:07

Kubernetes容器dockershim

2023-04-10 11:41:15

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

2024-02-02 09:08:32

2021-08-06 06:51:16

適配器配置Spring

2021-08-29 18:36:57

項目

2021-04-23 07:33:10

SpringSecurity單元

2022-02-16 23:58:41

Spring過濾器驗證碼

2022-04-08 16:14:21

FedoraBIOS32 位操作系統

2023-09-22 16:54:42

GNOME 45系統

2024-06-27 13:45:21

2022-06-03 09:41:03

DockerKubernetes容器

2021-06-04 05:21:19

KubernetesDocker容器

2023-10-12 07:46:02

2011-03-23 09:40:37

Google GearChrome

2025-04-27 03:00:00

Spring技術接口

2020-12-18 07:34:48

Spring配置文件
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲福利网 | 国产欧美精品区一区二区三区 | 欧美在线观看一区 | 国产一区中文 | 欧美日韩一区不卡 | 91精品中文字幕一区二区三区 | 欧美久久久久久久久 | 黄色网址av | 国产一区二区三区四区区 | 中文字幕一区在线观看视频 | 国产一区二 | 午夜视频免费在线观看 | 天天操综合网 | 色播久久 | 久久精品国产清自在天天线 | 国产精品毛片 | 亚洲欧洲在线视频 | 中文字幕国产一区 | 亚洲欧美一区二区三区国产精品 | 国产日韩欧美在线一区 | 97视频精品| 国产精品久久久久久久久久免费看 | av小说在线 | 欧美一级片在线播放 | 国产成人免费视频网站视频社区 | 美女福利视频一区 | 麻豆精品国产91久久久久久 | 日韩精品极品视频在线观看免费 | 久久精品免费观看 | 欧美精品一| 久久久久久久久综合 | 狠狠色狠狠色综合日日92 | 国产精品日韩 | 日韩成人在线观看 | 亚洲精品日韩综合观看成人91 | 欧美精品久久久 | 欧美电影一区 | 草久网| 欧美日韩一区二区三区不卡视频 | 日韩精品一区二区三区 | 中文字幕精品视频 |