繁体   English   中英

无法在 spring 引导安全中公开 swagger doc ui/api

[英]Unable to make swagger doc ui/api public in spring boot security

我正在尝试将swagger添加到我现有的应用程序中。 我添加了以下依赖项:

   <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

现在 API /swagger-ui//v2/api-docs工作正常。 我正在 REST API 中开发应用程序。 当我与他们一起发送 JWT 令牌时, POST man的 API 工作正常。 他们不在浏览器中工作。

为了让它们在浏览器中工作,我在 spring 安全许可中添加了 URL。 但它仍然无法在浏览器中运行。

Spring 安全配置:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                .antMatchers(
                        .............
                        "/v2/api-docs",
                        "/swagger-ui.html")
                .permitAll().anyRequest().authenticated().and().addFilter(getAuthenticationFilter())
                .addFilter(new AuthorizationFilter(authenticationManager())).sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().cors();

    }

我怎样才能让这些 API 公开?

尝试使用安全配置

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private static final String[] AUTH_WHITELIST = {
            "/healthz"
    };
    
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring()
                .antMatchers(AUTH_WHITELIST);
    }
}

您需要公开更多的网址,而不仅仅是这两个。

Springfox 文档状态允许以下 url

.antMatchers(
          HttpMethod.GET,
          "/v2/api-docs",
          "/swagger-resources/**",
          "/swagger-ui.html**",
          "/webjars/**",
          "favicon.ico"
        ).permitAll()

或忽略它们:

@Configuration
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
  @Override
  public void configure(WebSecurity web) throws Exception {
    web.ignoring()
      .antMatchers(
        "/v2/api-docs",
        "/swagger-resources/**",
        "/swagger-ui.html**",
        "/webjars/**");
  }
}

来源: https://springfox.github.io/springfox/docs/current/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM