gpt4 book ai didi

java - 如何在 Spring WebFlux Security(Reactive Spring Security) 配置中将多个用户角色添加到单个 pathMatcher/Route?

转载 作者:行者123 更新时间:2023-12-01 19:35:50 25 4
gpt4 key购买 nike

我有一条路由需要对多个用户进行身份验证。对spring cloud gateway服务进行集成测试,以测试所有路由安全性是否按预期工作。如何向单个路径匹配器/路由添加超过 1 个用户角色?

使用 Spring Boot 2.1.6、Spring Cloud Finchely.SR2、Spring Cloud Gateway、Spring WebFlux Security(响应式 Spring Security)

@EnableWebFluxSecurity
public class SecurityConfig {

@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {

http.csrf().disable()
.formLogin().disable()
.logout().disable()
.authorizeExchange()
.pathMatchers(prefix + "/publish/**")
.hasRole("XYZ_ROLE") //Here i want to add more than one user role
.anyExchange()
.authenticated().and().httpBasic();
}
}

最佳答案

编辑:

查看 Spring Security 源代码和 github 问题后,我发现 hasAnyRolehasAnyAuthorityimplemented适用于 spring security for webflux,计划在 Spring security v5.2.0 中发布

截至撰写本文时,当前稳定版本是 5.1.6,但 5.2.0 处于里程碑 4 中,因此应该很快就会发布。如果需要,您可以使用5.2.0的快照版本。

如果不使用快照,当前唯一的其他选项是实现您自己的自定义 ReactiveAuthorizationManager 并使用 ServerHttpSecurity.AuthorizeExchangeSpec#access功能。

旧答案仅适用于标准 Spring 安全性,不适用于 WEBFLUX:

您可以尝试hasAnyRole(String... roles)

关于java - 如何在 Spring WebFlux Security(Reactive Spring Security) 配置中将多个用户角色添加到单个 pathMatcher/Route?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57633276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com