Преглед изворни кода

add selectByUserNameAndPassword for userService

zhou-hao пре 7 година
родитељ
комит
9b8c9d283d

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

@@ -29,6 +29,8 @@ public interface UserService extends
 
     UserEntity selectByUsername(String username);
 
+    UserEntity selectByUserNameAndPassword(String plainUsername,String plainPassword);
+
     String encodePassword(String password, String salt);
 
     void updatePassword(String userId, String oldPassword, String newPassword);

+ 13 - 2
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

@@ -81,6 +81,17 @@ public class SimpleUserService extends AbstractService<UserEntity, String>
         return createQuery().where("username", username).single();
     }
 
+    @Override
+    @Transactional(readOnly = true)
+    public UserEntity selectByUserNameAndPassword(String plainUsername, String plainPassword) {
+        Objects.requireNonNull(plainUsername);
+        Objects.requireNonNull(plainPassword);
+
+        return Optional.ofNullable(selectByUsername(plainUsername))
+                .filter(user -> encodePassword(plainPassword, user.getSalt()).equals(user.getPassword()))
+                .orElse(null);
+    }
+
     @Override
     @Transactional(readOnly = true)
     public UserEntity selectByPk(String id) {
@@ -149,8 +160,8 @@ public class SimpleUserService extends AbstractService<UserEntity, String>
     @Caching(evict = {
             @CacheEvict(value = USER_CACHE_NAME, key = "#userId"),
             @CacheEvict(value = USER_AUTH_CACHE_NAME, key = "#userId"),
-            @CacheEvict(value = USER_AUTH_CACHE_NAME,key = "'user-menu-list:'+#userId"),
-            @CacheEvict(value = USER_AUTH_CACHE_NAME,key = "'menu-tree:'+#userId")
+            @CacheEvict(value = USER_AUTH_CACHE_NAME, key = "'user-menu-list:'+#userId"),
+            @CacheEvict(value = USER_AUTH_CACHE_NAME, key = "'menu-tree:'+#userId")
     })
     public void update(String userId, UserEntity userEntity) {
         userEntity.setId(userId);