如果要扩展WebSecurityConfigurerAdapter
,可以传入true
超级构造函数以禁用默认值。
如果您这样做,您可能需要提供其他 bean。
/**
* Creates an instance which allows specifying if the default configuration should be
* enabled. Disabling the default configuration should be considered more advanced
* usage as it requires more understanding of how the framework is implemented.
*
* @param disableDefaults true if the default configuration should be disabled, else
* false
*/
protected WebSecurityConfigurerAdapter(boolean disableDefaults) {
this.disableDefaults = disableDefaults;
}
如果您只想出于测试目的禁用它 - 除了完全禁用自动配置之外,我还创建了一个“InsecurityConfiguration”,并使用 Spring Profile 或 Property 值激活它。
从技术上讲,安全性仍然是配置的,但是是开放的。
@Configuration
@ConditionalOnProperty(prefix = "security", value = "disabled", havingValue = "true")
public class InsecurityConfiguration extends WebSecurityConfigurerAdapter {
private final static Logger log = LoggerFactory.getLogger(InsecurityConfiguration.class);
@Override
protected void configure(HttpSecurity http) throws Exception {
log.warn("configuring insecure HttpSecurity");
http.authorizeRequests().anyRequest().permitAll();
}
@Override
public void configure(WebSecurity web) throws Exception {
log.warn("configuring insecure WebSecurity");
web.ignoring().antMatchers("/**");
}
}
注意这适用于 mvc,而不是 webflux。对于 Webflux,您应该创建一个SecurityWebFilterChain
类似 Bryan 提到的。
这就是我在使用 JWT 时通常在 webflux 中禁用基本身份验证的方式 -
@Bean
public SecurityWebFilterChain configure(ServerHttpSecurity http) {
http
.authorizeExchange().anyExchange().authenticated().and()
.httpBasic().disable()
.formLogin().disable()
.logout().disable()
.oauth2ResourceServer()
.jwt()
.and()
.and().exceptionHandling().accessDeniedHandler(problemSupport);
return http.build();
}