Spring Boot Swagger UI - 保护 UI 访问



通过在代码中添加以下类,我在现有的 springboot REST API 中添加了一个简单的 swagger UI:

@EnableSwagger2
@Configuration
public class SwaggerConfig {                                    
@Bean
public Docket api() { 
return new Docket(DocumentationType.SWAGGER_2)  
.select()
.paths(PathSelectors.regex("/v1.*"))
.build()
.pathMapping("/")
.apiInfo(metadata());
}

private ApiInfo metadata() {
return new ApiInfoBuilder()
.title("My awesome API")
.description("Some description")
.version("1.0")
.build();
}
}

我的问题是 API 应该是公开的,但招摇的文档不应该。我想要一种请求对 swagger 文档进行身份验证的方法,有人知道实现这一目标的任何简单方法吗?

我试图用谷歌搜索它,但我只能找到 OAth 的东西,但这是对端点的身份验证,而不是招摇的文档......

当Swagger 与 Spring 启动应用程序集成时,Swagger 文档将在/v2/api-docs端点上提供。

为了保护资源,利用 spring 安全性并限制访问文档的端点

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

安全配置:仅对端点的访问限制为用户

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()               
.antMatchers("/v2/api-docs").authenticated()
.and()
.httpBasic();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
}

此外,还可以根据要求保护 swagger-ui.html。

这是一个替代解决方案。这是关于仅在开发/qa 环境中限制对 swagger 的访问。生产环境将无法访问 Swagger。我正在使用属性 (prop.swagger.enabled( 作为标志,仅在开发/qa 环境中绕过 swagger-ui 的 spring 安全身份验证。

@Configuration
@EnableSwagger2
public class SwaggerConfiguration extends WebSecurityConfigurerAdapter implements WebMvcConfigurer {
@Value("${prop.swagger.enabled:false}")
private boolean enableSwagger;
@Bean
public Docket SwaggerConfig() {
return new Docket(DocumentationType.SWAGGER_2)
.enable(enableSwagger)
.select()
.apis(RequestHandlerSelectors.basePackage("com.your.controller"))
.paths(PathSelectors.any())
.build();
}
@Override
public void configure(WebSecurity web) throws Exception {
if (enableSwagger)  
web.ignoring().antMatchers("/v2/api-docs",
"/configuration/ui",
"/swagger-resources/**",
"/configuration/security",
"/swagger-ui.html",
"/webjars/**");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (enableSwagger) {
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
}

我使用这个锅炉盘来配置和保护招摇

@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebSecurityConfigurerAdapter {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any()).build();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/v2/api-docs",
"/configuration/ui",
"/swagger-resources/**",
"/configuration/security",
"/swagger-ui.html",
"/webjars/**")
.authenticated().and().httpBasic();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
}
}

最新更新