|
@@ -7,16 +7,14 @@
|
|
|
|
|
|
package cn.com.qmth.examcloud.app.service.impl;
|
|
|
|
|
|
-import cn.com.qmth.examcloud.app.core.utils.HttpBuilder;
|
|
|
-import cn.com.qmth.examcloud.app.core.utils.HttpUtils;
|
|
|
-import cn.com.qmth.examcloud.app.core.utils.JsonMapper;
|
|
|
-import cn.com.qmth.examcloud.app.model.Constants;
|
|
|
-import cn.com.qmth.examcloud.app.model.ResBody;
|
|
|
-import cn.com.qmth.examcloud.app.model.Result;
|
|
|
-import cn.com.qmth.examcloud.app.model.User;
|
|
|
+import cn.com.qmth.examcloud.app.core.exception.ApiException;
|
|
|
+import cn.com.qmth.examcloud.app.core.utils.*;
|
|
|
+import cn.com.qmth.examcloud.app.model.*;
|
|
|
import cn.com.qmth.examcloud.app.service.PropertyService;
|
|
|
+import cn.com.qmth.examcloud.app.service.RedisService;
|
|
|
import cn.com.qmth.examcloud.app.service.UserAuthService;
|
|
|
import okhttp3.*;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -26,8 +24,7 @@ import org.springframework.util.Assert;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
-import static cn.com.qmth.examcloud.app.model.Constants.PARAM_KEY;
|
|
|
-import static cn.com.qmth.examcloud.app.model.Constants.PARAM_TOKEN;
|
|
|
+import static cn.com.qmth.examcloud.app.model.Constants.*;
|
|
|
|
|
|
/**
|
|
|
* 认证中心业务服务接口
|
|
@@ -40,17 +37,23 @@ public class UserAuthServiceImpl implements UserAuthService {
|
|
|
private static Logger log = LoggerFactory.getLogger(UserAuthServiceImpl.class);
|
|
|
@Autowired
|
|
|
private PropertyService propertyService;
|
|
|
+ @Autowired
|
|
|
+ private RedisService redisService;
|
|
|
|
|
|
@Override
|
|
|
- public Result<User> login(String account, String password, String accountType, Long rootOrgId, String domain) throws Exception {
|
|
|
+ public Result<User> login(LoginInfo loginInfo) throws Exception {
|
|
|
+ Assert.notNull(loginInfo, "LoginInfo must be not null.");
|
|
|
+ if (StringUtils.isBlank(loginInfo.getAccountType())) {
|
|
|
+ loginInfo.setAccountType(LoginType.COMMON_LOGIN_NAME.name());
|
|
|
+ }
|
|
|
//封装请求参数
|
|
|
final String requestUrl = String.format("%s/api/ecs_core/auth/login", propertyService.getUserAuthUrl());
|
|
|
Map<String, String> params = new HashMap<>();
|
|
|
- params.put("accountValue", account);
|
|
|
- params.put("password", password);
|
|
|
- params.put("accountType", accountType);
|
|
|
- params.put("rootOrgId", rootOrgId != null ? rootOrgId.toString() : "");
|
|
|
- params.put("domain", domain);
|
|
|
+ params.put("accountValue", loginInfo.getAccount());
|
|
|
+ params.put("password", loginInfo.getPassword());
|
|
|
+ params.put("accountType", loginInfo.getAccountType());
|
|
|
+ params.put("rootOrgId", loginInfo.getRootOrgId() != null ? loginInfo.getRootOrgId().toString() : "");
|
|
|
+ params.put("domain", loginInfo.getDomain());
|
|
|
String json = new JsonMapper().toJson(params);
|
|
|
RequestBody formBody = FormBody.create(MediaType.parse(Constants.CHARSET_JSON_UTF8), json);
|
|
|
Request request = new Request.Builder()
|
|
@@ -61,8 +64,8 @@ public class UserAuthServiceImpl implements UserAuthService {
|
|
|
Response response = HttpBuilder.client.getInstance().newCall(request).execute();
|
|
|
String bodyStr = response.body().string();
|
|
|
if (response.isSuccessful()) {
|
|
|
- JsonMapper mapper = new JsonMapper();
|
|
|
- User userInfo = mapper.fromJson(bodyStr, User.class);
|
|
|
+ //获取用户信息
|
|
|
+ User userInfo = new JsonMapper().fromJson(bodyStr, User.class);
|
|
|
return new Result().success(userInfo);
|
|
|
} else {
|
|
|
log.warn("Http response is " + bodyStr);
|
|
@@ -115,4 +118,72 @@ public class UserAuthServiceImpl implements UserAuthService {
|
|
|
return new Result().error();
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void cacheLoginInfo(LoginInfo loginInfo, User userInfo) {
|
|
|
+ if (loginInfo == null || userInfo == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //封装原始登录信息
|
|
|
+ loginInfo.setKey(userInfo.getKey());
|
|
|
+ loginInfo.setToken(userInfo.getToken());
|
|
|
+ //将原始登录信息存放到Redis中
|
|
|
+ String redisKey = StrUtils.md5Key(userInfo.getKey());
|
|
|
+ this.cacheLoginInfo(loginInfo, redisKey);
|
|
|
+ log.debug("redisKey:" + redisKey + " loginToken:" + loginInfo.getToken());
|
|
|
+ //替换login token为缓存的redisKey,并作为接口的结果返回
|
|
|
+ userInfo.setToken(redisKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 缓存用户登录信息
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void cacheLoginInfo(LoginInfo loginInfo, String key) {
|
|
|
+ if (StringUtils.isEmpty(key)) {
|
|
|
+ throw new ApiException("Key must be not empty.");
|
|
|
+ }
|
|
|
+ redisService.set(APP_SESSION_USER_KEY_PREFIX + key, new JsonMapper().toJson(loginInfo), APP_SESSION_EXPIRE_TIME);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取缓存中的用户登录信息
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public LoginInfo getLoginInfo(String key) {
|
|
|
+ if (StringUtils.isEmpty(key)) {
|
|
|
+ throw new ApiException("Key must be not empty.");
|
|
|
+ }
|
|
|
+ String loginInfo = redisService.get(APP_SESSION_USER_KEY_PREFIX + key);
|
|
|
+ if (loginInfo != null) {
|
|
|
+ return new JsonMapper().fromJson(loginInfo, LoginInfo.class);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取平台端的默认过期时间(秒)
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public int getSessionTimeout() {
|
|
|
+ try {
|
|
|
+ String timeout = redisService.get(Constants.PLATFORM_SESSION_TIMEOUT_KEY);
|
|
|
+ if (StringUtils.isNotEmpty(timeout)) {
|
|
|
+ return Integer.parseInt(timeout);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ //ignore
|
|
|
+ }
|
|
|
+ return Constants.PLATFORM_SESSION_EXPIRE_TIME;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化内部接口请求鉴权
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void initRequestTrace() {
|
|
|
+ String key = "C_" + ThreadUtils.getTraceID();
|
|
|
+ Long millis = System.currentTimeMillis();
|
|
|
+ redisService.set(key, millis.toString(), 10);
|
|
|
+ }
|
|
|
+
|
|
|
}
|