Browse Source

优化权限获取,避免无意义的转换

zhou-hao 3 years ago
parent
commit
10a05c516d

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

@@ -18,6 +18,7 @@
 
 package org.hswebframework.web.authorization;
 
+import org.apache.commons.collections.CollectionUtils;
 import org.hswebframework.web.authorization.simple.SimpleAuthentication;
 import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
@@ -48,12 +49,22 @@ public final class ReactiveAuthenticationHolder {
     private static Mono<Authentication> get(Function<ReactiveAuthenticationSupplier, Mono<Authentication>> function) {
 
         return Flux
-                .concat(suppliers
-                                .stream()
-                                .map(function)
-                                .collect(Collectors.toList()))
-                .reduceWith(SimpleAuthentication::new, Authentication::merge)
-                .filter(a -> a.getUser() != null);
+                .merge(suppliers
+                               .stream()
+                               .map(function)
+                               .collect(Collectors.toList()))
+                .collectList()
+                .filter(CollectionUtils::isNotEmpty)
+                .map(all -> {
+                    if (all.size() == 1) {
+                        return all.get(0);
+                    }
+                    SimpleAuthentication authentication = new SimpleAuthentication();
+                    for (Authentication auth : all) {
+                        authentication.merge(auth);
+                    }
+                    return authentication;
+                });
     }
 
     /**

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

@@ -1,6 +1,7 @@
 package org.hswebframework.web.authorization.simple;
 
 import lombok.AllArgsConstructor;
+import org.apache.commons.collections.CollectionUtils;
 import org.hswebframework.web.authorization.*;
 import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
@@ -12,32 +13,41 @@ import java.util.stream.Collectors;
 @AllArgsConstructor
 public class CompositeReactiveAuthenticationManager implements ReactiveAuthenticationManager {
 
-    private List<ReactiveAuthenticationManagerProvider> providers;
+    private final List<ReactiveAuthenticationManagerProvider> providers;
 
     @Override
     public Mono<Authentication> authenticate(Mono<AuthenticationRequest> request) {
-        return Flux.concat(providers.stream()
-                .map(manager -> manager
-                        .authenticate(request)
-                        .onErrorResume((err) -> {
-                            return Mono.empty();
-                        })).collect(Collectors.toList()))
-                .take(1)
-                .next();
+        return Flux.concat(providers
+                                   .stream()
+                                   .map(manager -> manager
+                                           .authenticate(request)
+                                           .onErrorResume((err) -> Mono.empty()))
+                                   .collect(Collectors.toList()))
+                   .take(1)
+                   .next();
     }
 
     @Override
     public Mono<Authentication> getByUserId(String userId) {
         return Flux
-                .fromStream(providers.stream()
-                        .map(manager -> manager
-                                .getByUserId(userId)
-                                .onErrorResume((err) -> {
-                                    return Mono.empty();
-                                })
-                        ))
+                .fromStream(providers
+                                    .stream()
+                                    .map(manager -> manager
+                                            .getByUserId(userId)
+                                            .onErrorResume((err) -> Mono.empty())
+                                    ))
                 .flatMap(Function.identity())
-                .reduceWith(SimpleAuthentication::of, Authentication::merge)
-                .filter(a -> a.getUser() != null);
+                .collectList()
+                .filter(CollectionUtils::isNotEmpty)
+                .map(all -> {
+                    if (all.size() == 1) {
+                        return all.get(0);
+                    }
+                    SimpleAuthentication authentication = new SimpleAuthentication();
+                    for (Authentication auth : all) {
+                        authentication.merge(auth);
+                    }
+                    return authentication;
+                });
     }
 }