【发布时间】:2020-08-20 05:12:44
【问题描述】:
所以,我两天前才开始使用 Swagger,我看到了一个关于如何在我的 API 上配置基本 Swagger 文档的视频。该视频只是告诉您使用以下代码创建一个新类:
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("your-api-package")
.paths(PathSelectors.any())
.build();
}
我下载了 spring-instrument.jar 并将其添加到我的项目中。在 pom.xml 中添加了依赖项,仅此而已。然后当我尝试访问 localhost/my-project/swagger-ui.html 时出现 Unable to infer base url 错误。上网查了一下,发现我也需要添加Swagger Security,所以我最终下载了spring-security.jar。我用这段代码创建了一个 SwaggerSecurity 类:
private static final String[] AUTH_LIST = {
"/swagger-resources/**",
"/swagger-ui.html",
"/v2/api-docs",
"/webjars/**"
};
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests().antMatchers(AUTH_LIST).authenticated()
.and()
.httpBasic().authenticationEntryPoint(swaggerAuthenticationEntryPoint())
.and()
.csrf().disable();
}
@Bean
public BasicAuthenticationEntryPoint swaggerAuthenticationEntryPoint() {
BasicAuthenticationEntryPoint entryPoint = new BasicAuthenticationEntryPoint();
entryPoint.setRealmName("Swagger Realm");
return entryPoint;
}
这似乎解决了某些人的问题,但错误仍然存在。所以我想知道是否有人也遇到过这种情况,你们是如何解决这个问题的。我不知道它是否对我的问题有任何补充,但我正在使用最新的 Eclipse 版本来运行该项目。
【问题讨论】:
标签: java eclipse rest swagger swagger-ui