zxy пре 3 година
родитељ
комит
1a0dc4fb55

+ 1 - 0
src/main/java/com/api/config/WebMvcConfig.java

@@ -30,6 +30,7 @@ public class WebMvcConfig implements WebMvcConfigurer {
30 30
     @Autowired
31 31
     private LoginUserHandlerMethodArgumentResolver loginUserHandlerMethodArgumentResolver;
32 32
 
33
+
33 34
     @Override
34 35
     public void addInterceptors(InterceptorRegistry registry) {
35 36
         registry.addInterceptor(authorizationInterceptor).addPathPatterns("/api/**");

+ 2 - 1
src/main/java/com/api/controller/ApiTestController.java

@@ -26,7 +26,7 @@ import springfox.documentation.annotations.ApiIgnore;
26 26
  * @author Mark sunlightcs@gmail.com
27 27
  */
28 28
 @RestController
29
-@RequestMapping("/api")
29
+@RequestMapping("/api/api")
30 30
 @Api(tags="测试接口")
31 31
 public class ApiTestController {
32 32
 
@@ -44,6 +44,7 @@ public class ApiTestController {
44 44
         return R.ok().put("userId", userId);
45 45
     }
46 46
 
47
+    @Login
47 48
     @GetMapping("notToken")
48 49
     @ApiOperation("忽略Token验证测试")
49 50
     public R notToken(){

+ 14 - 1
src/main/java/com/api/interceptor/AuthorizationInterceptor.java

@@ -13,6 +13,9 @@ import com.common.exception.RRException;
13 13
 import com.api.annotation.Login;
14 14
 import com.api.entity.TokenEntity;
15 15
 import com.api.service.TokenService;
16
+import com.common.utils.HttpContextUtils;
17
+import com.common.utils.R;
18
+import com.google.gson.Gson;
16 19
 import org.apache.commons.lang.StringUtils;
17 20
 import org.springframework.beans.factory.annotation.Autowired;
18 21
 import org.springframework.stereotype.Component;
@@ -56,7 +59,17 @@ public class AuthorizationInterceptor extends HandlerInterceptorAdapter {
56 59
 
57 60
         //token为空
58 61
         if(StringUtils.isBlank(token)){
59
-            throw new RRException("token不能为空");
62
+//            HttpServletResponse httpResponse = (HttpServletResponse) response;
63
+            response.setContentType("application/json;charset=utf-8");
64
+            response.setHeader("Access-Control-Allow-Credentials", "true");
65
+            response.setHeader("Access-Control-Allow-Origin", HttpContextUtils.getOrigin());
66
+
67
+            R r = R.error("invalid token");
68
+            String json = new Gson().toJson(r);
69
+            response.getWriter().print(json);
70
+//            LogUtils logUtils = new LogUtils();
71
+//            logUtils.requestLog(r);
72
+            return false;
60 73
         }
61 74
 
62 75
         //查询token信息

+ 28 - 33
src/main/java/com/common/exception/RRException.java

@@ -8,6 +8,10 @@
8 8
 
9 9
 package com.common.exception;
10 10
 
11
+import lombok.Getter;
12
+import lombok.Setter;
13
+import org.springframework.util.StringUtils;
14
+
11 15
 /**
12 16
  * 自定义异常
13 17
  *
@@ -15,46 +19,37 @@ package com.common.exception;
15 19
  */
16 20
 public class RRException extends RuntimeException {
17 21
 	private static final long serialVersionUID = 1L;
18
-	
19
-    private String msg;
20
-    private int code = 500;
21
-    
22
-    public RRException(String msg) {
23
-		super(msg);
24
-		this.msg = msg;
25
-	}
26
-	
27
-	public RRException(String msg, Throwable e) {
28
-		super(msg, e);
29
-		this.msg = msg;
30
-	}
31
-	
32
-	public RRException(String msg, int code) {
33
-		super(msg);
34
-		this.msg = msg;
35
-		this.code = code;
36
-	}
37
-	
38
-	public RRException(String msg, int code, Throwable e) {
39
-		super(msg, e);
40
-		this.msg = msg;
41
-		this.code = code;
42
-	}
22
+	@Getter
23
+	@Setter
24
+	private String errmsg;
25
+	@Getter
26
+	@Setter
27
+	private String errno = "5001";
43 28
 
44
-	public String getMsg() {
45
-		return msg;
29
+	public RRException(String errmsg) {
30
+		super(errmsg);
31
+		this.errmsg = errmsg;
46 32
 	}
47 33
 
48
-	public void setMsg(String msg) {
49
-		this.msg = msg;
34
+	public RRException(String errmsg, Throwable e) {
35
+		super(errmsg, e);
36
+		this.errmsg = errmsg;
50 37
 	}
51 38
 
52
-	public int getCode() {
53
-		return code;
39
+	public RRException(String errmsg, String errno) {
40
+		super(errmsg);
41
+		this.errmsg = errmsg;
42
+		if (!StringUtils.isEmpty(errno)) {
43
+			this.errno = errno;
44
+		}
54 45
 	}
55 46
 
56
-	public void setCode(int code) {
57
-		this.code = code;
47
+	public RRException(String errmsg, String errno, Throwable e) {
48
+		super(errmsg, e);
49
+		this.errmsg = errmsg;
50
+		if (!StringUtils.isEmpty(errno)) {
51
+			this.errno = errno;
52
+		}
58 53
 	}
59 54
 	
60 55
 	

+ 4 - 0
src/main/java/com/common/utils/HttpContextUtils.java

@@ -18,4 +18,8 @@ public class HttpContextUtils {
18 18
 	public static HttpServletRequest getHttpServletRequest() {
19 19
 		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
20 20
 	}
21
+	public static String getOrigin(){
22
+		HttpServletRequest request = getHttpServletRequest();
23
+		return request.getHeader("Origin");
24
+	}
21 25
 }

+ 39 - 15
src/main/java/com/common/utils/R.java

@@ -8,6 +8,9 @@
8 8
 
9 9
 package com.common.utils;
10 10
 
11
+import com.alibaba.fastjson.JSON;
12
+
13
+import java.util.Collections;
11 14
 import java.util.HashMap;
12 15
 import java.util.Map;
13 16
 
@@ -16,41 +19,52 @@ import java.util.Map;
16 19
  *
17 20
  * @author Mark sunlightcs@gmail.com
18 21
  */
19
-public class R extends HashMap<String, Object> {
22
+public class R<T> extends HashMap<String, Object> {
20 23
 	private static final long serialVersionUID = 1L;
21
-	
24
+	/**
25
+	 * 返回数据状态编码,为0表示成功
26
+	 */
27
+	private String errno;
28
+	/**
29
+	 * 返回数据说明
30
+	 */
31
+	private String errmsg;
32
+	/**
33
+	 * 返回数据
34
+	 */
35
+	private T result;
22 36
 	public R() {
23
-		put("code", 0);
24
-		put("msg", "success");
37
+		put("errno","0");
38
+		put("errmsg", "success");
25 39
 	}
26
-	
40
+
27 41
 	public static R error() {
28
-		return error(500, "未知异常,请联系管理员");
42
+		return error("未知异常,请联系管理员");
29 43
 	}
30
-	
44
+
31 45
 	public static R error(String msg) {
32
-		return error(500, msg);
46
+		return error("5001", msg);
33 47
 	}
34
-	
35
-	public static R error(int code, String msg) {
48
+
49
+	public static R error(String code, String msg) {
36 50
 		R r = new R();
37
-		r.put("code", code);
38
-		r.put("msg", msg);
51
+		r.put("errno", code);
52
+		r.put("errmsg", msg);
39 53
 		return r;
40 54
 	}
41 55
 
42 56
 	public static R ok(String msg) {
43 57
 		R r = new R();
44
-		r.put("msg", msg);
58
+		r.put("errmsg", msg);
45 59
 		return r;
46 60
 	}
47
-	
61
+
48 62
 	public static R ok(Map<String, Object> map) {
49 63
 		R r = new R();
50 64
 		r.putAll(map);
51 65
 		return r;
52 66
 	}
53
-	
67
+
54 68
 	public static R ok() {
55 69
 		return new R();
56 70
 	}
@@ -60,4 +74,14 @@ public class R extends HashMap<String, Object> {
60 74
 		super.put(key, value);
61 75
 		return this;
62 76
 	}
77
+
78
+	public R put(Object value) {
79
+		put("result", JSON.toJSON(value));
80
+		return this;
81
+	}
82
+
83
+	public R putEmptyList() {
84
+		super.put("result", Collections.emptyList());
85
+		return this;
86
+	}
63 87
 }

+ 2 - 0
src/main/resources/application-dev.yml

@@ -1,4 +1,6 @@
1 1
 spring:
2
+  resources:
3
+    static-locations:
2 4
   datasource:
3 5
     type: com.alibaba.druid.pool.DruidDataSource
4 6
     druid:

+ 14 - 0
src/main/resources/templates/error.html

@@ -0,0 +1,14 @@
1
+<!DOCTYPE html>
2
+<html lang="en" xmlns:th="http://www.thymeleaf.org">
3
+<head>
4
+    <meta charset="UTF-8">
5
+    <title>错误</title>
6
+</head>
7
+<body>
8
+<div style="margin-top: 20px;margin-left: 10px;text-align: center;">
9
+    <h1>
10
+        <span th:text="${errmsg}"></span>
11
+    </h1>
12
+</div>
13
+</body>
14
+</html>