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