mirror of
https://github.com/hs-web/hsweb-framework.git
synced 2026-09-03 06:35:25 +08:00
fix(认证): 优先使用响应式上下文认证 (#365)
* fix(认证): 优先使用响应式上下文认证 * refactor(认证): 简化上下文认证读取
This commit is contained in:
@@ -56,6 +56,11 @@ public final class ReactiveAuthenticationHolder {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录的用户权限信息。
|
||||
*
|
||||
* <p>调用链显式写入的认证快照优先于 Supplier,供网关或上游过滤器在不改写全局
|
||||
* Supplier 的情况下收敛本次请求的授权范围。</p>
|
||||
*
|
||||
* @return 当前登录的用户权限信息
|
||||
*/
|
||||
public static Mono<Authentication> get() {
|
||||
@@ -64,6 +69,10 @@ public final class ReactiveAuthenticationHolder {
|
||||
if (Boolean.TRUE.equals(ctx.getOrDefault(IGNORE_AUTH_KEY, false))) {
|
||||
return Mono.empty();
|
||||
}
|
||||
Authentication authentication = ctx.getOrDefault(Authentication.class, null);
|
||||
if (authentication != null) {
|
||||
return Mono.just(authentication);
|
||||
}
|
||||
return get(ReactiveAuthenticationSupplier::get);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -143,4 +143,18 @@ public class AuthenticationTests {
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPreferAuthenticationFromReactiveContext() {
|
||||
Authentication authentication = builder
|
||||
.user("{\"id\":\"scoped-user\",\"username\":\"scoped-user\"}")
|
||||
.build();
|
||||
|
||||
Authentication
|
||||
.currentReactive()
|
||||
.contextWrite(Context.of(Authentication.class, authentication))
|
||||
.as(StepVerifier::create)
|
||||
.assertNext(actual -> assertSame(authentication, actual))
|
||||
.verifyComplete();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user