Skip to content

Commit 30deee7

Browse files
committed
refactor: 认证信息支持忽略
1 parent 2eb26c4 commit 30deee7

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
package org.hswebframework.web.authorization;
2020

2121
import com.google.common.collect.Lists;
22-
import org.hswebframework.web.authorization.simple.SimpleAuthentication;
2322
import reactor.core.publisher.Flux;
2423
import reactor.core.publisher.Mono;
24+
import reactor.util.context.Context;
2525

2626
import java.util.List;
2727
import java.util.concurrent.CopyOnWriteArrayList;
@@ -45,18 +45,27 @@
4545
public final class ReactiveAuthenticationHolder {
4646
private static final List<ReactiveAuthenticationSupplier> suppliers = new CopyOnWriteArrayList<>();
4747

48+
public static final String IGNORE_AUTH_KEY = ".auth.ignore";
49+
50+
static final Context IGNORE_AUTH_CONTEXT_Y = Context.of(IGNORE_AUTH_KEY, true);
51+
static final Context IGNORE_AUTH_CONTEXT_N = Context.of(IGNORE_AUTH_KEY, false);
52+
4853
private static Mono<Authentication> get(Function<ReactiveAuthenticationSupplier, Mono<Authentication>> function) {
4954
return AuthenticationUtils
50-
.merge(Flux.merge(Lists.transform(suppliers, function::apply)))
51-
;
55+
.merge(Flux.merge(Lists.transform(suppliers, function::apply)));
5256
}
5357

5458
/**
5559
* @return 当前登录的用户权限信息
5660
*/
5761
public static Mono<Authentication> get() {
5862

59-
return get(ReactiveAuthenticationSupplier::get);
63+
return Mono.deferContextual(ctx -> {
64+
if (Boolean.TRUE.equals(ctx.getOrDefault(IGNORE_AUTH_KEY, false))) {
65+
return Mono.empty();
66+
}
67+
return get(ReactiveAuthenticationSupplier::get);
68+
});
6069
}
6170

6271
/**
@@ -83,6 +92,15 @@ public static void setSupplier(ReactiveAuthenticationSupplier supplier) {
8392
suppliers.add(supplier);
8493
}
8594

95+
public static Context ignoreContext(boolean ignore) {
96+
return ignore ? IGNORE_AUTH_CONTEXT_Y : IGNORE_AUTH_CONTEXT_N;
97+
}
98+
99+
public static Function<Context, Context> ignoreIfAbsent(boolean ignore) {
100+
return ctx -> ctx.hasKey(IGNORE_AUTH_KEY)
101+
? ctx
102+
: ctx.put(IGNORE_AUTH_KEY, ignore);
103+
}
86104

87105

88106
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public void addDimension(Dimension dimension) {
151151
private transient volatile Map<String, Permission> permissionMapping;
152152
private transient volatile long accessCount;
153153

154-
private boolean fastPath() {
154+
protected boolean fastPath() {
155155
// 总共访问超过8次,则进行初始化缓存.
156156
if (ACCESS_COUNT_UPDATER.incrementAndGet(this) == 8) {
157157
if (permissionMapping == null) {

0 commit comments

Comments
 (0)