Skip to content

Commit 2dbb9c7

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

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

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

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

106106
Authentication get() {
107-
return auth == null
108-
? AuthenticationHolder.get().orElse(null)
109-
: auth;
107+
return auth;
110108
}
111109
}
112110

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@
33
import io.micrometer.context.ThreadLocalAccessor;
44
import org.hswebframework.web.authorization.Authentication;
55
import org.hswebframework.web.authorization.AuthenticationHolder;
6+
import org.hswebframework.web.authorization.ReactiveAuthenticationHolder;
67

78
import javax.annotation.Nonnull;
89

9-
public class AuthenticationThreadLocalAccessor implements ThreadLocalAccessor<Authentication> {
10+
public class AuthenticationThreadLocalAccessor
11+
implements ThreadLocalAccessor<Authentication> {
1012

1113
static final String KEY = "cp.hs.auth";
1214

15+
static {
16+
ReactiveAuthenticationHolder.addSupplier(
17+
new ThreadLocalReactiveAuthenticationSupplier()
18+
);
19+
}
20+
1321
@Override
1422
@Nonnull
1523
public Object key() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.hswebframework.web.authorization.context;
2+
3+
import org.hswebframework.web.authorization.Authentication;
4+
import org.hswebframework.web.authorization.AuthenticationHolder;
5+
import org.hswebframework.web.authorization.ReactiveAuthenticationSupplier;
6+
import reactor.core.publisher.Mono;
7+
8+
class ThreadLocalReactiveAuthenticationSupplier implements ReactiveAuthenticationSupplier {
9+
@Override
10+
public Mono<Authentication> get(String userId) {
11+
return Mono.empty();
12+
}
13+
14+
@Override
15+
public Mono<Authentication> get() {
16+
return Mono.justOrEmpty(AuthenticationHolder.get());
17+
}
18+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ void testReadFromReactive() {
2626
() -> Authentication
2727
.currentReactive()
2828
.subscribeOn(Schedulers.boundedElastic())
29+
.contextCapture()
2930
.block());
3031

31-
assertEquals(auth,auth2);
32+
assertEquals(auth, auth2);
3233
}
3334

3435
@Test

0 commit comments

Comments
 (0)