Swagger-ui with Spring security

后端 未结 3 1816
遇见更好的自我
遇见更好的自我 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 07:53

    First you should registry swagger's resources.

    @Configuration
    public class WebMvcConfig extends WebMvcConfigurerAdapter {
    
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("swagger-ui.html")
                    .addResourceLocations("classpath:/META-INF/resources/");
        }
    }
    

    Then cause you're using Spring Security,maybe you should shutdown privileges.

       @Override
        public void configure(WebSecurity web) throws Exception {
            web.ignoring().mvcMatchers(HttpMethod.OPTIONS, "/**");
            // ignore swagger 
            web.ignoring().mvcMatchers("/swagger-ui.html/**", "/configuration/**", "/swagger-resources/**", "/v2/api-docs");
        }
    

    And maybe it's better for you to use swagger which the version is under 2.8.0,or you may have to face to lots of bugs.

提交回复
热议问题