網站首頁 編程語言 正文
配置數據庫自定義的UserDetalisService的身份認證時,發現
WebSecurityConfigurerAdapter
已廢棄,在網上查閱了官方的案例文檔,再加上自己項目的自定義數據庫認證方案,將參考內容整理如下,有問題歡迎大家指正。
在 Spring Security 5.7.0-M2 中,棄用了 WebSecurityConfigurerAdapter
,Spring 鼓勵用戶轉向基于組件的安全配置。
配置 HttpSecurity
在 Spring Security 5.4 中,Spring 引入了通過創建 SecurityFilterChain
bean 來配置 HttpSecurity
的能力。以下是使用 (不推薦使用)WebSecurityConfigurerAdapter
的示例配置,該配置使用 HTTP Basic
保護所有站點:
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((authz) -> authz
.anyRequest().authenticated()
)
.httpBasic(withDefaults());
}
}
推薦的方法是注冊一個 SecurityFilterChain
bean:
@Configuration
public class SecurityConfiguration {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((authz) -> authz
.anyRequest().authenticated()
)
.httpBasic(withDefaults());
return http.build();
}
}
配置WebSecurity
在 Spring Security 5.4 中,Spring 還引入了 WebSecurityCustomizer
。WebSecurityCustomizer
是一個回調接口,可用于自定義 WebSecurity
。下面是使用 (不推薦使用)WebSecurityConfigurerAdapter
的示例配置,它忽略匹配 /ignore1
或 /ignore2
的請求:
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) {
web.ignoring().antMatchers("/ignore1", "/ignore2");
}
}
推薦的方法是注冊一個 WebSecurityCustomizer
bean:
@Configuration
public class SecurityConfiguration {
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return (web) -> web.ignoring().antMatchers("/ignore1", "/ignore2");
}
}
警告:如果將 WebSecurity 配置為忽略請求,請考慮通過 HttpSecurity#authorizeHttpRequests 使用 permitAll。有關其他詳細信息,請參閱
configure
Javadoc。
全局認證管理器
要創建可供整個應用程序使用的 AuthenticationManager
,您只需將 AuthenticationManager
注冊為 @Bean
。
@Configuration
public class SecurityConfiguration {
@Bean
public EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean() {
EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean =
EmbeddedLdapServerContextSourceFactoryBean.fromEmbeddedLdapServer();
contextSourceFactoryBean.setPort(0);
return contextSourceFactoryBean;
}
@Bean
AuthenticationManager ldapAuthenticationManager(
BaseLdapPathContextSource contextSource) {
LdapBindAuthenticationManagerFactory factory =
new LdapBindAuthenticationManagerFactory(contextSource);
factory.setUserDnPatterns("uid={0},ou=people");
factory.setUserDetailsContextMapper(new PersonContextMapper());
return factory.createAuthenticationManager();
}
}
本地身份驗證管理器
在 Spring Security 5.6 中,Spring 引入了 HttpSecurity#authenticationManager 方法,它覆蓋了特定 SecurityFilterChain
的默認 AuthenticationManager
。
下面是一個將自定義 AuthenticationManager
設置為默認值的示例配置:
@Configuration
public class SecurityConfiguration {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((authz) -> authz
.anyRequest().authenticated()
)
.httpBasic(withDefaults())
.authenticationManager(new CustomAuthenticationManager());
return http.build();
}
}
基于數據庫自定義UserDetailsService
這是自己使用的基于數據庫與JWT的認證方案。
以下是使用 (不推薦使用)WebSecurityConfigurerAdapter
的示例配置,該配置使用 HTTP Basic
保護所有站點:
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
@Bean
public BCryptPasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userServiceImpl);
}
@Override
public void configure(HttpSecurity httpSecurity) throws Exception {
//Configuring HttpSecurity
···
}
}
推薦的方法是注冊一個 SecurityFilterChain
bean:
@Configuration
public class SecurityConfig{
@Bean
public BCryptPasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
@Bean
AuthenticationManager authenticationManager(HttpSecurity httpSecurity) throws Exception {
AuthenticationManager authenticationManager = httpSecurity.getSharedObject(AuthenticationManagerBuilder.class)
.userDetailsService(userServiceImpl)
.passwordEncoder(passwordEncoder())
.and()
.build();
return authenticationManager;
}
@Bean
public SecurityFilterChain securityFilterChain(AuthenticationManager authenticationManager, HttpSecurity httpSecurity) throws Exception {
//Configuring HttpSecurity
···
}
}
參考網頁
1. 基于數據庫的SpringSecurity配置
2. Spring Security without the WebSecurityConfigurerAdapter
原文鏈接:https://blog.csdn.net/lazy_LYF/article/details/127284459
相關推薦
- 2023-01-09 GO中優雅編碼與降低圈復雜度詳析_Golang
- 2022-09-21 Oracle數據庫對象的使用詳解_oracle
- 2022-12-02 C語言中qsort函數用法及用冒泡排序實現_C 語言
- 2023-02-06 Python利用Pytorch實現繪制ROC與PR曲線圖_python
- 2022-06-22 配置Git并從GitHub上克隆項目_其它綜合
- 2022-10-22 SQL?Server實現group_concat功能的詳細實例_MsSql
- 2022-06-04 Kubernetes中Deployment的升級與回滾_云和虛擬化
- 2022-06-18 python使用Random隨機生成列表的方法實例_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支