如何解决Spring Webflux禁用登录
让我简短地描述一下我现在面临的问题。
我已经为webflux应用程序配置了spring security,当我尝试访问不需要身份验证的路由时,系统提示登录表单。路由为/ 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
}
}
如果有人有任何建议,请让我知道,我将不胜感激。这是我在浏览器中得到的图片。
解决方法
这个问题也让我非常恼火。问题在于,通过将.httpBasic().disable()
放入代码中,您将期望spring跳过基本身份验证(该浏览器窗口),但事实并非如此。
相反,请尝试向ServerAuthenticationEntryPoint
提供一个.httpBasic()
。
最简单的一个是HttpStatusServerEntryPoint。
例如将您的代码更改为:
***
return http
.formLogin().disable()
.httpBasic().authenticationEntryPoint(new HttpStatusServerEntryPoint(HttpStatus.UNAUTHORIZED))
.authenticationManager(authenticationManager)
***
通过更改服务器将返回401 UNAUTHORIZED
HttpStatus而不是该浏览器窗口!
干杯!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。