spring-boot - Spring Boot Security - 如何禁用 Swagger UI 的安全性

标签 spring-boot swagger-ui spring-security-oauth2

我有一个只有 REST 端点的应用程序。我通过以下方式启用了 oauth2 token 安全性:

@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/获取 access_token token?grant_type=password 调用。如果我添加正确的 Authorization header 和上次调用中返回的 token ,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 类中我添加了:

@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 端点不再需要 token ,无论我做什么,password 端点都会给出 403 Forbidden。

我认为我的方法是错误的,但我不知道是什么。基本上我想要:

  • 我所有 REST 端点上的 Oauth token 安全性(例如以/api/* 开头)
  • Swagger UI 页面应该可以访问
  • swagger 页面上的端点应该有一种方法来指定 access_token

我如何实现这一目标?

最佳答案

我就是这样解决的。我删除了扩展 WebSecurityConfigurerAdapter 的类(见上文)并替换为:

@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 页面上启用 token 身份验证,我遵循了本教程:http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api

关于spring-boot - Spring Boot Security - 如何禁用 Swagger UI 的安全性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49642894/

相关文章:

java - 错误 : Unable to find @SpringBootConfiguration when doing @WebMvcTest for Spring Controller

java - 由于 PostgreSQL 数据库上的 "duplicate"索引,Liquibase 无法执行迁移

java - 如何修复无法调用元素 RollingFile 类 org.apache.logging.log4j.core.appender.RollingFileAppender 中的工厂方法?

ServiceStack.Api.Swagger SwaggerResourcesService 不包括以 Route Parameter 开头的路由

java - 使用额外回调处理自定义登录

java - Spring Boot 在测试中不 Autowiring 类

annotations - 在数组字段请求上添加模型模式捕获元素

servicestack - 如何在 Servicestack 的 Swagger UI 中隐藏端点?

spring-security - 如何在 spring security 中更新刷新 token 的到期时间而不生成新的刷新 token ?

spring-security - 如何在 jhipster 中通过 OAuth2 成功登录时执行操作