Skip to content

Commit a7bbd59

Browse files
committed
feat: 优化上下文传递
1 parent ec5b489 commit a7bbd59

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/AuthenticationHolder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ private static Optional<Authentication> get(Function<AuthenticationSupplier, Opt
7878
* @return 当前登录的用户权限信息
7979
*/
8080
public static Optional<Authentication> get() {
81-
Authentication current = CURRENT.get();
81+
Authentication current = CURRENT.getIfExists();
8282
if (current != null) {
8383
return Optional.of(current);
8484
}

hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/ReactiveAuthenticationHolder.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ public synchronized void merge(Authentication auth) {
104104
}
105105

106106
Authentication get() {
107-
return auth;
107+
return auth == null
108+
? AuthenticationHolder.get().orElse(null)
109+
: auth;
108110
}
109111
}
110112

hsweb-authorization/hsweb-authorization-api/src/test/java/org/hswebframework/web/authorization/context/AuthenticationThreadLocalAccessorTest.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,27 @@
1212

1313
class AuthenticationThreadLocalAccessorTest {
1414

15+
static {
16+
Hooks.enableAutomaticContextPropagation();
17+
}
1518

1619
@Test
1720
void testReadFromReactive() {
1821

19-
Hooks.enableAutomaticContextPropagation();
22+
Authentication auth = new SimpleAuthentication();
23+
24+
Authentication auth2 = AuthenticationHolder.executeWith(
25+
auth,
26+
() -> Authentication
27+
.currentReactive()
28+
.subscribeOn(Schedulers.boundedElastic())
29+
.block());
30+
31+
assertEquals(auth,auth2);
32+
}
33+
34+
@Test
35+
void testReadInReactive() {
2036

2137
Authentication auth = new SimpleAuthentication();
2238

@@ -28,7 +44,6 @@ void testReadFromReactive() {
2844
return Authentication.current().orElse(null);
2945
})
3046
.subscribeOn(Schedulers.boundedElastic())
31-
.contextWrite(c->c)
3247
.block());
3348

3449
assertEquals(auth, auth2);

0 commit comments

Comments
 (0)