Просмотр исходного кода

ClearUserAuthorizationCacheEvent 支持响应式

zhouhao 2 лет назад
Родитель
Сommit
1da33b79d2

+ 18 - 1
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-api/src/main/java/org/hswebframework/web/system/authorization/api/event/ClearUserAuthorizationCacheEvent.java

@@ -1,6 +1,9 @@
 package org.hswebframework.web.system.authorization.api.event;
 
 import lombok.Getter;
+import org.hswebframework.web.event.DefaultAsyncEvent;
+import org.springframework.context.ApplicationEventPublisher;
+import reactor.core.publisher.Mono;
 
 import java.util.Arrays;
 import java.util.Collection;
@@ -13,11 +16,13 @@ import java.util.Set;
  * @since 3.0.0-RC
  */
 @Getter
-public class ClearUserAuthorizationCacheEvent {
+public class ClearUserAuthorizationCacheEvent extends DefaultAsyncEvent {
     private Set<String> userId;
 
     private boolean all;
 
+    private boolean async;
+
     public static ClearUserAuthorizationCacheEvent of(Collection<String> collection) {
         ClearUserAuthorizationCacheEvent event = new ClearUserAuthorizationCacheEvent();
         if (collection == null || collection.isEmpty()) {
@@ -32,6 +37,18 @@ public class ClearUserAuthorizationCacheEvent {
         return ClearUserAuthorizationCacheEvent.of((String[]) null);
     }
 
+    //兼容async
+    public ClearUserAuthorizationCacheEvent useAsync() {
+        this.async = true;
+        return this;
+    }
+
+    @Override
+    public Mono<Void> publish(ApplicationEventPublisher eventPublisher) {
+        this.async = true;
+        return super.publish(eventPublisher);
+    }
+
     public static ClearUserAuthorizationCacheEvent of(String... userId) {
 
         return of(userId == null ? null : Arrays.asList(userId));

+ 1 - 2
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/java/org/hswebframework/web/system/authorization/defaults/service/DefaultAuthorizationSettingService.java

@@ -93,8 +93,7 @@ public class DefaultAuthorizationSettingService extends GenericReactiveCrudServi
                                 .justOrEmpty(typeProviderMapping.get(setting.getDimensionType()))
                                 .flatMapMany(provider -> provider.getUserIdByDimensionId(setting.getDimensionTarget()))))
                 .collectList()
-                .map(ClearUserAuthorizationCacheEvent::of)
-                .doOnNext(eventPublisher::publishEvent)
+                .flatMap(lst-> ClearUserAuthorizationCacheEvent.of(lst).publish(eventPublisher))
                 .then();
     }
 

+ 6 - 7
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/java/org/hswebframework/web/system/authorization/defaults/service/DefaultDimensionUserService.java

@@ -123,10 +123,11 @@ public class DefaultDimensionUserService extends GenericReactiveCrudService<Dime
                                 return dimensionIdGroup
                                         .map(DimensionUserEntity::getUserId)
                                         .collectList()
-                                        .flatMap(userIdList -> {
-                                            eventPublisher.publishEvent(ClearUserAuthorizationCacheEvent.of(userIdList));
-                                            return event.apply(type, dimensionId, userIdList).publish(eventPublisher);
-                                        });
+                                        .flatMap(userIdList -> ClearUserAuthorizationCacheEvent
+                                                .of(userIdList)
+                                                .publish(eventPublisher)
+                                                .then(event.apply(type, dimensionId, userIdList)
+                                                           .publish(eventPublisher)));
                             });
                 })
                 .thenMany(cache);
@@ -137,9 +138,7 @@ public class DefaultDimensionUserService extends GenericReactiveCrudService<Dime
                    .map(DimensionUserEntity::getUserId)
                    .distinct()
                    .collectList()
-                   .map(ClearUserAuthorizationCacheEvent::of)
-                   .doOnNext(eventPublisher::publishEvent)
-                   .then();
+                   .flatMap(list -> ClearUserAuthorizationCacheEvent.of(list).publish(eventPublisher));
     }
 
 }

+ 13 - 5
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/java/org/hswebframework/web/system/authorization/defaults/service/DefaultPermissionService.java

@@ -19,31 +19,39 @@ public class DefaultPermissionService extends GenericReactiveCrudService<Permiss
     @Override
     public Mono<SaveResult> save(Publisher<PermissionEntity> entityPublisher) {
         return super.save(entityPublisher)
-                .doOnSuccess(r -> eventPublisher.publishEvent(ClearUserAuthorizationCacheEvent.of()));
+                    .flatMap(e -> ClearUserAuthorizationCacheEvent.all().publish(eventPublisher).thenReturn(e));
     }
 
     @Override
     public Mono<Integer> updateById(String id, Mono<PermissionEntity> entityPublisher) {
         return super.updateById(id, entityPublisher)
-                .doOnSuccess(r -> eventPublisher.publishEvent(ClearUserAuthorizationCacheEvent.of()));
+                    .flatMap(e -> ClearUserAuthorizationCacheEvent.all().publish(eventPublisher).thenReturn(e));
     }
 
     @Override
     public Mono<Integer> deleteById(Publisher<String> idPublisher) {
         return super.deleteById(idPublisher)
-                .doOnSuccess(r -> eventPublisher.publishEvent(ClearUserAuthorizationCacheEvent.of()));
+                    .flatMap(e -> ClearUserAuthorizationCacheEvent.all().publish(eventPublisher).thenReturn(e));
     }
 
     @Override
     public ReactiveDelete createDelete() {
         return super.createDelete()
-                .onExecute((ignore,i) -> i.doOnSuccess(r -> eventPublisher.publishEvent(ClearUserAuthorizationCacheEvent.of())));
+                    .onExecute((ignore, i) -> i
+                            .flatMap(e -> ClearUserAuthorizationCacheEvent
+                                    .all()
+                                    .publish(eventPublisher)
+                                    .thenReturn(e)));
     }
 
     @Override
     public ReactiveUpdate<PermissionEntity> createUpdate() {
         return super.createUpdate()
-                .onExecute((ignore,i) -> i.doOnSuccess(r -> eventPublisher.publishEvent(ClearUserAuthorizationCacheEvent.of())));
+                    .onExecute((ignore, i) -> i
+                            .flatMap(e -> ClearUserAuthorizationCacheEvent
+                                    .all()
+                                    .publish(eventPublisher)
+                                    .thenReturn(e)));
     }
 
 

+ 27 - 16
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/java/org/hswebframework/web/system/authorization/defaults/service/DefaultReactiveAuthenticationManager.java

@@ -1,18 +1,17 @@
 package org.hswebframework.web.system.authorization.defaults.service;
 
 import lombok.extern.slf4j.Slf4j;
-import org.hswebframework.web.authorization.*;
-import org.hswebframework.web.authorization.exception.AccessDenyException;
+import org.hswebframework.web.authorization.Authentication;
+import org.hswebframework.web.authorization.AuthenticationRequest;
+import org.hswebframework.web.authorization.ReactiveAuthenticationInitializeService;
+import org.hswebframework.web.authorization.ReactiveAuthenticationManagerProvider;
 import org.hswebframework.web.authorization.simple.PlainTextUsernamePasswordAuthenticationRequest;
 import org.hswebframework.web.cache.ReactiveCacheManager;
 import org.hswebframework.web.system.authorization.api.entity.UserEntity;
 import org.hswebframework.web.system.authorization.api.event.ClearUserAuthorizationCacheEvent;
 import org.hswebframework.web.system.authorization.api.service.reactive.ReactiveUserService;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.cache.CacheManager;
-import org.springframework.cache.support.SimpleValueWrapper;
 import org.springframework.context.event.EventListener;
-import org.springframework.core.Ordered;
 import org.springframework.core.annotation.Order;
 import reactor.core.publisher.Mono;
 
@@ -32,18 +31,25 @@ public class DefaultReactiveAuthenticationManager implements ReactiveAuthenticat
     @EventListener
     public void handleClearAuthCache(ClearUserAuthorizationCacheEvent event) {
         if (cacheManager != null) {
+            Mono<Void> operator;
             if (event.isAll()) {
-                cacheManager.getCache("user-auth")
+                operator = cacheManager
+                        .getCache("user-auth")
                         .clear()
                         .doOnSuccess(nil -> log.info("clear all user authentication cache success"))
-                        .doOnError(err -> log.error(err.getMessage(), err))
-                        .subscribe();
+                        .doOnError(err -> log.error(err.getMessage(), err));
             } else {
-                cacheManager.getCache("user-auth")
+                operator = cacheManager
+                        .getCache("user-auth")
                         .evictAll(event.getUserId())
                         .doOnError(err -> log.error(err.getMessage(), err))
-                        .doOnSuccess(__ -> log.info("clear user {} authentication cache success", event.getUserId()))
-                        .subscribe();
+                        .doOnSuccess(__ -> log.info("clear user {} authentication cache success", event.getUserId()));
+            }
+            if (event.isAsync()) {
+                event.async(operator);
+            } else {
+                log.warn("please use async for ClearUserAuthorizationCacheEvent");
+                operator.subscribe();
             }
         }
     }
@@ -62,11 +68,16 @@ public class DefaultReactiveAuthenticationManager implements ReactiveAuthenticat
 
     @Override
     public Mono<Authentication> getByUserId(String userId) {
+        if (userId == null) {
+            return Mono.empty();
+        }
+        if (cacheManager == null) {
+            return initializeService.initUserAuthorization(userId);
+        }
 
-        return Mono.justOrEmpty(userId)
-                .flatMap(_id -> Mono.justOrEmpty(cacheManager)
-                        .map(cm -> cacheManager.<Authentication>getCache("user-auth"))
-                        .flatMap(cache -> cache.mono(userId).onCacheMissResume(() -> initializeService.initUserAuthorization(userId)))
-                        .cast(Authentication.class));
+        return cacheManager
+                .<Authentication>getCache("user-auth")
+                .mono(userId)
+                .onCacheMissResume(() -> initializeService.initUserAuthorization(userId));
     }
 }

+ 4 - 3
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/java/org/hswebframework/web/system/authorization/defaults/service/DefaultReactiveUserService.java

@@ -110,9 +110,10 @@ public class DefaultReactiveUserService extends GenericReactiveCrudService<UserE
                             .execute()
                             .flatMap(__ -> new UserModifiedEvent(userEntity, passwordChanged).publish(eventPublisher))
                             .thenReturn(userEntity)
-                            .doOnNext(e -> {
-                                eventPublisher.publishEvent(ClearUserAuthorizationCacheEvent.of(e.getId()));
-                            });
+                            .flatMap(e -> ClearUserAuthorizationCacheEvent
+                                    .of(e.getId())
+                                    .publish(eventPublisher)
+                                    .thenReturn(e));
                 });
 
     }