|
@@ -0,0 +1,120 @@
|
|
|
+package cn.com.qmth.examcloud.core.basic.service.impl;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import org.apache.commons.collections.CollectionUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import com.google.common.collect.Lists;
|
|
|
+
|
|
|
+import cn.com.qmth.examcloud.commons.base.exception.StatusException;
|
|
|
+import cn.com.qmth.examcloud.commons.base.util.UUID;
|
|
|
+import cn.com.qmth.examcloud.commons.web.redis.RedisClient;
|
|
|
+import cn.com.qmth.examcloud.commons.web.security.bean.Role;
|
|
|
+import cn.com.qmth.examcloud.commons.web.security.bean.User;
|
|
|
+import cn.com.qmth.examcloud.commons.web.support.ResponseStatus;
|
|
|
+import cn.com.qmth.examcloud.core.basic.api.UserCloudService;
|
|
|
+import cn.com.qmth.examcloud.core.basic.api.bean.RoleBean;
|
|
|
+import cn.com.qmth.examcloud.core.basic.api.bean.UserBean;
|
|
|
+import cn.com.qmth.examcloud.core.basic.api.request.LoginReq;
|
|
|
+import cn.com.qmth.examcloud.core.basic.api.response.LoginResp;
|
|
|
+import cn.com.qmth.examcloud.core.basic.service.AuthService;
|
|
|
+import cn.com.qmth.examcloud.core.basic.service.bean.LoginInfo;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 类注释
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ * @date 2018年5月25日
|
|
|
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class AuthServiceImpl implements AuthService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ RedisClient redisClient;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ UserCloudService userCloudService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public User login(LoginInfo loginInfo) {
|
|
|
+ LoginReq loginReq = new LoginReq();
|
|
|
+ loginReq.setAccountType(loginInfo.getAccountType());
|
|
|
+ loginReq.setAccountValue(loginInfo.getAccountValue());
|
|
|
+ loginReq.setRootOrgId(loginInfo.getRootOrgId());
|
|
|
+ loginReq.setDomain(loginInfo.getDomain());
|
|
|
+ loginReq.setPassword(loginInfo.getPassword());
|
|
|
+ LoginResp loginResp = null;
|
|
|
+ try {
|
|
|
+ loginResp = userCloudService.login(loginReq);
|
|
|
+ } catch (StatusException e) {
|
|
|
+ if (e.getCode().equals(ResponseStatus.SERVER_ERROR.getCode())) {
|
|
|
+ throw e;
|
|
|
+ } else {
|
|
|
+ throw new StatusException("P-001001", "用户名或密码错误", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ UserBean userBean = loginResp.getUserBean();
|
|
|
+
|
|
|
+ User user = new User();
|
|
|
+ List<Role> roleList = Lists.newArrayList();
|
|
|
+ user.setRoleList(roleList);
|
|
|
+
|
|
|
+ user.setUserType(userBean.getUserType());
|
|
|
+ user.setUserId(userBean.getUserId());
|
|
|
+ user.setDisplayName(userBean.getDisplayName());
|
|
|
+ user.setRootOrgId(userBean.getRootOrgId());
|
|
|
+ user.setRootOrgName(userBean.getRootOrgName());
|
|
|
+ user.setOrgId(userBean.getOrgId());
|
|
|
+ user.setOrgName(userBean.getOrgName());
|
|
|
+ user.setIdentityNumber(userBean.getIdentityNumber());
|
|
|
+ user.setStudentCode(userBean.getStudentCode());
|
|
|
+
|
|
|
+ List<RoleBean> roleBeanList = userBean.getRoleList();
|
|
|
+
|
|
|
+ if (CollectionUtils.isNotEmpty(roleBeanList)) {
|
|
|
+ for (RoleBean cur : roleBeanList) {
|
|
|
+ Role role = new Role();
|
|
|
+ role.setRoleCode(cur.getRoleCode());
|
|
|
+ role.setRoleName(cur.getRoleName());
|
|
|
+ roleList.add(role);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ user.setTokenCreationTime(new Date());
|
|
|
+ user.setToken(UUID.randomUUID());
|
|
|
+
|
|
|
+ String key = buildUserKey(user.getUserType(), user.getRootOrgId(), user.getUserId());
|
|
|
+ user.setKey(key);
|
|
|
+ user.setUserToken(key+":"+user.getToken());
|
|
|
+
|
|
|
+ redisClient.set(key, user, 2 * 60 * 60);
|
|
|
+
|
|
|
+ return user;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建key
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ * @param userType
|
|
|
+ * @param rootOrgId
|
|
|
+ * @param userId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String buildUserKey(String userType, Long rootOrgId, Long userId) {
|
|
|
+ String key = StringUtils.join("U_", userType, "_", String.valueOf(rootOrgId), "_",
|
|
|
+ String.valueOf(userId));
|
|
|
+ return key;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void logout(User user) {
|
|
|
+ redisClient.delete(user.getKey());
|
|
|
+ }
|
|
|
+
|
|
|
+}
|