Swagger-ui with Spring security

后端 未结 3 1813
遇见更好的自我
遇见更好的自我 2021-01-23 07:21

I have a simple REST application with authentication service. I tried to add swagger and swagger-ui to it, but I can only see my endpoints in /v2/api-docs. In

3条回答
  •  深忆病人
    2021-01-23 08:13

    The previous answers helped me, but are not quite complete / outdated. I was facing the same issue and it's working now:

    @Configuration
    public class WebMvcConfiguration extends WebMvcConfigurationSupport {
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("swagger-ui.html")
                    .addResourceLocations("classpath:/META-INF/resources/");
    
            registry.addResourceHandler("/webjars/**")
                    .addResourceLocations("classpath:/META-INF/resources/webjars/");
        }
    }
    
    @Configuration
    @EnableWebSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    
      ...
    
      @Override
      public void configure(WebSecurity web) throws Exception {
        web
          .ignoring()
            .mvcMatchers("/swagger-ui.html/**", "/configuration/**", "/swagger-resources/**", "/v2/api-docs", "/webjars/**");
      }
    
      ...
    
    }
    

提交回复
热议问题