網(wǎng)站首頁 編程語言 正文
配置數(shù)據(jù)庫自定義的UserDetalisService的身份認證時,發(fā)現(xiàn)
WebSecurityConfigurerAdapter
已廢棄,在網(wǎng)上查閱了官方的案例文檔,再加上自己項目的自定義數(shù)據(jù)庫認證方案,將參考內(nèi)容整理如下,有問題歡迎大家指正。
在 Spring Security 5.7.0-M2 中,棄用了 WebSecurityConfigurerAdapter
,Spring 鼓勵用戶轉(zhuǎn)向基于組件的安全配置。
配置 HttpSecurity
在 Spring Security 5.4 中,Spring 引入了通過創(chuàng)建 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
是一個回調(diào)接口,可用于自定義 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。有關(guān)其他詳細信息,請參閱
configure
Javadoc。
全局認證管理器
要創(chuàng)建可供整個應(yīng)用程序使用的 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
設(shè)置為默認值的示例配置:
@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();
}
}
基于數(shù)據(jù)庫自定義UserDetailsService
這是自己使用的基于數(shù)據(jù)庫與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
···
}
}
參考網(wǎng)頁
1. 基于數(shù)據(jù)庫的SpringSecurity配置
2. Spring Security without the WebSecurityConfigurerAdapter
原文鏈接:https://blog.csdn.net/lazy_LYF/article/details/127284459
相關(guān)推薦
- 2022-06-08 VM配置Centos7虛擬機
- 2023-07-02 jQuery和HTML對某個標簽設(shè)置只讀或者禁用屬性的方式_jquery
- 2022-08-29 Python常見異常處理總結(jié)_python
- 2022-04-28 使用Matlab制作簡易版八分音符醬游戲_C 語言
- 2023-05-08 C語言單鏈表的圖文示例講解_C 語言
- 2023-01-15 利用Python實現(xiàn)讀取Word文檔里的Excel附件_python
- 2022-11-26 詳解Python中的with語句和上下文管理器_python
- 2022-06-01 docker安裝nginx并配置ssl的方法步驟_docker
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支