Search code examples
javaspringswaggerspringfox

Swagger UI is prompting for login even after ignoring in configuration after migrating to Java 17


 <dependency>
     <groupId>io.springfox</groupId>
     <artifactId>springfox-swagger2</artifactId>
     <version>2.9.2</version>
 </dependency>

 <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-bean-validators</artifactId>
      <version>2.9.2</version>
 </dependency>

 <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>2.9.2</version>
 </dependency>

// Web Security Config 

public static final String[] excludedURLs = {
            "/swagger-ui.html", "/webjars/**", "/swagger-resources/**", "/v2/**"};
@Bean
    public WebSecurityCustomizer webSecurityCustomizer() {
        return (web) -> web.ignoring().antMatchers(excludedURLs);
    }

@Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.authenticationProvider(XXXXAPIAuthenticationProvider)
                     .httpBasic()
                     .and()
                     .authorizeRequests()
                     .antMatchers(excludedURLs).permitAll()
                    .anyRequest().authenticated()
                    .and()
                    .addFilter(getXXXXXBasicAuthenticationFilter())
                    .addFilterBefore(getOktaAuthFilter(), XXXXXXBasicAuthenticationFilter.class)
                    .csrf().disable();

        return http.build();
    }

// Swagger Config

@Profile({"Test","STAGE"})
@Configuration
@EnableSwagger2
public class SwaggerConfig {


    private static final String AUTHORIZATION_HEADER = "Authorization";
    public static final String DEFAULT_INCLUDE_PATTERN = "/.*";

    @Bean
    public Docket api() {
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .securityContexts(Lists.newArrayList(securityContext()))
                .securitySchemes(Lists.newArrayList(apiKey()))
                .useDefaultResponseMessages(false)
                .select()
                .apis(RequestHandlerSelectors
                        .basePackage("com.XXXX.XXXX.controllers"))
                .paths(regex("/.*"))
                .build()
                .apiInfo(apiEndPointsInfo());
        docket = docket.select()
                .paths(regex(DEFAULT_INCLUDE_PATTERN))
                .build();

        return docket;
    }

I have gone through below posts

Why does springfox-swagger2 UI tell me "Unable to infer base url."

https://github.com/springfox/springfox/issues/2191

https://github.com/springfox/springfox/issues/2907

Unable to infer base url... springfox-swagger2 version 2.9.2

Please let me know what am I missing


Solution

  • Old swagger ( <= 2.9.2 or v1/docs, v2/docs ) is not working with Java 17 or higher version hence I upgraded the Swagger to use open API. Added below maven dependency in application pom.xml

      <dependency>
          <groupId>org.springdoc</groupId>
          <artifactId>springdoc-openapi-ui</artifactId>
          <version>1.6.15</version>
      </dependency>
    

    Removed all swagger related dependency related to version 2.9.2 and also no need to explicitly define SwaggerConfig.java Removed this class.

    Just permitALL paths "/v3/api-docs/", "/swagger-ui/" in spring security configuration as below and swagger UI is able to open without prompting any authentication.

    public static final String[] excludedURLs = {"/v3/api-docs/**", "/swagger-ui/**"};
    
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
            http.authenticationProvider(xxxAPIAuthenticationProvider)
                    .httpBasic()
                    .and()
                    .authorizeRequests()
                    .antMatchers(excludedURLs).permitAll()
                    .anyRequest().authenticated();
    
       return http.build();
    
    }