を拡張する場合は、スーパー コンストラクターにWebSecurityConfigurerAdapter
渡してデフォルトを無効にすることができます。
これを行う場合は、他の Bean を提供する必要がある場合があります。 true
/**
* 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;
}
テスト目的でのみ無効にする場合 - 自動構成を完全に無効にするのではなく、「SecurityConfiguration」に加えて「InsecurityConfiguration」を作成し、Spring プロファイルまたはプロパティ値でアクティブ化します。
技術的にはセキュリティはまだ構成されていますが、広く開かれています。
@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("/**");
}
}
注これは、webflux ではなく、mvc 用です。Webflux の場合、SecurityWebFilterChain
ブライアンが言及したようなものを作成する必要があります。
これは、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();
}