Ern*_*ani 8 authentication spring swagger-ui spring-boot
我通过在我的代码中添加以下类,向现有的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();
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是API应该是公开的,但是swagger文档不应该是公开的.我想要一种请求对swagger文档进行身份验证的方法,任何人都知道实现这一目的的任何简单方法吗?
我试图谷歌它,但我只能找到OAth的东西,但这是端点的身份验证而不是昂首阔步的文档......
Bar*_*ath 14
当swagger与spring boot应用程序集成时,将在/ v2/api-docs端点提供Swagger文档.
为了保护资源,请使用spring安全性并限制端点以访问文档
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
安全配置:仅限制对用户的端点访问
@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");
}
}
Run Code Online (Sandbox Code Playgroud)
此外,还可以根据要求保护swagger-ui.html.
归档时间: |
|
查看次数: |
8929 次 |
最近记录: |