Skip to content

Commit 2eb26c4

Browse files
committed
refactor: 优化权限合并逻辑
1 parent f87356c commit 2eb26c4

File tree

10 files changed

+1067
-92
lines changed

10 files changed

+1067
-92
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,8 @@ default boolean hasPermission(String permissionId, String... actions) {
204204

205205
default boolean hasPermission(String permissionId, Collection<String> actions) {
206206
for (Permission permission : getPermissions()) {
207-
if (Objects.equals(permission.getId(), "*")) {
208-
return true;
209-
}
210-
if (Objects.equals(permissionId, permission.getId())) {
207+
if (Objects.equals(permission.getId(), "*") ||
208+
Objects.equals(permissionId, permission.getId())) {
211209
return actions.isEmpty()
212210
|| permission.getActions().containsAll(actions)
213211
|| permission.getActions().contains("*");

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ private static Optional<Authentication> get(Function<AuthenticationSupplier, Opt
6565
if (size == 1) {
6666
return function.apply(suppliers.get(0));
6767
}
68-
ReactiveAuthenticationHolder.AuthenticationMerging merging
69-
= new ReactiveAuthenticationHolder.AuthenticationMerging();
68+
AuthenticationUtils.AuthenticationMerging merging
69+
= new AuthenticationUtils.AuthenticationMerging();
7070
for (AuthenticationSupplier supplier : suppliers) {
7171
function.apply(supplier).ifPresent(merging::merge);
7272
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,48 @@
11
package org.hswebframework.web.authorization;
22

3+
import org.hswebframework.web.authorization.simple.SimpleAuthentication;
34
import org.springframework.util.ObjectUtils;
45
import org.springframework.util.StringUtils;
6+
import reactor.core.publisher.Flux;
7+
import reactor.core.publisher.Mono;
58

69
/**
710
* @author zhouhao
811
* @since 3.0
912
*/
1013
public class AuthenticationUtils {
1114

15+
16+
public static Mono<Authentication> merge(Flux<Authentication> authenticationFlux){
17+
return authenticationFlux
18+
.collect(AuthenticationMerging::new, AuthenticationMerging::merge)
19+
.mapNotNull(AuthenticationMerging::get);
20+
}
21+
22+
static class AuthenticationMerging {
23+
24+
private Authentication auth;
25+
private int count;
26+
27+
public synchronized void merge(Authentication auth) {
28+
if (this.auth == null || this.auth == auth) {
29+
this.auth = auth;
30+
} else {
31+
if (count++ == 0) {
32+
SimpleAuthentication newAuth = new SimpleAuthentication();
33+
newAuth.merge(this.auth);
34+
this.auth = newAuth;
35+
}
36+
this.auth.merge(auth);
37+
}
38+
}
39+
40+
Authentication get() {
41+
return auth;
42+
}
43+
}
44+
45+
1246
public static AuthenticationPredicate createPredicate(String expression) {
1347
if (ObjectUtils.isEmpty(expression)) {
1448
return (authentication -> false);

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

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,9 @@ public final class ReactiveAuthenticationHolder {
4646
private static final List<ReactiveAuthenticationSupplier> suppliers = new CopyOnWriteArrayList<>();
4747

4848
private static Mono<Authentication> get(Function<ReactiveAuthenticationSupplier, Mono<Authentication>> function) {
49-
return Flux
50-
.merge(Lists.transform(suppliers, function::apply))
51-
.collect(AuthenticationMerging::new, AuthenticationMerging::merge)
52-
.mapNotNull(AuthenticationMerging::get);
49+
return AuthenticationUtils
50+
.merge(Flux.merge(Lists.transform(suppliers, function::apply)))
51+
;
5352
}
5453

5554
/**
@@ -85,27 +84,5 @@ public static void setSupplier(ReactiveAuthenticationSupplier supplier) {
8584
}
8685

8786

88-
static class AuthenticationMerging {
89-
90-
private Authentication auth;
91-
private int count;
92-
93-
public synchronized void merge(Authentication auth) {
94-
if (this.auth == null || this.auth == auth) {
95-
this.auth = auth;
96-
} else {
97-
if (count++ == 0) {
98-
SimpleAuthentication newAuth = new SimpleAuthentication();
99-
newAuth.merge(this.auth);
100-
this.auth = newAuth;
101-
}
102-
this.auth.merge(auth);
103-
}
104-
}
105-
106-
Authentication get() {
107-
return auth;
108-
}
109-
}
11087

11188
}

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

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ public class ResourcesDefinition {
2222

2323
private Phased phased = Phased.before;
2424

25-
public void clear(){
25+
public void clear() {
2626
resources.clear();
2727
}
28+
2829
public void addResource(ResourceDefinition resource, boolean merge) {
2930
ResourceDefinition definition = getResource(resource.getId()).orElse(null);
3031
if (definition != null) {
@@ -44,26 +45,26 @@ public void addResource(ResourceDefinition resource, boolean merge) {
4445

4546
public Optional<ResourceDefinition> getResource(String id) {
4647
return resources
47-
.stream()
48-
.filter(resource -> resource.getId().equals(id))
49-
.findAny();
48+
.stream()
49+
.filter(resource -> resource.getId().equals(id))
50+
.findAny();
5051
}
5152

5253
@JsonIgnore
5354
public List<ResourceDefinition> getDataAccessResources() {
5455
return resources
55-
.stream()
56-
.filter(ResourceDefinition::hasDataAccessAction)
57-
.collect(Collectors.toList());
56+
.stream()
57+
.filter(ResourceDefinition::hasDataAccessAction)
58+
.collect(Collectors.toList());
5859
}
5960

6061
public boolean hasPermission(Permission permission) {
6162
if (CollectionUtils.isEmpty(resources)) {
6263
return true;
6364
}
6465
return getResource(permission.getId())
65-
.filter(resource -> resource.hasAction(permission.getActions()))
66-
.isPresent();
66+
.filter(resource -> resource.hasAction(permission.getActions()))
67+
.isPresent();
6768
}
6869

6970
public boolean isEmpty() {
@@ -72,18 +73,27 @@ public boolean isEmpty() {
7273

7374
public boolean hasPermission(Authentication authentication) {
7475

75-
if (CollectionUtils.isEmpty(resources)) {
76+
int size = resources.size();
77+
if (size == 0) {
7678
return true;
7779
}
80+
if (size == 1) {
81+
for (ResourceDefinition resource : resources) {
82+
if (authentication.hasPermission(resource.getId(), resource.getActionIds())) {
83+
return true;
84+
}
85+
}
86+
return false;
87+
}
7888

7989
if (logical == Logical.AND) {
8090
return resources
81-
.stream()
82-
.allMatch(resource -> authentication.hasPermission(resource.getId(), resource.getActionIds()));
91+
.stream()
92+
.allMatch(resource -> authentication.hasPermission(resource.getId(), resource.getActionIds()));
8393
}
8494

8595
return resources
86-
.stream()
87-
.anyMatch(resource -> authentication.hasPermission(resource.getId(), resource.getActionIds()));
96+
.stream()
97+
.anyMatch(resource -> authentication.hasPermission(resource.getId(), resource.getActionIds()));
8898
}
8999
}

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

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import lombok.AllArgsConstructor;
44
import lombok.extern.slf4j.Slf4j;
5-
import org.apache.commons.collections4.CollectionUtils;
65
import org.hswebframework.web.authorization.*;
76
import reactor.core.publisher.Flux;
87
import reactor.core.publisher.Mono;
@@ -19,43 +18,38 @@ public class CompositeReactiveAuthenticationManager implements ReactiveAuthentic
1918

2019
@Override
2120
public Mono<Authentication> authenticate(Mono<AuthenticationRequest> request) {
22-
return Flux.concat(providers
23-
.stream()
24-
.map(manager -> manager
25-
.authenticate(request)
26-
.onErrorResume((err) -> {
27-
log.warn("get user authenticate error", err);
28-
return Mono.empty();
29-
}))
30-
.collect(Collectors.toList()))
31-
.take(1)
32-
.next();
21+
return Flux
22+
.concat(
23+
providers
24+
.stream()
25+
.map(manager -> manager
26+
.authenticate(request)
27+
.onErrorResume((err) -> {
28+
log.warn("get user authenticate error", err);
29+
return Mono.empty();
30+
}))
31+
.collect(Collectors.toList()))
32+
.take(1)
33+
.next();
3334
}
3435

3536
@Override
3637
public Mono<Authentication> getByUserId(String userId) {
38+
if (providers.size() == 1) {
39+
return providers.get(0).getByUserId(userId);
40+
}
3741
return Flux
38-
.fromStream(providers
39-
.stream()
40-
.map(manager -> manager
41-
.getByUserId(userId)
42-
.onErrorResume((err) -> {
43-
log.warn("get user [{}] authentication error", userId, err);
44-
return Mono.empty();
45-
})
46-
))
47-
.flatMap(Function.identity())
48-
.collectList()
49-
.filter(CollectionUtils::isNotEmpty)
50-
.map(all -> {
51-
if (all.size() == 1) {
52-
return all.get(0);
53-
}
54-
SimpleAuthentication authentication = new SimpleAuthentication();
55-
for (Authentication auth : all) {
56-
authentication.merge(auth);
57-
}
58-
return authentication;
59-
});
42+
.fromStream(providers
43+
.stream()
44+
.map(manager -> manager
45+
.getByUserId(userId)
46+
.onErrorResume((err) -> {
47+
log.warn("get user [{}] authentication error", userId, err);
48+
return Mono.empty();
49+
})
50+
))
51+
.flatMap(Function.identity())
52+
.as(AuthenticationUtils::merge);
6053
}
54+
6155
}

0 commit comments

Comments
 (0)