【问题标题】:How to pass bypass Options request in spring webflux security?如何在spring webflux安全中通过绕过选项请求?
【发布时间】:2021-11-03 14:11:53
【问题描述】:

我创建了一个 spring webflux 安全类来启用自定义身份验证和授权过滤器的安全性。

  public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
    log.debug("Configuring tenant web security");

    return http
        .cors(Customizer.withDefaults())
        .csrf().disable()
        .authorizeExchange()
        .pathMatchers(HttpMethod.OPTIONS, "/**").permitAll()
        .pathMatchers(new String[]{"/api/demo"}).permitAll()
        .anyExchange().authenticated()
        .and()
        .addFilterAt(authenticationWebFilter(), SecurityWebFiltersOrder.AUTHENTICATION)
        .addFilterAt(authorizationWebFilter(), SecurityWebFiltersOrder.AUTHORIZATION)
        .build();
  }

这里的 Authenticaion 过滤器是:

   /**
   * Method to get the instance of {@link AuthenticationWebFilter}.
   *
   * @return {@link AuthenticationWebFilter} instance.
   */
  private AuthenticationWebFilter authenticationWebFilter() {
    AuthenticationWebFilter authenticationWebFilter = new AuthenticationWebFilter(
        customAuthenticationManager);
    authenticationWebFilter.setServerAuthenticationConverter(customAutenticationConverter);
    NegatedServerWebExchangeMatcher negateWhiteList = new NegatedServerWebExchangeMatcher(
        ServerWebExchangeMatchers.pathMatchers(new String[]{"/api/demo"}));
    authenticationWebFilter.setRequiresAuthenticationMatcher(negateWhiteList);
    return authenticationWebFilter;
  }

授权网页过滤器是:

 private AuthorizationWebFilter authorizationWebFilter() {
    return new AuthorizationWebFilter(customAuthorizationManager);
  }

Cors 配置是:-

  @Bean
  public CorsConfigurationSource corsConfigurationSource() {
    CorsConfiguration corsConfiguration = new CorsConfiguration();
    corsConfiguration.addAllowedOrigin("*");
    corsConfiguration.addAllowedHeader("*");
    corsConfiguration.addAllowedMethod(HttpMethod.OPTIONS);
    UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
    corsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
    return corsConfigurationSource;
  }

现在,当我发出 OPTIONS 请求时,它仍会转到我在上面在过滤器中注册的 customAuthenticationManager

我怎样才能绕过这个?

【问题讨论】:

  • 这是因为当您添加自定义过滤器时,所有赌注都已关闭。当您所做的只是编写所有自定义内容时,为什么还要使用安全框架? (我可能会添加一个不好的做法)
  • 需要自定义过滤器,因为对于每个请求,它们都是一个标头 x-auth-token,然后我将其用于我的身份验证服务器来验证令牌,然后仅通过身份验证。
  • 为什么不自定义框架为您提供的 Spring Security 中已经默认实现的过滤器?正如我所说,自定义安全性是不好的做法,当使用自定义过滤器时,您基本上选择退出框架及其所有功能。
  • 我自定义 AuthenticationWebFilter 只是给它我的自定义 AuthenticationManagerServerAuthenticationConverter
  • 不,您不是,您正在创建一个全新的 AuthenticationWebFilter,并在过滤器链中手动设置它。您没有自定义当前过滤器,您应该通过向当前过滤器拾取的上下文提供 @Bean 来实现这一点。

标签: spring-boot spring-security spring-webflux


【解决方案1】:

corsConfigurationSource bean 为我工作。我遇到的请求有问题

【讨论】:

  • 请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。
猜你喜欢
  • 2014-01-31
  • 2021-06-23
  • 2019-12-20
  • 2016-05-07
  • 2016-10-04
  • 2021-08-15
  • 2021-05-06
  • 2018-10-31
  • 1970-01-01
相关资源
最近更新 更多