浏览代码

增加UserPasswordModifiedEvent,用于在修改密码后执行一些操作

zhouhao 7 年之前
父节点
当前提交
a5c717a7c6

+ 0 - 1
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-api/src/main/java/org/hswebframework/web/service/authorization/PasswordStrengthValidator.java

@@ -3,7 +3,6 @@ package org.hswebframework.web.service.authorization;
 import org.hswebframework.web.service.Validator;
 import org.hswebframework.web.service.Validator;
 
 
 /**
 /**
- * TODO 完成注释
  *
  *
  * @author zhouhao
  * @author zhouhao
  */
  */

+ 18 - 0
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-api/src/main/java/org/hswebframework/web/service/authorization/events/UserPasswordModifiedEvent.java

@@ -0,0 +1,18 @@
+package org.hswebframework.web.service.authorization.events;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+/**
+ * 用户密码发生修改时事件
+ *
+ * @author zhouhao
+ * @see org.springframework.context.event.EventListener
+ * @see org.springframework.context.ApplicationEventPublisher
+ * @since 3.0
+ */
+@AllArgsConstructor
+@Getter
+public class UserPasswordModifiedEvent {
+    private String userId;
+}

+ 13 - 1
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/SimpleUserService.java

@@ -15,12 +15,14 @@ import org.hswebframework.web.service.AbstractService;
 import org.hswebframework.web.service.DefaultDSLQueryService;
 import org.hswebframework.web.service.DefaultDSLQueryService;
 import org.hswebframework.web.service.DefaultDSLUpdateService;
 import org.hswebframework.web.service.DefaultDSLUpdateService;
 import org.hswebframework.web.service.authorization.*;
 import org.hswebframework.web.service.authorization.*;
+import org.hswebframework.web.service.authorization.events.UserPasswordModifiedEvent;
 import org.hswebframework.web.validate.ValidationException;
 import org.hswebframework.web.validate.ValidationException;
 import org.hswebframework.utils.ListUtils;
 import org.hswebframework.utils.ListUtils;
 import org.hswebframework.web.validator.group.CreateGroup;
 import org.hswebframework.web.validator.group.CreateGroup;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.cache.annotation.CacheEvict;
 import org.springframework.cache.annotation.CacheEvict;
 import org.springframework.cache.annotation.Caching;
 import org.springframework.cache.annotation.Caching;
+import org.springframework.context.ApplicationEventPublisher;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.Assert;
 import org.springframework.util.Assert;
@@ -62,6 +64,9 @@ public class SimpleUserService extends AbstractService<UserEntity, String>
     @Autowired
     @Autowired
     private RoleDao roleDao;
     private RoleDao roleDao;
 
 
+    @Autowired
+    private ApplicationEventPublisher publisher;
+
     @Override
     @Override
     public String encodePassword(String password, String salt) {
     public String encodePassword(String password, String salt) {
         return passwordEncoder.encode(password, salt);
         return passwordEncoder.encode(password, salt);
@@ -177,10 +182,11 @@ public class SimpleUserService extends AbstractService<UserEntity, String>
         //修改密码
         //修改密码
         if (StringUtils.hasLength(userEntity.getPassword())) {
         if (StringUtils.hasLength(userEntity.getPassword())) {
             //密码强度验证
             //密码强度验证
-            tryValidateProperty(usernameValidator, UserEntity.password, userEntity.getPassword());
+            tryValidateProperty(passwordStrengthValidator, UserEntity.password, userEntity.getPassword());
             //密码MD5
             //密码MD5
             userEntity.setPassword(encodePassword(userEntity.getPassword(), oldUser.getSalt()));
             userEntity.setPassword(encodePassword(userEntity.getPassword(), oldUser.getSalt()));
             updateProperties.add(UserEntity.password);
             updateProperties.add(UserEntity.password);
+
         }
         }
         //修改数据
         //修改数据
         DefaultDSLUpdateService.createUpdate(getDao(), userEntity)
         DefaultDSLUpdateService.createUpdate(getDao(), userEntity)
@@ -194,6 +200,9 @@ public class SimpleUserService extends AbstractService<UserEntity, String>
             //同步角色信息
             //同步角色信息
             trySyncUserRole(userEntity.getId(), bindRoleUserEntity.getRoles());
             trySyncUserRole(userEntity.getId(), bindRoleUserEntity.getRoles());
         }
         }
+        if (updateProperties.contains(UserEntity.password)) {
+            publisher.publishEvent(new UserPasswordModifiedEvent(userId));
+        }
     }
     }
 
 
     @Override
     @Override
@@ -220,11 +229,14 @@ public class SimpleUserService extends AbstractService<UserEntity, String>
         if (!userEntity.getPassword().equals(oldPassword)) {
         if (!userEntity.getPassword().equals(oldPassword)) {
             throw new ValidationException("{old_password_error}", "password");
             throw new ValidationException("{old_password_error}", "password");
         }
         }
+        tryValidateProperty(passwordStrengthValidator, UserEntity.password, newPassword);
+
         newPassword = encodePassword(newPassword, userEntity.getSalt());
         newPassword = encodePassword(newPassword, userEntity.getSalt());
         DefaultDSLUpdateService.createUpdate(getDao())
         DefaultDSLUpdateService.createUpdate(getDao())
                 .set(UserEntity.password, newPassword)
                 .set(UserEntity.password, newPassword)
                 .where(GenericEntity.id, userId)
                 .where(GenericEntity.id, userId)
                 .exec();
                 .exec();
+        publisher.publishEvent(new UserPasswordModifiedEvent(userId));
     }
     }