Swagger and Spring Security

If your project uses Spring Security and you have added Swagger to it then there is a little of additional configuration you need to do to make your /v2/api-docs and swagger-ui.html pages work.

Enable Swagger URLs in Spring Security Project

To enable Swagger URLs in a RESTful Web Services project build with Spring Boot and Spring Security Framework, make the following configuration to your Java class which extends the WebSecurityConfigurerAdapter and which is annotated with @EnableWebSecurity annotation.

.antMatchers("/v2/api-docs", "/configuration/**", "/swagger*/**", "/webjars/**")

below is an example of my WebSecurity Java class with the needed configuration to enable Swagger URLs.

package com.appsdeveloperblog.app.ws.security;

import com.appsdeveloperblog.app.ws.service.UserService;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

public class WebSecurity extends WebSecurityConfigurerAdapter {

    private final UserService userDetailsService;
    private final BCryptPasswordEncoder bCryptPasswordEncoder;

    public WebSecurity(UserService userDetailsService, BCryptPasswordEncoder bCryptPasswordEncoder) {
        this.userDetailsService = userDetailsService;
        this.bCryptPasswordEncoder = bCryptPasswordEncoder;

    protected void configure(HttpSecurity http) throws Exception {

                .antMatchers(HttpMethod.POST, SecurityConstants.SIGN_UP_URL)
                .antMatchers("/v2/api-docs", "/configuration/**", "/swagger*/**", "/webjars/**")
                .addFilter( new AuthenticationFilter(authenticationManager()) )
                .addFilter( new AuthorizationFilter( authenticationManager() ))

    public void configure(AuthenticationManagerBuilder auth) throws Exception {

Once you add the above-mentioned antMatchers to your WebSecurity Java class it should help you to get the /v2/api-docs and swagger-ui.html pages start working. When opening the above URLs do not forget to include the application path you have it configured in your application.properties file. For example, if your application properties file has the following entry:


then you will need to open the /v2/api-docs or the swagger-ui.htm pages this way:




If you are confused about other details on how to add Swagger to your Spring Boot application have a look at my other tutorial How to Add Swagger to a Spring Boot REST API Project.

To learn more about Swagger and how to work with it, have a look at the below video courses which teach Swagger.