Webflux安全性:同一匹配中多个角色

3

我在Spring Cloud Gateway中使用了Spring Security。云版本是Finchely.SR2,Spring Boot版本是2.0.x。

然后我像这样在一个匹配器中设置了两个角色:

            .pathMatchers("/apis/**").hasRole("TEST1")
            .pathMatchers("/apis/**").hasRole("TEST2")

但是,当我启动应用程序并进行测试时,只有TEST1角色可以访问。 TEST2角色会收到403禁止响应。

我知道在spring-boot-starter-web与spring-security中,它有类似的方法

             .antMatchers("/apis/**").hasAnyRole("TEST1", "TEST2")

Webflux Spring Security有没有像hasAnyRole(String...roles)这样的API可以使用?
3个回答

0

hasAnyRolehasAnyAuthority将在Spring Security 5.2.0的WebFlux中提供。
您现在可以在5.2.0.M3中尝试它们。
语法是相同的。

.pathMatchers("/apis/**").hasAnyRole("TEST1", "TEST2")

0

在Spring Security的基本API中没有可用的,但是我一直在使用以下代码来实现anyAuthority

public class HasAnyAuthority<T> implements ReactiveAuthorizationManager<T> {

    private final Collection<String> allowedAuthorities;

    public HasAnyAuthority(Collection<String> allowedAuthorities) {
        this.allowedAuthorities = allowedAuthorities;
    }

    @Override
    public Mono<AuthorizationDecision> check(final Mono<Authentication> authentication,
            T object) {
        return authentication.filter(Authentication::isAuthenticated)
                .flatMapIterable(Authentication::getAuthorities)
                .map(GrantedAuthority::getAuthority).any(allowedAuthorities::contains)
                .map(AuthorizationDecision::new)
                .defaultIfEmpty(new AuthorizationDecision(false));
    }

使用方式如下:

.access(new HasAnyAuthority<>(allowedAuth.getAuthorities())


0
如果有帮助的话
.pathMatchers("/apis/**")
            .access((mono, context) -> mono.map(auth -> auth.getAuthorities().stream()
                .filter(e -> (e.getAuthority().contains(TEST1) || e.getAuthority().contains(TEST2)))
                .count() > 0)
                .map(AuthorizationDecision::new))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接