【问题标题】:Spring Webflux Security Allow All But One UrlSpring Webflux Security 允许除一个 URL 之外的所有 URL
【发布时间】:2018-11-20 01:40:06
【问题描述】:

我想允许在特定控制器中定义的所有 url,除了一个。

假设我的控制器公开了 3 个带有基本 url /users 的 url

  1. “/”列出所有用户
  2. "/{id}" 按 id 列出用户
  3. “/admin”获取用户的管理员级别详细信息。

我想允许除最后一个用户之外的任何用户访问 1 和 2 个 url。

我一直在做这样的事情

@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
    http.authorizeExchange()
        .pathMatchers("/users/**")
        .permitAll()
        .pathMatchers("/users/admin")
        .hasRole("ADMIN")
        .anyExchange()
        .authenticated()
        .and()
        .httpBasic()
        .and()
        .formLogin();
    return http.build();
}

但它不起作用。我也尝试过相反的方法,即

@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
    http.authorizeExchange()
        .pathMatchers("/users/admin")
        .hasRole("ADMIN")
        .anyExchange()
        .authenticated()
        .pathMatchers("/users/**")
        .permitAll()
        .and()
        .httpBasic()
        .and()
        .formLogin();
    return http.build();
}

这也不起作用,因为anyExchange() 已经注册,所以无法访问下一个pathMatcher

【问题讨论】:

    标签: java spring spring-security spring-webflux


    【解决方案1】:

    我找到了解决方案。 anyExchange()authenticated() 导致了这个问题。 anyExchange() 不允许进一步添加任何路径匹配器,authenticated() 正在使整个应用程序安全,导致每个 url 提示进行身份验证。

    删除这两个有效。

    @Bean
    public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
        http.authorizeExchange()
            .pathMatchers("/users/admin/**")
            .hasRole("ADMIN")
            .pathMatchers("/**").permitAll()
            .and().httpBasic();
        return http.build();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-22
      • 2017-11-06
      • 2018-01-03
      • 2019-08-10
      • 2018-05-25
      • 1970-01-01
      • 2021-08-15
      • 1970-01-01
      相关资源
      最近更新 更多