Browse Source

系统安全性优化,登陆Token写Cookie时进行MD5加密,同时Cookie启用HttpOnly;

xuxueli 7 years ago
parent
commit
badcf6e3cb

+ 1 - 1
doc/XXL-JOB官方文档.md View File

@@ -1101,7 +1101,7 @@ Tips: 历史版本(V1.3.x)目前已经Release至稳定版本, 进入维护阶段
1101 1101
 - 24、Log地址格式兼容,支持非"/"结尾路径配置;
1102 1102
 - 25、底层系统日志级别规范调整,清理遗留代码;
1103 1103
 - 26、建表SQL优化,支持同步创建制定编码的库和表;
1104
-- 27、系统安全性优化,登陆Token写Cookie时进行MD5加密;
1104
+- 27、系统安全性优化,登陆Token写Cookie时进行MD5加密,同时Cookie启用HttpOnly
1105 1105
 
1106 1106
 
1107 1107
 ### TODO LIST

+ 7 - 2
xxl-job-admin/src/main/java/com/xxl/job/admin/controller/interceptor/PermissionInterceptor.java View File

@@ -17,7 +17,8 @@ import java.math.BigInteger;
17 17
  * @author xuxueli 2015-12-12 18:09:04
18 18
  */
19 19
 public class PermissionInterceptor extends HandlerInterceptorAdapter {
20
-	
20
+
21
+
21 22
 	public static final String LOGIN_IDENTITY_KEY = "XXL_JOB_LOGIN_IDENTITY";
22 23
 	public static final String LOGIN_IDENTITY_TOKEN;
23 24
     static {
@@ -30,7 +31,9 @@ public class PermissionInterceptor extends HandlerInterceptorAdapter {
30 31
 
31 32
 		LOGIN_IDENTITY_TOKEN = tokenTmp;
32 33
     }
33
-	
34
+
35
+
36
+
34 37
 	public static boolean login(HttpServletResponse response, String username, String password, boolean ifRemember){
35 38
 
36 39
     	// login token
@@ -56,6 +59,8 @@ public class PermissionInterceptor extends HandlerInterceptorAdapter {
56 59
 		return true;
57 60
 	}
58 61
 
62
+
63
+
59 64
 	@Override
60 65
 	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
61 66
 		

+ 17 - 17
xxl-job-admin/src/main/java/com/xxl/job/admin/core/util/CookieUtil.java View File

@@ -6,9 +6,11 @@ import javax.servlet.http.HttpServletResponse;
6 6
 
7 7
 /**
8 8
  * Cookie.Util
9
+ *
9 10
  * @author xuxueli 2015-12-12 18:01:06
10 11
  */
11 12
 public class CookieUtil {
13
+
12 14
 	// 默认缓存时间,单位/秒, 2H
13 15
 	private static final int COOKIE_MAX_AGE = 60 * 60 * 2;
14 16
 	// 保存路径,根路径
@@ -16,43 +18,39 @@ public class CookieUtil {
16 18
 	
17 19
 	/**
18 20
 	 * 保存
21
+	 *
19 22
 	 * @param response
20 23
 	 * @param key
21 24
 	 * @param value
22 25
 	 * @param ifRemember 
23 26
 	 */
24 27
 	public static void set(HttpServletResponse response, String key, String value, boolean ifRemember) {
25
-		
26
-		int age = COOKIE_MAX_AGE;
27
-		if (ifRemember) {
28
-			age = COOKIE_MAX_AGE;
29
-		} else {
30
-			age = -1;
31
-		}
32
-		
33
-		Cookie cookie = new Cookie(key, value);
34
-		cookie.setMaxAge(age);				// Cookie过期时间,单位/秒
35
-		cookie.setPath(COOKIE_PATH);		// Cookie适用的路径
36
-		response.addCookie(cookie);
28
+		int age = ifRemember?COOKIE_MAX_AGE:-1;
29
+		set(response, key, value, null, COOKIE_PATH, age, true);
37 30
 	}
38 31
 
39 32
 	/**
40 33
 	 * 保存
34
+	 *
41 35
 	 * @param response
42 36
 	 * @param key
43 37
 	 * @param value
44 38
 	 * @param maxAge
45 39
 	 */
46
-	private static void set(HttpServletResponse response, 
47
-			String key, String value, int maxAge, String path) {
40
+	private static void set(HttpServletResponse response, String key, String value, String domain, String path, int maxAge, boolean isHttpOnly) {
48 41
 		Cookie cookie = new Cookie(key, value);
49
-		cookie.setMaxAge(maxAge);	// Cookie过期时间,单位/秒
50
-		cookie.setPath(path);		// Cookie适用的路径
42
+		if (domain != null) {
43
+			cookie.setDomain(domain);
44
+		}
45
+		cookie.setPath(path);
46
+		cookie.setMaxAge(maxAge);
47
+		cookie.setHttpOnly(isHttpOnly);
51 48
 		response.addCookie(cookie);
52 49
 	}
53 50
 	
54 51
 	/**
55 52
 	 * 查询value
53
+	 *
56 54
 	 * @param request
57 55
 	 * @param key
58 56
 	 * @return
@@ -67,6 +65,7 @@ public class CookieUtil {
67 65
 
68 66
 	/**
69 67
 	 * 查询Cookie
68
+	 *
70 69
 	 * @param request
71 70
 	 * @param key
72 71
 	 */
@@ -84,6 +83,7 @@ public class CookieUtil {
84 83
 	
85 84
 	/**
86 85
 	 * 删除Cookie
86
+	 *
87 87
 	 * @param request
88 88
 	 * @param response
89 89
 	 * @param key
@@ -91,7 +91,7 @@ public class CookieUtil {
91 91
 	public static void remove(HttpServletRequest request, HttpServletResponse response, String key) {
92 92
 		Cookie cookie = get(request, key);
93 93
 		if (cookie != null) {
94
-			set(response, key, "", 0, COOKIE_PATH);
94
+			set(response, key, "", null, COOKIE_PATH, 0, true);
95 95
 		}
96 96
 	}
97 97