Lao*_*dao 9 swagger swagger-ui spring-boot swagger-2.0
我通过@EnableSwagger2 启用 swagger2。但是,当我尝试点击“/swagger-ui.html”时,它首先点击了我的身份验证过滤器。然后,我编写了以下代码来绕过身份验证检查
String resourcePath = new UrlPathHelper().getPathWithinApplication(httpRequest);
if ("/swagger-ui.html".equalsIgnoreCase(resourcePath)) {
filterChain.doFilter(request, response);
}
Run Code Online (Sandbox Code Playgroud)
我可以看到filterChain.doFilter(request, response);被击中了。但是,当我进行调试时,它会返回一个包含以下信息的页面
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Apr 04 15:41:50 EDT 2018
There was an unexpected error (type=Unauthorized, status=401).
No message available
Run Code Online (Sandbox Code Playgroud)
有什么想法吗,伙计们?
小智 5
认为您可以添加自己的方法WebSecurityConfig extends WebSecurityConfigurerAdapter,而不是重写configure(WebSecurity web)方法,然后web.ignoring().antMatchers("/swagger-ui.html")使用 @Configuration 注释该类
小智 5
然后我在我的项目中遇到了同样的问题, 首先将配置文件添加到项目中
package bla.bla.conf;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors
.basePackage("bla.bla.controllers"))
.paths(PathSelectors.any())
.build();
}
}
Run Code Online (Sandbox Code Playgroud)
并添加 WebSecurityConfig
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().mvcMatchers(HttpMethod.OPTIONS, "/**");
web.ignoring().mvcMatchers("/swagger-ui.html/**", "/configuration/**", "/swagger-resources/**", "/v2/api-docs","/webjars/**");
}
Run Code Online (Sandbox Code Playgroud)
我的问题解决了
| 归档时间: |
|
| 查看次数: |
18567 次 |
| 最近记录: |