首先,让我们看看如何配置`HTTPSecurity`。在旧的方法中,我们通常会重写`configure(HTTPSecurity http)`方法来设置我们的安全规则。现在,我们可以使用`SecurityFilterChain` bean来替代。以下是新写法的示例:
```java @Bean SecurityFilterChain filterChain(HttpSecurity http) throws Exception { return http .antMatcher("/**") .authorizeRequests(authorize -> authorize .anyRequest().authenticated() ) .build(); } ```
这种方式更加模块化,它将安全配置作为一个bean返回,使代码更加清晰。
接下来是`WebSecurity`的配置。在过去,我们可能会重写`configure(WebSecurity web)`方法来忽略某些URL。现在,我们可以使用`WebSecurityCustomizer`来替代。以下是一个简单的例子:
```java @Bean public WebSecurityCustomizer webSecurityCustomizer() { return (web) -> web.ignoring().antMatchers("/ignorE1", "/ignore2"); } ```
需要注意的是,`WebSecurity`配置并不常用。如果需要忽略某些URL,推荐使用`HttpSecurity.authorizeHttpRequests().permitAll()`方法来实现。
然后是认证管理器的配置。在过去,我们可能会重写`configure(AuthenticationManagerBuilder auth)`方法来配置用户详情和密码编码器。现在,我们可以直接定义一个`AuthenticationManager` bean。以下是一个例子:
```java @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http .authorizeHttpRequests(authz -> authz .anyRequest().authenticated() ) .httpBASIC(withDefaults()) .authenticationManager(new CustomAuthenticationManager()); } ```
全局认证管理器可以这样配置:
```java @Bean public AuthenticationManager authenticationManager(HttpSecurity httpSecurity) throws Exception { return httpSecurity.getSharedObject(AuthenticationManagerBuilder.class) .userDetailsService(userDetailsService) .passwordEncoder(bCryptPasswordEncoder()) .and() .build(); } ```
这些新的配置方式不仅使代码更加简洁,还提供了更好的灵活性和可维护性。
技术总是在不断进步和迭代。作为技术人员,我们不能固守旧有的方法,而应该学会拥抱变化,掌握新的技术和工具。通过采用这些新的Spring Security配置方法,我们可以构建更加安全、高效和可扩展的应用程序。这不仅能够提高我们的工作效率,还能为用户提供更加安全的体验。
0. 概述
以前我们配置 SpringSecurity 的方式是继承 WebSecurityConfigurerAdapter
,然后重写其中的几个方法:
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//配置 Spring Security 中的过滤器链
@Override
void configure(HttpSecurity http) {}
//配置路径放行规则
@Override
void configure(WebSecurity web) {}
//配置本地认证管理器
@Override
void configure(AuthenticationManagerBuilder auth) {}
//配置全局认证管理器
@Override
AuthenticationManager authenticationManagerBean() {}
}
目前这个类已经过期,虽然可以继续使用,但是总觉得别扭。那么它的替代方案是什么?下面我来为大家一一介绍。
1. HttpSecurity
原写法:
@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();
}
2. WebSecurity
原写法:
@Override
public void configure(WebSecurity web) {
web.ignoring().antMatchers("/ignore1", "/ignore2");
}
新写法:
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return (web) - > web.ignoring().antMatchers("/ignore1", "/ignore2");
}
WebSecurity配置不常使用,如果需要忽略Url,推荐通过
HttpSecurity.authorizeHttpRequests
的permitAll
来实现。
3. AuthenticationManager
原写法:
@Autowired
private UserDetailsService userDetailsService;
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
//Local
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
}
//Global
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
新写法:
@Autowired
private UserDetailsService userDetailsService;
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
//Local
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((authz) - > authz
.anyRequest().authenticated()
)
.httpBasic(withDefaults())
.authenticationManager(new CustomAuthenticationManager());
}
//Global
@Bean
public AuthenticationManager authenticationManager(HttpSecurity httpSecurity) throws Exception {
return httpSecurity.getSharedObject(AuthenticationManagerBuilder.class)
.userDetailsService(userDetailsService)
.passwordEncoder(bCryptPasswordEncoder())
.and()
.build();
}
4. 心得
技术是不断迭代的,我们作为技术人员,不能墨守成规,要学会拥抱变化。