Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Let me shortly describe what problem i am facing right now.
I have configured spring security for webflux application, and i am getting login form prompted, when i try to access the route that doesn't require authentication. The route is /swagger-ui/ and it should get opened without any login forms or whatever.
Below is the code i have within the 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
If anyone has any suggestions, please let me know, i will appreciate it. Here is the picture what i got in the browser.
–
–
–
–
I was really annoyed by this issue too. The problem is that by putting
.httpBasic().disable()
in your code you would expect spring to skip basic authentication (that browser window) but it doesn't.
Instead, try providing a
ServerAuthenticationEntryPoint
to the
.httpBasic()
.
The most simple one is the
HttpStatusServerEntryPoint
.
For example in your code change to:
return http
.formLogin().disable()
.httpBasic().authenticationEntryPoint(new HttpStatusServerEntryPoint(HttpStatus.UNAUTHORIZED))
.authenticationManager(authenticationManager)
By changing that your server will return a
401 UNAUTHORIZED
HttpStatus instead of that browser window!
Cheers!
Thanks for contributing an answer to Stack Overflow!
-
Please be sure to
answer the question
. Provide details and share your research!
But
avoid
…
-
Asking for help, clarification, or responding to other answers.
-
Making statements based on opinion; back them up with references or personal experience.
To learn more, see our
tips on writing great answers
.