【问题标题】:Accessing Springfox local swagger ui triggers HttpServletRequests访问Springfox本地swagger ui触发HttpServletRequests
【发布时间】:2021-03-19 10:10:47
【问题描述】:

我有一个带有一些休息端点和一个招摇的 ui 页面的 spring boot 项目。我可以使用 springfox-swagger-ui & springfox-swagger2 2.9.2(或 http://localhost:8080/swagger)在 http://localhost:8080/swagger-ui.html#/ 成功访问 swagger 页面-ui/ 使用 springfox-boot-starter 3.0.0 时)。作为我的安全配置的一部分,我有一个简单的身份验证过滤器,可用于验证用户将能够在 swagger 页面中输入的 api 密钥。

但是,我的问题是,每次我进入 swagger 页面时,它都会在我的过滤器中自动调用 tryAuthentication() 方法(并且多次调用)。这不应该发生。只有当我输入关键信息然后点击页面上的授权时才会调用它。

尝试身份验证的 HttpServletRequest 对于所有 10 个请求都是相同的类型。所有被触发的请求都是在不同 URI 上的 GET。这是请求类型

SecurityContextHolderAwareRequestWrapper[org.springframework.security.web.header.HeaderWriterFilter$HeaderWriterRequest]

这些是所有的 URI:

/swagger-ui/

/swagger-ui/springfox.css

/swagger-ui/swagger-ui.css

/swagger-ui/swagger-ui-bundle.js

/swagger-ui/swagger-ui-standalone-preset.js

/swagger-ui/springfox.js

/swagger-resources/configuration/ui

/swagger-resources/配置/安全

/swagger-资源

/v2/api-docs

SimpleAuthFilter

public class SimpleAuthFilter extends AbstractAuthenticationProcessingFilter {

  public SimpleAuthFilter(final RequestMatcher req) {
    super(req)
    setAuthenticationManager(new MyAuthManager())
  }

  @Override
  public Authentication attemptAuthentication(HttpServletRequest req, HttpServletResponse res) {
    // -> Getting called here when visting swagger page
  }

  @Override
  protected void successfulAuthentication(final HttpServletRequest req, final HttpServletResponse, final FilterChain chain, final Authentication auth) throws IOException, ServletException {
    SecurityContextHolder.getContext().setAuthentication(auth);
    chain.doFilter(req, res);
  }
}

我的授权管理器

private class MyAuthManager implements AuthenticationManager {
    @Override
    public Authentication authenticate(Authentication auth) throws AuthenticationException {
        throw new AuthenticationServiceException("Bad auth");
    }
}

我的安全配置

@Configuration
@EnableWebSecurity
public class MySecurityConfig extends WebSecurityConfigurerAdapter {
  @Override
  public void configure(HttpSecurity sec) throws Exception {
        http.sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
            .addFilterBefore(filter(), AnonymousAuthenticationFilter.class)
            .authorizeRequests()
            ...
            ;
  }

  @Bean
  public SimpleAuthFilter filter() {
    RequestMatcher match = new OrRequestMatcher(new AntPathRequestMatcher("/**"));
    return new SimpleAuthFilter(match);
  }
}

Swagger 配置

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
  @Bean
  public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.basePackage("MyProject"))
            .paths(PathSelectors.any())
            .build()
            .enableUrlTemplating(true)
            .securitySchemes(Collections.singleton(new ApiKey("mykey", "Authorization", "header")))
            .securityContexts(Collections.singleton(context()));
  }
  
  public SecurityContext context() {
    return SecurityContext.builder()
             .securityReferences(securityReferences())
             .forPaths(PathSelectors.any())
             .build();
  }

  public List<SecurityReference> securityReferences() {
    return Collections.singletonList(new SecurityReference("mykey", new AuthorizationScope[]{new AuthorizationScope("global", "accessEverything")}));
  }
}

我在想这个问题可能与需要通过扩展 WebMvcConfigurationSupport 来配置资源处理程序有关,但是每次我尝试招摇页面都有问题。或者我可能需要以某种方式排除具有多个 antMatcher() 的那些 URI 路径?

我尝试使用不同的 swagger 依赖项,在 HttpSecurity 配置、资源处理程序、视图控制器中移动过滤器位置,但对我没有任何帮助。

我正在使用 spring-boot-starter-web、spring-boot-starter-security 2.3.4 和 spring-boot-starter-validation 2.4.0

【问题讨论】:

    标签: java spring http swagger


    【解决方案1】:

    我想出了一个部分答案,即用于在 MySecurityConfig 中创建过滤器的 RequestMatcher 应该只针对端点而不是/**,这将导致过滤器即使在访问 swagger 页面时也会实际尝试身份验证.

    使用 AntPathRequestMatcher 的问题是,如果我的端点没有前缀模式,例如,

    /endpoint1

    /endpoint2

    【讨论】:

      猜你喜欢
      • 2017-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-16
      • 2020-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多