问题
In a non reactive spring application I would usually create a configuration class, extend WebSecurityConfigurerAdapter
and configure the WebSecurity
like such:
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/pathToIgnore");
}
How can I do the equivalent in a reactive application?
回答1:
In your security config class which you have annotated with @EnableWebFluxSecurity
and @EnableReactiveMethodSecurity
, register a bean as follows:
@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
return http.authorizeExchange()
.pathMatchers("/pathToIgnore")
.permitAll()
.anyExchange()
.authenticated()
.and()
.formLogin()
.and()
.csrf()
.disable()
.build();
}
In this config, pathMatchers("/pathToIgnore").permitAll()
would configure it to allow the paths matched to be excluded from auth and anyExchange().authenticated()
would configure it to authenticate all other requests.
来源:https://stackoverflow.com/questions/52178552/how-to-exclude-a-path-from-authentication-in-a-spring-based-reactive-application