Spring 引导安全性 - 如何禁用 Swagger 的安全性 UI
Spring Boot Security - How to disable security for Swagger UI
我有一个只有 REST 端点的应用程序。我通过以下方式启用了 oauth2 令牌安全性:
@Configuration
@EnableAuthorizationServer
public class AuthServerOAuth2Config extends AuthorizationServerConfigurerAdapter {
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("xxx").secret("xxx").accessTokenValiditySeconds(3600)
.authorizedGrantTypes("client_credentials")
.scopes("xxx", "xxx")
.and()
.withClient("xxx").secret("xxx").accessTokenValiditySeconds(3600)
.authorizedGrantTypes("password", "refresh_token")
.scopes("xxx", "xxx");
}
}
现在,如果我尝试访问我的任何端点,我会收到 401 Unauthorized,我首先必须通过 /oauth/token?grant_type=client_credentials
或 /oauth/token?grant_type=password
调用获得 access_token。如果我添加正确的 Authorization
header 和上次调用中返回的令牌,REST 端点将按预期工作。
但是,我无法访问 swagger-ui 页面。我通过以下方式启用了 swagger:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select().apis(RequestHandlerSelectors.basePackage("com.xxx"))
.paths(PathSelectors.regex("/xxx/.*"))
.build();
}
}
如果我去 localhost:8080/swagger-ui.html
我得到:
<oauth>
<error_description>
Full authentication is required to access this resource
</error_description>
<error>unauthorized</error>
</oauth>
所以我添加了以下内容以便能够访问 Swagger:
@Configuration
public class ResourceServerConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/swagger-ui.html")
.antMatchers("/webjars/springfox-swagger-ui/**")
.antMatchers("/swagger-resources/**")
.antMatchers("/v2/api-docs");
}
@Override
public void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
}
}
并在 @EnableWebMvc
class 中添加:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
现在我可以访问 Swagger UI 页面,但是 REST 端点的安全性被搞砸了。我的意思是,client_credentials 端点不再需要令牌,无论我做什么,密码端点都会给出 403 Forbidden。
我认为我的方法是错误的,但我不知道是什么。基本上我想要:
- 我所有 REST 端点上的 Oauth 令牌安全性(例如以 /api/* 开头)
- Swagger UI 页面应该可以访问
- swagger 页面上的端点应该有一种方法来指定 access_token
我该如何实现?
我就是这样解决的。我删除了扩展 WebSecurityConfigurerAdapter
的 class(见上文)并替换为:
@Configuration
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/xxx/**").authenticated();
http.authorizeRequests().anyRequest().permitAll();
http.csrf().disable();
}
}
要在 swagger 页面上启用令牌身份验证,我遵循了本教程:http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api
我有一个只有 REST 端点的应用程序。我通过以下方式启用了 oauth2 令牌安全性:
@Configuration
@EnableAuthorizationServer
public class AuthServerOAuth2Config extends AuthorizationServerConfigurerAdapter {
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("xxx").secret("xxx").accessTokenValiditySeconds(3600)
.authorizedGrantTypes("client_credentials")
.scopes("xxx", "xxx")
.and()
.withClient("xxx").secret("xxx").accessTokenValiditySeconds(3600)
.authorizedGrantTypes("password", "refresh_token")
.scopes("xxx", "xxx");
}
}
现在,如果我尝试访问我的任何端点,我会收到 401 Unauthorized,我首先必须通过 /oauth/token?grant_type=client_credentials
或 /oauth/token?grant_type=password
调用获得 access_token。如果我添加正确的 Authorization
header 和上次调用中返回的令牌,REST 端点将按预期工作。
但是,我无法访问 swagger-ui 页面。我通过以下方式启用了 swagger:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select().apis(RequestHandlerSelectors.basePackage("com.xxx"))
.paths(PathSelectors.regex("/xxx/.*"))
.build();
}
}
如果我去 localhost:8080/swagger-ui.html
我得到:
<oauth>
<error_description>
Full authentication is required to access this resource
</error_description>
<error>unauthorized</error>
</oauth>
所以我添加了以下内容以便能够访问 Swagger:
@Configuration
public class ResourceServerConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/swagger-ui.html")
.antMatchers("/webjars/springfox-swagger-ui/**")
.antMatchers("/swagger-resources/**")
.antMatchers("/v2/api-docs");
}
@Override
public void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
}
}
并在 @EnableWebMvc
class 中添加:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
现在我可以访问 Swagger UI 页面,但是 REST 端点的安全性被搞砸了。我的意思是,client_credentials 端点不再需要令牌,无论我做什么,密码端点都会给出 403 Forbidden。
我认为我的方法是错误的,但我不知道是什么。基本上我想要:
- 我所有 REST 端点上的 Oauth 令牌安全性(例如以 /api/* 开头)
- Swagger UI 页面应该可以访问
- swagger 页面上的端点应该有一种方法来指定 access_token
我该如何实现?
我就是这样解决的。我删除了扩展 WebSecurityConfigurerAdapter
的 class(见上文)并替换为:
@Configuration
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/xxx/**").authenticated();
http.authorizeRequests().anyRequest().permitAll();
http.csrf().disable();
}
}
要在 swagger 页面上启用令牌身份验证,我遵循了本教程:http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api