Kaynağa Gözat

增加passwordEncoder

zhouhao 8 yıl önce
ebeveyn
işleme
b0fb6620ca

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

@@ -0,0 +1,26 @@
+/*
+ *  Copyright 2016 http://www.hswebframework.org
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *
+ */
+
+package org.hswebframework.web.service.authorization;
+
+/**
+ * @author zhouhao
+ */
+public interface PasswordEncoder {
+    String encode(String password, String salt);
+}

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

@@ -31,7 +31,5 @@ public interface UserService extends
 
     String encodePassword(String password, String salt);
 
-    void updateLoginInfo(String userId, String ip, Long loginTime);
-
     void updatePassword(String userId, String oldPassword, String newPassword);
 }

+ 18 - 22
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

@@ -10,10 +10,7 @@ import org.hswebframework.web.id.IDGenerator;
 import org.hswebframework.web.service.AbstractService;
 import org.hswebframework.web.service.DefaultDSLQueryService;
 import org.hswebframework.web.service.DefaultDSLUpdateService;
-import org.hswebframework.web.service.authorization.DataAccessFactory;
-import org.hswebframework.web.service.authorization.PasswordStrengthValidator;
-import org.hswebframework.web.service.authorization.UserService;
-import org.hswebframework.web.service.authorization.UsernameValidator;
+import org.hswebframework.web.service.authorization.*;
 import org.hswebframework.web.validate.ValidationException;
 import org.hswebframwork.utils.ListUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -47,6 +44,9 @@ public class SimpleUserService extends AbstractService<UserEntity, String>
     @Autowired(required = false)
     private UsernameValidator usernameValidator;
 
+    @Autowired(required = false)
+    private PasswordEncoder passwordEncoder = (password, salt) -> DigestUtils.md5Hex(String.format("hsweb.%s.framework.%s", password, salt));
+
     @Autowired
     private UserDao userDao;
 
@@ -79,24 +79,14 @@ public class SimpleUserService extends AbstractService<UserEntity, String>
 
     @Override
     public String encodePassword(String password, String salt) {
-        return DigestUtils.md5Hex(String.format("hsweb.%s.framework.%s", password, salt));
-    }
-
-    @Override
-    public void updateLoginInfo(String userId, String ip, Long loginTime) {
-        Assert.notNull(userId, "userId:{not_be_null}");
-        Assert.notNull(ip, "ip:{not_be_null}");
-        Assert.notNull(loginTime, "loginTime:{not_be_null}");
-        DefaultDSLUpdateService.createUpdate(getDao())
-                .set("lastLoginIp", ip)
-                .set("lastLoginTime", loginTime)
-                .where(GenericEntity.id, userId)
-                .exec();
+        return passwordEncoder.encode(password, salt);
     }
 
     @Override
     @Transactional(readOnly = true)
     public UserEntity selectByUsername(String username) {
+        tryValidateProperty(StringUtils.hasLength(username), UserEntity.username, "id:{not_be_null}");
+
         Assert.notNull(username, "username:{not_be_null}");
         return createQuery().where("username", username).single();
     }
@@ -104,19 +94,25 @@ public class SimpleUserService extends AbstractService<UserEntity, String>
     @Override
     @Transactional(readOnly = true)
     public UserEntity selectByPk(String id) {
-        Assert.notNull(id, "id:{not_be_null}");
-        return createQuery().where(GenericEntity.id, id).single();
+        tryValidateProperty(StringUtils.hasLength(id), UserEntity.id, "id:{not_be_null}");
+        return createQuery().where(UserEntity.id, id).single();
+    }
+
+    @Override
+    public List<UserEntity> selectByPk(List<String> id) {
+        tryValidateProperty(id != null && !id.isEmpty(), UserEntity.id, "id:{not_be_null}");
+        return createQuery().where().in(UserEntity.id, id).listNoPaging();
     }
 
     @Override
     @CacheEvict(value = USER_CACHE_NAME, key = "#userEntity.id")
     public String insert(UserEntity userEntity) {
         //判断用户是否已经存在
-        tryValidateProperty(null == selectByUsername(userEntity.getUsername()), "username", "{username_exists}");
+        tryValidateProperty(null == selectByUsername(userEntity.getUsername()), UserEntity.username, "{username_exists}");
         //用户名合法性验证
-        tryValidateProperty(usernameValidator, "username", userEntity.getUsername());
+        tryValidateProperty(usernameValidator, UserEntity.username, userEntity.getUsername());
         //密码强度验证
-        tryValidateProperty(passwordStrengthValidator, "password", userEntity.getPassword());
+        tryValidateProperty(passwordStrengthValidator, UserEntity.password, userEntity.getPassword());
         userEntity.setCreateTime(System.currentTimeMillis());
         userEntity.setId(IDGenerator.MD5.generate());
         userEntity.setSalt(IDGenerator.RANDOM.generate());