feat: 优化上下文传递

This commit is contained in:
zhouhao
2025-08-04 13:55:38 +08:00
parent a7bbd59d01
commit 2dbb9c7cc0
4 changed files with 30 additions and 5 deletions

View File

@@ -104,9 +104,7 @@ public final class ReactiveAuthenticationHolder {
}
Authentication get() {
return auth == null
? AuthenticationHolder.get().orElse(null)
: auth;
return auth;
}
}

View File

@@ -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() {

View File

@@ -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());
}
}

View File

@@ -26,9 +26,10 @@ class AuthenticationThreadLocalAccessorTest {
() -> Authentication
.currentReactive()
.subscribeOn(Schedulers.boundedElastic())
.contextCapture()
.block());
assertEquals(auth,auth2);
assertEquals(auth, auth2);
}
@Test