mirror of
https://github.com/hs-web/hsweb-framework.git
synced 2026-06-04 11:52:41 +08:00
feat: 优化上下文传递
This commit is contained in:
@@ -104,9 +104,7 @@ public final class ReactiveAuthenticationHolder {
|
||||
}
|
||||
|
||||
Authentication get() {
|
||||
return auth == null
|
||||
? AuthenticationHolder.get().orElse(null)
|
||||
: auth;
|
||||
return auth;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,13 +3,21 @@ package org.hswebframework.web.authorization.context;
|
||||
import io.micrometer.context.ThreadLocalAccessor;
|
||||
import org.hswebframework.web.authorization.Authentication;
|
||||
import org.hswebframework.web.authorization.AuthenticationHolder;
|
||||
import org.hswebframework.web.authorization.ReactiveAuthenticationHolder;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public class AuthenticationThreadLocalAccessor implements ThreadLocalAccessor<Authentication> {
|
||||
public class AuthenticationThreadLocalAccessor
|
||||
implements ThreadLocalAccessor<Authentication> {
|
||||
|
||||
static final String KEY = "cp.hs.auth";
|
||||
|
||||
static {
|
||||
ReactiveAuthenticationHolder.addSupplier(
|
||||
new ThreadLocalReactiveAuthenticationSupplier()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public Object key() {
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package org.hswebframework.web.authorization.context;
|
||||
|
||||
import org.hswebframework.web.authorization.Authentication;
|
||||
import org.hswebframework.web.authorization.AuthenticationHolder;
|
||||
import org.hswebframework.web.authorization.ReactiveAuthenticationSupplier;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
class ThreadLocalReactiveAuthenticationSupplier implements ReactiveAuthenticationSupplier {
|
||||
@Override
|
||||
public Mono<Authentication> get(String userId) {
|
||||
return Mono.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Authentication> get() {
|
||||
return Mono.justOrEmpty(AuthenticationHolder.get());
|
||||
}
|
||||
}
|
||||
@@ -26,9 +26,10 @@ class AuthenticationThreadLocalAccessorTest {
|
||||
() -> Authentication
|
||||
.currentReactive()
|
||||
.subscribeOn(Schedulers.boundedElastic())
|
||||
.contextCapture()
|
||||
.block());
|
||||
|
||||
assertEquals(auth,auth2);
|
||||
assertEquals(auth, auth2);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user