Переглянути джерело

添加获取登录用户逻辑

wangwei 7 роки тому
батько
коміт
5b31b7e49f

+ 17 - 0
examcloud-core-basic-api-provider/src/main/java/cn/com/qmth/examcloud/service/core/api/AuthController.java

@@ -1,9 +1,11 @@
 package cn.com.qmth.examcloud.service.core.api;
 
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
 import cn.com.qmth.examcloud.commons.base.exception.StatusException;
@@ -43,4 +45,19 @@ public class AuthController extends ControllerSupport {
 		}
 		authService.logout(user);
 	}
+
+	@ApiOperation(value = "获取登录用户", notes = "")
+	@PostMapping("getLoginUser")
+	public User getLoginUser(@RequestParam String key, @RequestParam String token) {
+
+		if (StringUtils.isBlank(key)) {
+			throw new StatusException("P-001009", "key is blank");
+		}
+		if (StringUtils.isBlank(key)) {
+			throw new StatusException("P-001010", "token is blank");
+		}
+
+		return authService.getLoginUser(key, token);
+	}
+
 }

+ 16 - 0
examcloud-core-basic-service/src/main/java/cn/com/qmth/examcloud/core/basic/service/AuthService.java

@@ -21,6 +21,22 @@ public interface AuthService {
 	 */
 	User login(LoginInfo loginInfo);
 
+	/**
+	 * 登出
+	 *
+	 * @author WANGWEI
+	 * @param user
+	 */
 	void logout(User user);
 
+	/**
+	 * 获取登录用户
+	 *
+	 * @author WANGWEI
+	 * @param key
+	 * @param token
+	 * @return
+	 */
+	User getLoginUser(String key, String token);
+
 }

+ 15 - 1
examcloud-core-basic-service/src/main/java/cn/com/qmth/examcloud/core/basic/service/impl/AuthServiceImpl.java

@@ -90,7 +90,7 @@ public class AuthServiceImpl implements AuthService {
 
 		String key = buildUserKey(user.getUserType(), user.getRootOrgId(), user.getUserId());
 		user.setKey(key);
-		user.setUserToken(key+":"+user.getToken());
+		user.setUserToken(key + ":" + user.getToken());
 
 		redisClient.set(key, user, 2 * 60 * 60);
 
@@ -117,4 +117,18 @@ public class AuthServiceImpl implements AuthService {
 		redisClient.delete(user.getKey());
 	}
 
+	@Override
+	public User getLoginUser(String key, String token) {
+		User user = redisClient.get(key, User.class);
+		if (null == user) {
+			throw new StatusException("P-001012", "未登录");
+		}
+
+		if (user.getToken().equals(token)) {
+			throw new StatusException("P-001012", "token错误");
+		}
+
+		return user;
+	}
+
 }