deason %!s(int64=7) %!d(string=hai) anos
pai
achega
9db18c5f9e

+ 23 - 6
src/main/java/cn/com/qmth/examcloud/app/config/AccessInterceptor.java

@@ -8,8 +8,11 @@
 package cn.com.qmth.examcloud.app.config;
 
 import cn.com.qmth.examcloud.app.model.DeviceRecord;
+import cn.com.qmth.examcloud.app.model.Result;
+import cn.com.qmth.examcloud.app.model.UserInfo;
 import cn.com.qmth.examcloud.app.model.UserToken;
 import cn.com.qmth.examcloud.app.service.DeviceRecordService;
+import cn.com.qmth.examcloud.app.service.UserAuthService;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -28,17 +31,31 @@ public class AccessInterceptor extends HandlerInterceptorAdapter {
     private final static Logger log = LoggerFactory.getLogger(AccessInterceptor.class);
     @Autowired
     private DeviceRecordService deviceRecordService;
+    @Autowired
+    private UserAuthService userAuthService;
 
     @Override
     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
         DeviceRecord record = this.parseParams(request);
-        if (StringUtils.isNotEmpty(record.getLoginKey())) {
-            /*HttpSession session = request.getSession();
-            UserToken curUser = (UserToken) session.getAttribute(record.getLoginKey());
-            if (curUser == null) {
+        /*if (StringUtils.isNotEmpty(record.getLoginToken())) {
+            //带Token时则需要认证
+            HttpSession session = request.getSession();
+            System.out.println(record.getLoginToken() + " " + session.getMaxInactiveInterval());
 
-            }*/
-        }
+            //获取原来的登录信息,并判断是否再有效时间内
+            UserToken userToken = (UserToken) session.getAttribute(record.getLoginToken());
+            if (userToken != null && !userToken.hasExpired()) {
+                //登录已失效,则Token续期
+                Result<UserInfo> result = userAuthService.login(userToken.getAccount(), userToken.getPassword(),
+                        userToken.getAccountType(), userToken.getRootOrgId(), userToken.getDomain());
+                UserInfo userInfo = result.getData();
+                if (userInfo != null) {
+                    log.debug("new token is " + userInfo.getToken());
+                    userToken.setToken(userInfo.getToken());
+                    session.setAttribute(record.getLoginToken(), userToken);
+                }
+            }
+        }*/
         //异步保存设备访问记录
         deviceRecordService.addDeviceRecord(record);
         return true;

+ 1 - 1
src/main/java/cn/com/qmth/examcloud/app/controller/v1/PracticeExamRestController.java

@@ -96,7 +96,7 @@ public class PracticeExamRestController {
         return netExamService.checkOnlineExamRecord(key, token);
     }
 
-    @ApiOperation(value = "获取当前考生的当前课程练习统计信息接口")
+    @ApiOperation(value = "获取当前考生的当前课程练习统计信息接口", hidden = true)
     @RequestMapping(value = "/exam/record/practice/course/total", method = {RequestMethod.GET, RequestMethod.POST})
     public Result getExamRecordPracticeTotalInfo(@RequestHeader String key, @RequestHeader String token, @RequestParam String examStudentId) throws Exception {
         return netExamService.getExamRecordPracticeTotalInfo(key, token, examStudentId);

+ 26 - 17
src/main/java/cn/com/qmth/examcloud/app/controller/v1/UserAuthRestController.java

@@ -7,19 +7,20 @@
 
 package cn.com.qmth.examcloud.app.controller.v1;
 
-import cn.com.qmth.examcloud.app.model.Constants;
 import cn.com.qmth.examcloud.app.model.Result;
+import cn.com.qmth.examcloud.app.model.UserInfo;
 import cn.com.qmth.examcloud.app.model.UserToken;
 import cn.com.qmth.examcloud.app.service.UserAuthService;
-import cn.com.qmth.examcloud.app.utils.JsonMapper;
+import cn.com.qmth.examcloud.app.utils.StrUtils;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpSession;
-import java.util.Date;
 
 import static cn.com.qmth.examcloud.app.model.Constants.COMMON_ACCOUNT_TYPE;
 
@@ -32,26 +33,34 @@ import static cn.com.qmth.examcloud.app.model.Constants.COMMON_ACCOUNT_TYPE;
 @RequestMapping("/api/v1")
 @Api(tags = "认证中心相关接口")
 public class UserAuthRestController {
+    private final static Logger log = LoggerFactory.getLogger(UserAuthRestController.class);
     @Autowired
     private UserAuthService userAuthService;
 
     @ApiOperation(value = "登录接口")
     @RequestMapping(value = "/user/login", method = {RequestMethod.GET, RequestMethod.POST})
-    public Result login(@RequestParam String account, @RequestParam String password, @RequestParam(required = false) String accountType,
-                        @RequestParam(required = false) Long rootOrgId, @RequestParam(required = false) String domain, HttpServletRequest request) throws Exception {
-        Result<String> result = userAuthService.login(account, password, accountType, rootOrgId, domain);
-        if (result.isSuccess()) {
-            UserToken curUser = new JsonMapper().fromJson(result.getData(), UserToken.class);
-            curUser.setAccount(account);
-            curUser.setPassword(password);
-            curUser.setAccountType(accountType != null ? accountType : COMMON_ACCOUNT_TYPE);
-            curUser.setDomain(domain);
-            curUser.setCreateTime(new Date());
-            //存放到Session中
-            HttpSession session = request.getSession();
-            session.setMaxInactiveInterval(Constants.EXPIRE_TIME);
-            session.setAttribute(curUser.getKey(), curUser);
+    public Result<UserInfo> login(@RequestParam String account, @RequestParam String password, @RequestParam(required = false) String accountType,
+                                  @RequestParam(required = false) Long rootOrgId, @RequestParam(required = false) String domain, HttpServletRequest request) throws Exception {
+        if (accountType == null) {
+            accountType = COMMON_ACCOUNT_TYPE;
         }
+        Result<UserInfo> result = userAuthService.login(account, password, accountType, rootOrgId, domain);
+        /*if (result.isSuccess()) {
+            //获取当前登录用户信息
+            UserInfo userInfo = result.getData();
+            UserToken userToken = new UserToken(account, password, accountType, rootOrgId, domain, userInfo.getKey(), userInfo.getToken());
+
+            //存放登录信息到Session中
+            String sessionKey = StrUtils.uuid();
+            HttpSession session = request.getSession();
+            //session.setMaxInactiveInterval(EXPIRE_TIME);
+            session.setMaxInactiveInterval(60);
+            session.setAttribute(sessionKey, userToken);
+            log.debug("token:" + userToken.getToken() + " sessionId:" + sessionKey);
+
+            //替换Token为本地SessionKey
+            userInfo.setToken(sessionKey);
+        }*/
         return result;
     }
 

+ 1 - 1
src/main/java/cn/com/qmth/examcloud/app/model/Constants.java

@@ -10,7 +10,7 @@ package cn.com.qmth.examcloud.app.model;
 public interface Constants {
 
     /* Session的过期时长,单位为秒 */
-    int EXPIRE_TIME = 2 * 60 * 60;
+    int EXPIRE_TIME = 720 * 60 * 60;
 
     String CHARSET_JSON_UTF8 = "application/json; charset=utf-8";
 

+ 126 - 0
src/main/java/cn/com/qmth/examcloud/app/model/UserInfo.java

@@ -0,0 +1,126 @@
+package cn.com.qmth.examcloud.app.model;
+
+import java.io.Serializable;
+
+/* 用户信息 */
+public class UserInfo implements Serializable {
+    private static final long serialVersionUID = 1L;
+    private Long userId;
+    private String userType;
+    private String displayName;
+    private String identityNumber;
+    private String studentCode;
+    private String studentId;
+    private String phone;
+    private Long rootOrgId;
+    private String rootOrgName;
+    private Long orgId;
+    private String orgName;
+    private String key;
+    private String token;
+
+    public Long getUserId() {
+        return userId;
+    }
+
+    public void setUserId(Long userId) {
+        this.userId = userId;
+    }
+
+    public String getUserType() {
+        return userType;
+    }
+
+    public void setUserType(String userType) {
+        this.userType = userType;
+    }
+
+    public String getDisplayName() {
+        return displayName;
+    }
+
+    public void setDisplayName(String displayName) {
+        this.displayName = displayName;
+    }
+
+    public String getIdentityNumber() {
+        return identityNumber;
+    }
+
+    public void setIdentityNumber(String identityNumber) {
+        this.identityNumber = identityNumber;
+    }
+
+    public String getStudentCode() {
+        return studentCode;
+    }
+
+    public void setStudentCode(String studentCode) {
+        this.studentCode = studentCode;
+    }
+
+    public String getStudentId() {
+        return studentId;
+    }
+
+    public void setStudentId(String studentId) {
+        this.studentId = studentId;
+    }
+
+    public String getPhone() {
+        return phone;
+    }
+
+    public void setPhone(String phone) {
+        this.phone = phone;
+    }
+
+    public Long getRootOrgId() {
+        return rootOrgId;
+    }
+
+    public void setRootOrgId(Long rootOrgId) {
+        this.rootOrgId = rootOrgId;
+    }
+
+    public String getRootOrgName() {
+        return rootOrgName;
+    }
+
+    public void setRootOrgName(String rootOrgName) {
+        this.rootOrgName = rootOrgName;
+    }
+
+    public Long getOrgId() {
+        return orgId;
+    }
+
+    public void setOrgId(Long orgId) {
+        this.orgId = orgId;
+    }
+
+    public String getOrgName() {
+        return orgName;
+    }
+
+    public void setOrgName(String orgName) {
+        this.orgName = orgName;
+    }
+
+    public String getKey() {
+        return key;
+    }
+
+    public void setKey(String key) {
+        this.key = key;
+    }
+
+    public String getToken() {
+        return token;
+    }
+
+    public void setToken(String token) {
+        this.token = token;
+    }
+
+}

+ 41 - 28
src/main/java/cn/com/qmth/examcloud/app/model/UserToken.java

@@ -1,51 +1,48 @@
 package cn.com.qmth.examcloud.app.model;
 
 import java.io.Serializable;
+import java.util.Calendar;
 import java.util.Date;
 
+/* 用户登录信息 */
 public class UserToken implements Serializable {
     private static final long serialVersionUID = 1L;
-    private Long userId;
-    private Long rootOrgId;
-    private Long orgId;
-    private String domain;
     private String account;
     private String password;
     private String accountType;
+    private Long rootOrgId;
+    private String domain;
     private String key;
     private String token;
     private Date createTime;
 
-    public Long getUserId() {
-        return userId;
-    }
-
-    public void setUserId(Long userId) {
-        this.userId = userId;
-    }
-
-    public Long getRootOrgId() {
-        return rootOrgId;
-    }
-
-    public void setRootOrgId(Long rootOrgId) {
+    public UserToken(String account, String password, String accountType, Long rootOrgId, String domain, String key, String token) {
+        this.account = account;
+        this.password = password;
+        this.accountType = accountType;
         this.rootOrgId = rootOrgId;
+        this.domain = domain;
+        this.key = key;
+        this.token = token;
+        this.createTime = new Date();
     }
 
-    public Long getOrgId() {
-        return orgId;
-    }
-
-    public void setOrgId(Long orgId) {
-        this.orgId = orgId;
-    }
+    public UserToken() {
 
-    public String getDomain() {
-        return domain;
     }
 
-    public void setDomain(String domain) {
-        this.domain = domain;
+    public boolean hasExpired() {
+        if (createTime == null) {
+            return true;
+        }
+        Calendar c = Calendar.getInstance();
+        c.setTime(new Date());
+        c.set(Calendar.HOUR, 2);
+        //判断是否在2小时内
+        if (c.getTime().before(createTime)) {
+            return false;
+        }
+        return true;
     }
 
     public String getAccount() {
@@ -72,6 +69,22 @@ public class UserToken implements Serializable {
         this.accountType = accountType;
     }
 
+    public Long getRootOrgId() {
+        return rootOrgId;
+    }
+
+    public void setRootOrgId(Long rootOrgId) {
+        this.rootOrgId = rootOrgId;
+    }
+
+    public String getDomain() {
+        return domain;
+    }
+
+    public void setDomain(String domain) {
+        this.domain = domain;
+    }
+
     public String getKey() {
         return key;
     }

+ 1 - 0
src/main/java/cn/com/qmth/examcloud/app/service/NetExamService.java

@@ -141,6 +141,7 @@ public class NetExamService {
         return HttpUtils.doGet(requestUrl, key, token);
     }
 
+    @Deprecated
     public Result getExamRecordPracticeTotalInfo(String key, String token, String examStudentId) throws Exception {
         //封装请求参数
         final String requestUrl = String.format("%s/api/practice_course/%s", propertyService.getNetExamUrl(), examStudentId);

+ 8 - 4
src/main/java/cn/com/qmth/examcloud/app/service/UserAuthService.java

@@ -10,6 +10,7 @@ package cn.com.qmth.examcloud.app.service;
 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.UserInfo;
 import cn.com.qmth.examcloud.app.utils.HttpBuilder;
 import cn.com.qmth.examcloud.app.utils.HttpUtils;
 import cn.com.qmth.examcloud.app.utils.JsonMapper;
@@ -23,7 +24,8 @@ import org.springframework.util.Assert;
 import java.util.HashMap;
 import java.util.Map;
 
-import static cn.com.qmth.examcloud.app.model.Constants.*;
+import static cn.com.qmth.examcloud.app.model.Constants.PARAM_KEY;
+import static cn.com.qmth.examcloud.app.model.Constants.PARAM_TOKEN;
 
 /**
  * 认证中心业务服务接口
@@ -34,13 +36,13 @@ public class UserAuthService {
     @Autowired
     private PropertyService propertyService;
 
-    public Result<String> login(String account, String password, String accountType, Long rootOrgId, String domain) throws Exception {
+    public Result<UserInfo> login(String account, String password, String accountType, Long rootOrgId, String domain) throws Exception {
         //封装请求参数
         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 != null ? accountType : COMMON_ACCOUNT_TYPE);
+        params.put("accountType", accountType);
         params.put("rootOrgId", rootOrgId != null ? rootOrgId.toString() : "");
         params.put("domain", domain);
         String json = new JsonMapper().toJson(params);
@@ -53,7 +55,9 @@ public class UserAuthService {
         Response response = HttpBuilder.client.getInstance().newCall(request).execute();
         String bodyStr = response.body().string();
         if (response.isSuccessful()) {
-            return new Result().success(bodyStr);
+            JsonMapper mapper = new JsonMapper();
+            UserInfo userInfo = mapper.fromJson(bodyStr, UserInfo.class);
+            return new Result().success(userInfo);
         } else {
             log.warn("Http response is " + bodyStr);
             ResBody body = new JsonMapper().fromJson(bodyStr, ResBody.class);

+ 5 - 0
src/main/java/cn/com/qmth/examcloud/app/utils/StrUtils.java

@@ -8,9 +8,14 @@
 package cn.com.qmth.examcloud.app.utils;
 
 import java.util.Random;
+import java.util.UUID;
 
 public class StrUtils {
 
+    public static String uuid() {
+        return UUID.randomUUID().toString().replaceAll("-", "");
+    }
+
     public static Integer randomNumber() {
         return randomNumber(100000, 999999);
     }

+ 12 - 0
src/test/java/cn/com/qmth/examcloud/app/SimpleTest.java

@@ -0,0 +1,12 @@
+package cn.com.qmth.examcloud.app;
+
+import org.junit.Test;
+
+public class SimpleTest {
+
+    @Test
+    public void demo() throws Exception {
+
+    }
+
+}