【发布时间】:2019-01-27 01:35:05
【问题描述】:
我正在尝试编写一个使用 Springs WebFlux Security 的应用程序。在前端 Angular 应用程序中,我将用户带到登录页面,一旦他们登录,应用程序应该重定向到不同的 uri。截至目前,我收到此错误:
Failed to load http://localhost:8080/login: Redirect from
'http://localhost:8080/login' to 'http://localhost:8080/pets' has been
blocked by CORS policy: No 'Access-Control-Allow-Origin' header is
present on the requested resource. Origin 'http://localhost:4200' is
therefore not allowed access.
目前我不确定为什么会出现 CORS 错误,但这是我对 spring webflux 安全性的了解
@EnableWebFluxSecurity
public class SecurityConfig {
@Autowired
private ReactiveUserDetailsService reactiveUserDetailsService;
@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http
.authorizeExchange()
.pathMatchers(HttpMethod.POST, "/login").permitAll()
.pathMatchers(HttpMethod.POST, "/logout").permitAll()
.pathMatchers(HttpMethod.POST, "/register").permitAll()
.anyExchange().authenticated()
.and()
.httpBasic().disable()
.csrf().disable()
.formLogin()
.authenticationManager(authenticationManager())
.requiresAuthenticationMatcher(ServerWebExchangeMatchers.pathMatchers(HttpMethod.POST, "/login"))
.authenticationSuccessHandler(new RedirectServerAuthenticationSuccessHandler("/pets"))
.authenticationFailureHandler(new ServerAuthenticationEntryPointFailureHandler(new HttpBasicServerAuthenticationEntryPoint()))
.authenticationEntryPoint(new HttpBasicServerAuthenticationEntryPoint())
.and()
.logout()
.logoutSuccessHandler(((exchange, authentication) -> Mono.fromRunnable(() -> exchange.getExchange().getResponse().setStatusCode(HttpStatus.OK))));
return http.build();
}
@Bean
public ReactiveAuthenticationManager authenticationManager() {
UserDetailsRepositoryReactiveAuthenticationManager authManager =
new UserDetailsRepositoryReactiveAuthenticationManager(reactiveUserDetailsService);
authManager.setPasswordEncoder(passwordEncoder());
return authManager;
}
@Bean
PasswordEncoder passwordEncoder() {
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}
}
我有什么遗漏或需要修改的吗?
【问题讨论】:
-
你确定你可以从4200端口向8080端口发起http请求吗?
标签: angular spring-boot spring-security spring-webflux