Parcourir la source

继续修改对外API异常处理方式

1.修改字符集设置filter错误
2.重新定义异常类与错误信息
luoshi il y a 6 ans
Parent
commit
5b0ce5fbe1

+ 0 - 31
stmms-biz/src/main/java/cn/com/qmth/stmms/biz/api/auth/exception/ApiException.java

@@ -1,31 +0,0 @@
-package cn.com.qmth.stmms.biz.api.auth.exception;
-
-public class ApiException extends RuntimeException {
-
-    private static final long serialVersionUID = 1808125284186980834L;
-
-    public static final ApiException USER_UNEXIST = new ApiException(401, "用户不存在");
-
-    public static final ApiException USER_PASSWORD_ERROR = new ApiException(401, "用户密码错误");
-
-    public static final ApiException USER_NOT_ADMIN = new ApiException(401, "用户没有管理员权限");
-
-    public static final ApiException EXAM_NOT_ACCESSIBLE = new ApiException(401, "用户没有访问该考试的权限");
-
-    private int code;
-
-    private String message;
-
-    private ApiException(int code, String message) {
-        this.code = code;
-        this.message = message;
-    }
-
-    public int getCode() {
-        return code;
-    }
-
-    public String getMessage() {
-        return message;
-    }
-}

+ 11 - 0
stmms-biz/src/main/java/cn/com/qmth/stmms/biz/api/auth/exception/ExamNotAccessibleException.java

@@ -0,0 +1,11 @@
+package cn.com.qmth.stmms.biz.api.auth.exception;
+
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.ResponseStatus;
+
+@ResponseStatus(value = HttpStatus.UNAUTHORIZED, reason = "用户没有访问该考试的权限")
+public class ExamNotAccessibleException extends RuntimeException {
+
+    private static final long serialVersionUID = -4572037880420043500L;
+
+}

+ 11 - 0
stmms-biz/src/main/java/cn/com/qmth/stmms/biz/api/auth/exception/UserNotAdminException.java

@@ -0,0 +1,11 @@
+package cn.com.qmth.stmms.biz.api.auth.exception;
+
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.ResponseStatus;
+
+@ResponseStatus(value = HttpStatus.UNAUTHORIZED, reason = "用户没有管理员权限")
+public class UserNotAdminException extends RuntimeException {
+
+    private static final long serialVersionUID = -2421416160871608645L;
+
+}

+ 11 - 0
stmms-biz/src/main/java/cn/com/qmth/stmms/biz/api/auth/exception/UserPasswordErrorException.java

@@ -0,0 +1,11 @@
+package cn.com.qmth.stmms.biz.api.auth.exception;
+
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.ResponseStatus;
+
+@ResponseStatus(value = HttpStatus.UNAUTHORIZED, reason = "用户密码错误")
+public class UserPasswordErrorException extends RuntimeException {
+
+    private static final long serialVersionUID = 2155033130710649371L;
+
+}

+ 11 - 0
stmms-biz/src/main/java/cn/com/qmth/stmms/biz/api/auth/exception/UserUnExistException.java

@@ -0,0 +1,11 @@
+package cn.com.qmth.stmms.biz.api.auth.exception;
+
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.ResponseStatus;
+
+@ResponseStatus(value = HttpStatus.UNAUTHORIZED, reason = "用户不存在")
+public class UserUnExistException extends RuntimeException {
+
+    private static final long serialVersionUID = -4572037880420043500L;
+
+}

+ 4 - 3
stmms-biz/src/main/java/cn/com/qmth/stmms/biz/api/auth/service/validator/AdminUserValidator.java

@@ -5,7 +5,8 @@ import javax.annotation.PostConstruct;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
-import cn.com.qmth.stmms.biz.api.auth.exception.ApiException;
+import cn.com.qmth.stmms.biz.api.auth.exception.UserNotAdminException;
+import cn.com.qmth.stmms.biz.api.auth.exception.UserUnExistException;
 import cn.com.qmth.stmms.biz.api.auth.interfaces.AuthValidator;
 import cn.com.qmth.stmms.biz.api.auth.model.AuthInfo;
 import cn.com.qmth.stmms.biz.user.model.User;
@@ -26,12 +27,12 @@ public class AdminUserValidator implements AuthValidator {
     public boolean validate(AuthInfo auth) {
         User user = auth.getLoginUser();
         if (user == null) {
-            throw ApiException.USER_UNEXIST;
+            throw new UserUnExistException();
         }
         if (user.getRoles().contains(Role.CAMPUS_ADMIN) || user.getRoles().contains(Role.SCHOOL_ADMIN)) {
             return true;
         } else {
-            throw ApiException.USER_NOT_ADMIN;
+            throw new UserNotAdminException();
         }
     }
 

+ 4 - 3
stmms-biz/src/main/java/cn/com/qmth/stmms/biz/api/auth/service/validator/DefaultValidator.java

@@ -5,7 +5,8 @@ import javax.annotation.PostConstruct;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
-import cn.com.qmth.stmms.biz.api.auth.exception.ApiException;
+import cn.com.qmth.stmms.biz.api.auth.exception.UserPasswordErrorException;
+import cn.com.qmth.stmms.biz.api.auth.exception.UserUnExistException;
 import cn.com.qmth.stmms.biz.api.auth.interfaces.AuthValidator;
 import cn.com.qmth.stmms.biz.api.auth.model.AuthInfo;
 import cn.com.qmth.stmms.biz.api.auth.model.BasicAuthInfo;
@@ -46,10 +47,10 @@ public class DefaultValidator implements AuthValidator {
                 info.setLoginUser(RoleAuthUtil.buildRoleAuthByUser(user, roleService));
                 return true;
             } else {
-                throw ApiException.USER_PASSWORD_ERROR;
+                throw new UserPasswordErrorException();
             }
         } else {
-            throw ApiException.USER_UNEXIST;
+            throw new UserUnExistException();
         }
     }
 

+ 3 - 5
stmms-web/src/main/java/cn/com/qmth/stmms/api/controller/ExamInfoController.java

@@ -1,6 +1,5 @@
 package cn.com.qmth.stmms.api.controller;
 
-import java.io.IOException;
 import java.util.LinkedList;
 import java.util.List;
 
@@ -17,7 +16,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
 
 import cn.com.qmth.stmms.admin.vo.ExamSubjectVO;
 import cn.com.qmth.stmms.biz.api.auth.annotation.AuthValidate;
-import cn.com.qmth.stmms.biz.api.auth.exception.ApiException;
+import cn.com.qmth.stmms.biz.api.auth.exception.ExamNotAccessibleException;
 import cn.com.qmth.stmms.biz.campus.model.Campus;
 import cn.com.qmth.stmms.biz.campus.service.CampusService;
 import cn.com.qmth.stmms.biz.exam.model.Exam;
@@ -116,7 +115,7 @@ public class ExamInfoController {
     @RequestMapping(value = "/subject/update", method = RequestMethod.POST)
     @ResponseBody
     public JSONObject updateSubject(HttpServletRequest request, HttpServletResponse response,
-            @RequestBody ExamSubjectVO subject) throws IOException {
+            @RequestBody ExamSubjectVO subject) {
         User user = RequestUtils.getApiUser(request);
         JSONObject result = new JSONObject();
         Exam exam = examService.findById(subject.getExamId());
@@ -135,8 +134,7 @@ public class ExamInfoController {
                 result.accumulate("code", "");
             }
         } else {
-            response.sendError(ApiException.EXAM_NOT_ACCESSIBLE.getCode(),
-                    ApiException.EXAM_NOT_ACCESSIBLE.getMessage());
+            throw new ExamNotAccessibleException();
         }
         return result;
     }

+ 9 - 10
stmms-web/src/main/java/cn/com/qmth/stmms/api/controller/ExamStudentController.java

@@ -1,6 +1,5 @@
 package cn.com.qmth.stmms.api.controller;
 
-import java.io.IOException;
 import java.text.DecimalFormat;
 import java.util.LinkedList;
 import java.util.List;
@@ -22,7 +21,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
 
 import cn.com.qmth.stmms.api.utils.AESUtil;
 import cn.com.qmth.stmms.biz.api.auth.annotation.AuthValidate;
-import cn.com.qmth.stmms.biz.api.auth.exception.ApiException;
+import cn.com.qmth.stmms.biz.api.auth.exception.ExamNotAccessibleException;
 import cn.com.qmth.stmms.biz.campus.model.Campus;
 import cn.com.qmth.stmms.biz.campus.service.CampusService;
 import cn.com.qmth.stmms.biz.exam.model.Exam;
@@ -70,7 +69,7 @@ public class ExamStudentController {
     @RequestMapping(value = "/package/{examId}", method = RequestMethod.GET)
     @ResponseBody
     public JSONArray getPackageCode(HttpServletRequest request, HttpServletResponse response,
-            @PathVariable Integer examId) throws IOException {
+            @PathVariable Integer examId) {
         User user = RequestUtils.getApiUser(request);
         JSONArray array = new JSONArray();
         Exam exam = examService.findById(examId);
@@ -82,8 +81,7 @@ public class ExamStudentController {
                 }
             }
         } else {
-            response.sendError(ApiException.EXAM_NOT_ACCESSIBLE.getCode(),
-                    ApiException.EXAM_NOT_ACCESSIBLE.getMessage());
+            throw new ExamNotAccessibleException();
         }
         return array;
     }
@@ -92,7 +90,7 @@ public class ExamStudentController {
     @RequestMapping(value = "/package/{examId}", method = RequestMethod.POST)
     @ResponseBody
     public int updatePackage(HttpServletRequest request, HttpServletResponse response, @PathVariable Integer examId,
-            @RequestBody ExamPackage examPackage) throws IOException {
+            @RequestBody ExamPackage examPackage) {
         User user = RequestUtils.getApiUser(request);
         Exam exam = examService.findById(examId);
         if (exam != null && exam.getSchoolId().equals(user.getSchoolId())) {
@@ -103,14 +101,13 @@ public class ExamStudentController {
                 return obj.getPicCount();
             }
         } else {
-            response.sendError(ApiException.EXAM_NOT_ACCESSIBLE.getCode(),
-                    ApiException.EXAM_NOT_ACCESSIBLE.getMessage());
+            throw new ExamNotAccessibleException();
         }
         return -1;
     }
 
     @AuthValidate("adminUser")
-    @RequestMapping(value = "/exam/students/{examId}", method = RequestMethod.GET)
+    @RequestMapping("/exam/students/{examId}")
     @ResponseBody
     public JSONArray getExamStudents(HttpServletRequest request, @PathVariable Integer examId,
             @RequestParam(required = false) Integer pageNumber, @RequestParam(required = false) Integer pageSize) {
@@ -147,7 +144,7 @@ public class ExamStudentController {
     }
 
     @AuthValidate("adminUser")
-    @RequestMapping(value = "/students/{examId}", method = RequestMethod.GET)
+    @RequestMapping("/students/{examId}")
     @ResponseBody
     public JSONArray getStudent(HttpServletRequest request, @PathVariable Integer examId,
             @RequestParam(required = false) Boolean upload, @RequestParam(required = false) Boolean absent,
@@ -166,12 +163,14 @@ public class ExamStudentController {
             for (ExamStudent student : query.getResult()) {
                 JSONObject obj = new JSONObject();
                 obj.accumulate("id", student.getId());
+                obj.accumulate("schoolId", student.getSchoolId());
                 obj.accumulate("examNumber", student.getExamNumber());
                 obj.accumulate("campusName", student.getCampusName());
                 obj.accumulate("subjectCode", student.getSubjectCode());
                 obj.accumulate("subjectName", student.getSubjectName());
                 obj.accumulate("name", student.getName());
                 obj.accumulate("studentCode", student.getStudentCode());
+                obj.accumulate("packageCode", StringUtils.trimToEmpty(student.getPackageCode()));
 
                 Campus campus = campusService.findBySchoolAndName(exam.getSchoolId(), student.getCampusName());
                 obj.accumulate("campusCode", campus != null ? campus.getId().toString() : "");

+ 3 - 5
stmms-web/src/main/java/cn/com/qmth/stmms/api/controller/ScanController.java

@@ -21,7 +21,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
 import cn.com.qmth.stmms.admin.exam.parameter.BaseParameterController;
 import cn.com.qmth.stmms.api.utils.ScanStudentParameter;
 import cn.com.qmth.stmms.biz.api.auth.annotation.AuthValidate;
-import cn.com.qmth.stmms.biz.api.auth.exception.ApiException;
+import cn.com.qmth.stmms.biz.api.auth.exception.ExamNotAccessibleException;
 import cn.com.qmth.stmms.biz.exam.model.Exam;
 import cn.com.qmth.stmms.biz.exam.model.ExamStudent;
 import cn.com.qmth.stmms.biz.exam.service.ExamService;
@@ -59,16 +59,14 @@ public class ScanController extends BaseParameterController {
     @ResponseBody
     @Transactional
     public JSONArray saveStudent(HttpServletRequest request, HttpServletResponse response, @PathVariable Integer examId,
-            @RequestBody ScanStudentParameter[] scStudentParameter) throws IOException {
+            @RequestBody ScanStudentParameter[] scStudentParameter) {
         User user = RequestUtils.getApiUser(request);
         JSONArray array = new JSONArray();
 
         Exam exam = examService.findById(examId);
         // 判断上传权限
         if (exam == null || exam.getSchoolId().equals(user.getSchoolId())) {
-            response.sendError(ApiException.EXAM_NOT_ACCESSIBLE.getCode(),
-                    ApiException.EXAM_NOT_ACCESSIBLE.getMessage());
-            return array;
+            throw new ExamNotAccessibleException();
         }
 
         if (scStudentParameter != null && scStudentParameter.length > 0) {

+ 3 - 13
stmms-web/src/main/java/cn/com/qmth/stmms/api/interceptor/ApiInterceptor.java

@@ -5,7 +5,6 @@ import java.util.List;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import org.apache.commons.lang.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
 import org.springframework.web.method.HandlerMethod;
@@ -14,7 +13,6 @@ import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
 
 import cn.com.qmth.stmms.api.utils.AuthInfoUtils;
 import cn.com.qmth.stmms.biz.api.auth.annotation.AuthValidate;
-import cn.com.qmth.stmms.biz.api.auth.exception.ApiException;
 import cn.com.qmth.stmms.biz.api.auth.interfaces.AuthValidator;
 import cn.com.qmth.stmms.biz.api.auth.model.AuthInfo;
 import cn.com.qmth.stmms.biz.api.auth.service.AuthInfoService;
@@ -51,23 +49,15 @@ public class ApiInterceptor extends HandlerInterceptorAdapter {
             if (validators != null) {
                 boolean flag = true;
                 int status = 200;
-                String reason = null;
                 for (AuthValidator validator : validators) {
-                    try {
-                        if (!validator.validate(auth)) {
-                            flag = false;
-                            status = HttpStatus.UNAUTHORIZED.value();
-                            break;
-                        }
-                    } catch (ApiException e) {
+                    if (!validator.validate(auth)) {
                         flag = false;
-                        status = e.getCode();
-                        reason = e.getMessage();
+                        status = HttpStatus.UNAUTHORIZED.value();
                         break;
                     }
                 }
                 if (!flag) {
-                    response.sendError(status, StringUtils.trimToEmpty(reason));
+                    response.sendError(status);
                 } else {
                     RequestUtils.setApiUser(request, auth.getLoginUser());
                 }

+ 1 - 1
stmms-web/src/main/webapp/WEB-INF/application.properties

@@ -1,7 +1,7 @@
 #jdbc config
 driverClassName=com.mysql.jdbc.Driver
 #jdbc config
-url=jdbc:mysql://192.168.10.30:3306/stmms_hk_0508?useUnicode=true&characterEncoding=UTF-8
+url=jdbc:mysql://192.168.10.30:3306/stmms_gx?useUnicode=true&characterEncoding=UTF-8
 username=root
 password=root
 

+ 4 - 14
stmms-web/src/main/webapp/WEB-INF/web.xml

@@ -18,16 +18,6 @@
 		<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
 	</listener>
 
-	<filter>
-		<description>字符集过滤器</description>
-		<filter-name>encodingFilter</filter-name>
-		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
-		<init-param>
-			<description>字符集编码</description>
-			<param-name>encoding</param-name>
-			<param-value>UTF-8</param-value>
-		</init-param>
-	</filter>
 	<filter>
 		<filter-name>sessionCookieFilter</filter-name>
 		<filter-class>cn.com.qmth.stmms.common.filter.SessionFilter</filter-class>
@@ -43,6 +33,10 @@
 			<param-name>encoding</param-name>
 			<param-value>utf-8</param-value>
 		</init-param>
+		<init-param>
+			<param-name>forceEncoding</param-name>
+			<param-value>true</param-value>
+		</init-param>
 	</filter>
 	<filter>
 		<filter-name>sitemeshFilter</filter-name>
@@ -54,10 +48,6 @@
 		<filter-name>characterEncodingFilter</filter-name>
 		<servlet-name>stmms-web</servlet-name>
 	</filter-mapping>
-	<filter-mapping>
-		<filter-name>encodingFilter</filter-name>
-		<servlet-name>stmms-web</servlet-name>
-	</filter-mapping>
 	<filter-mapping>
 		<filter-name>sessionCookieFilter</filter-name>
 		<servlet-name>stmms-web</servlet-name>