首页 文章

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

提问于
浏览
1

我有一个只有REST endpoints 的应用程序 . 我通过以下方式启用了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");

    }
}

现在,如果我尝试访问任何 endpoints ,我将获得401 Unauthorized,我首先必须通过 /oauth/token?grant_type=client_credentials/oauth/token?grant_type=password 调用获取access_token . 如果我使用前一次调用中返回的令牌添加正确的 Authorization 标头,则REST endpoints 将按预期工作 .

但是,我无法访问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 endpoints 的安全性搞砸了 . 我的意思是,client_credentials endpoints 不再需要令牌,无论我做什么,密码 endpoints 都会提供403 Forbidden .

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

  • 所有REST endpoints 上的Oauth令牌安全性(例如以/ api / *开头)

  • Swagger UI页面应该是可访问的

  • swagger页面上的 endpoints 应该有一种指定access_token的方法

我该如何实现这一目标?

1 回答

  • 0

    这是我修复它的方式 . 我删除了扩展 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页面上启用令牌身份验证,我遵循了本教程:http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api

相关问题