org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter的替代
时间: 2023-10-01 15:03:52 浏览: 153
SpringSecurity_day03.pdf
在Spring Security 5.0之后,建议使用实现了接口SecurityConfigurer的配置类来配置WebSecurity。这些配置类需要注入到WebSecurityConfigurerAdapter中。
例如,可以创建一个类实现SecurityConfigurer接口,如下所示:
```
@Configuration
@EnableWebSecurity
public class SecurityConfig implements SecurityConfigurer<DefaultSecurityFilterChain, HttpSecurity> {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/public/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
}
```
然后,在WebSecurityConfigurerAdapter中注入该配置类,如下所示:
```
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private SecurityConfig securityConfig;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.apply(securityConfig);
}
}
```
通过以上方式,可以实现类似WebSecurityConfigurerAdapter的功能。
阅读全文