【问题标题】:Spring Security in WebfluxWebflux 中的 Spring 安全性
【发布时间】:2020-11-18 17:17:30
【问题描述】:

我一直在尝试使用我自己的自定义身份验证方法在 Web Flux 中启用 Spring Security。到目前为止一切顺利,但我无法使用 permitall 允许某些 URL 模式。

我尝试创建不同的 SecurityWebFilterChain bean,也尝试了完全不同的配置,但似乎没有什么对我有用。

这是我的 SecurityWebFilterChain

@Bean
  public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
    return http.csrf()
        .disable()
        .httpBasic()
        .disable()
        .formLogin()
        .disable()
        .logout()
        .disable()
        .authenticationManager(this.authenticationManager())
        .securityContextRepository(this.securityContextRepository())
        .authorizeExchange()
        .pathMatchers("**/signal/health").permitAll()
        .pathMatchers("**/order").permitAll()
        .and()
        .authorizeExchange()
        .anyExchange()
        .authenticated()
        .and()
        .build();
  }

我有一个内部健康检查系统,它会在我的应用程序启动后立即运行,所以我希望允许这样做。

此外,我还想允许其他一对或 URI,但上面的配置对我不起作用。

一切都是为了验证。

我在这里做错了什么?

【问题讨论】:

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


    【解决方案1】:

    我看到一个错误的 andauthorizeExchange 放在两者之间。试试这个:

    @Bean
      public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
        return http.csrf().disable()
            .httpBasic().disable()
            .formLogin().disable()
            .logout().disable()
            .authenticationManager(this.authenticationManager())
            .securityContextRepository(this.securityContextRepository())
            .authorizeExchange()
            .pathMatchers("**/signal/health","**/order").permitAll()
            .anyExchange()
            .authenticated()
            .and().build();
      }
    

    【讨论】:

    • 它仍然给我未经授权。此外,健康检查仍在覆盖的加载方法中进行。 ``` @Override public Mono load(ServerWebExchange serverWebExchange) { log.info(serverWebExchange.getRequest().getURI()); ```
    • 哪个覆盖加载方法?能否也分享一下 authenticationManager 和 securityContextRepository bean 的代码
    • ``` @Bean ReactiveAuthenticationManager authenticationManager() { return authentication -> { log.debug("Authentication:" + authentication.toString()); if (authentication instanceof CustomPreAuthenticationToken) { authentication.setAuthenticated(true); } return Mono.just(authentication); }; } ```
    • ``` public class CustomPreAuthenticationToken extends UsernamePasswordAuthenticationToken { public CustomPreAuthenticationToken(String key, Object principal, Collection extends GrantedAuthority> authority) { super(key, principal, authority); } } ```
    • securityContextRepository bean 有点太长,所以我分两部分发布。
    猜你喜欢
    • 2017-12-20
    • 1970-01-01
    • 2020-10-09
    • 2021-06-23
    • 2019-02-05
    • 2020-03-20
    • 2020-07-18
    • 2019-03-30
    • 2021-05-11
    相关资源
    最近更新 更多