让我简要地描述一下我现在面临的问题。
我已经为webflux应用程序配置了spring安全,当我尝试访问不需要身份验证的路由时,会收到登录表单提示。路由是/swagger-ui/,它应该在没有任何登录表单或其他东西的情况下打开。
下面是我在SecurityWebFilterChain中拥有的代码
@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
//@formatter:off
return http
.formLogin().disable()
.httpBasic().disable()
.authenticationManager(authenticationManager)
.securityContextRepository(securityContextRepository)
.authorizeExchange()
.pathMatchers(HttpMethod.OPTIONS).permitAll()
.pathMatchers("/v2/api-docs", "/v3/api-docs", "/configuration/ui", "/swagger-resources",
"/configuration/security", "/swagger-ui/", "/swagger-ui",
"/webjars/**", "/swagger-resources/configuration/ui",
"/swagger-resources/configuration/security").permitAll() // Allowed routes for swagger
.pathMatchers("/api/auth", "/api/auth/**").permitAll() // Allowed routes for auth
.and()
.authorizeExchange()
.anyExchange()
.authenticated() // All other routes require authentication
.and()
.csrf().disable()
.headers()
.hsts()
.includeSubdomains(true)
.maxAge(Duration.ofSeconds(31536000))
.and()
.frameOptions().mode(XFrameOptionsServerHttpHeadersWriter.Mode.SAMEORIGIN)
.and()
.build();
//@formatter:on
}
}如果任何人有任何建议,请让我知道,我将不胜感激。这是我在浏览器中得到的图片。

发布于 2020-12-10 07:41:39
这个问题也让我很恼火。问题是,通过将.httpBasic().disable()放在代码中,您希望spring跳过基本身份验证(浏览器窗口),但它没有。
相反,可以尝试为.httpBasic()提供一个ServerAuthenticationEntryPoint。
最简单的是HttpStatusServerEntryPoint。
例如,在您的代码中将更改为:
***
return http
.formLogin().disable()
.httpBasic().authenticationEntryPoint(new HttpStatusServerEntryPoint(HttpStatus.UNAUTHORIZED))
.authenticationManager(authenticationManager)
***通过更改,您的服务器将返回一个401 UNAUTHORIZED HttpStatus,而不是浏览器窗口!干杯!
https://stackoverflow.com/questions/63614553
复制相似问题