Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to Access Swagger-UI After Implementing Spring Security Configuration #4080

Closed
Krish-Radadiya9 opened this issue Feb 9, 2024 · 2 comments

Comments

@Krish-Radadiya9
Copy link

I've already integrated Spring Security 6 and Spring Boot 3 into my service, which is currently functioning properly. However, I'm now facing an issue while trying to incorporate Swagger-UI. Despite configuring both Swagger and Spring Security, I'm unable to access the specific URL "http://localhost:8082/swagger-ui.html". Whenever I attempt to visit this URL, I encounter an error message stating "You don't have authorization to view this page" (HTTP ERROR 403). Additionally, I've configured Spring Security as follows:

@bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.csrf(AbstractHttpConfigurer::disable)
.cors(AbstractHttpConfigurer::disable)
.exceptionHandling()
.accessDeniedHandler(customAccessDeniedHandler)
.and()
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests((requests) -> requests
.requestMatchers("/swagger-ui.html").permitAll()
.requestMatchers("/admin/").hasRole("ADMIN")
.requestMatchers("/user/
").hasRole("USER")
.anyRequest()
.authenticated());
http.addFilterBefore(filter, UsernamePasswordAuthenticationFilter.class);
return http.build();
}

Despite including the particular configuration ".requestMatchers("/swagger-ui.html").permitAll()", I'm still unable to access the URL. If anyone is interested in assisting, please let me know, and I can provide further details about my project.
Thank you so much !!

@K1ethoang
Copy link

Me too
U have fixed?

@dGiorgiana
Copy link

for me it worked following https://medium.com/@javedalikhan50/comprehensive-guide-to-openapi-swagger-integration-in-spring-boot-with-spring-security-jwt-edf8c84e7d91 .
@bean
public WebSecurityCustomizer webSecurityCustomizer() {
return web -> web.ignoring().requestMatchers(
"/swagger-ui/", "/v3/api-docs/","/swagger-ui.html"
);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants