Browse Source

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

xuxueli 8 years ago
parent
commit
badcf6e3cb

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

1101
 - 24、Log地址格式兼容,支持非"/"结尾路径配置;
1101
 - 24、Log地址格式兼容,支持非"/"结尾路径配置;
1102
 - 25、底层系统日志级别规范调整,清理遗留代码;
1102
 - 25、底层系统日志级别规范调整,清理遗留代码;
1103
 - 26、建表SQL优化,支持同步创建制定编码的库和表;
1103
 - 26、建表SQL优化,支持同步创建制定编码的库和表;
1104
-- 27、系统安全性优化,登陆Token写Cookie时进行MD5加密;
1104
+- 27、系统安全性优化,登陆Token写Cookie时进行MD5加密,同时Cookie启用HttpOnly
1105
 
1105
 
1106
 
1106
 
1107
 ### TODO LIST
1107
 ### TODO LIST

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

17
  * @author xuxueli 2015-12-12 18:09:04
17
  * @author xuxueli 2015-12-12 18:09:04
18
  */
18
  */
19
 public class PermissionInterceptor extends HandlerInterceptorAdapter {
19
 public class PermissionInterceptor extends HandlerInterceptorAdapter {
20
-	
20
+
21
+
21
 	public static final String LOGIN_IDENTITY_KEY = "XXL_JOB_LOGIN_IDENTITY";
22
 	public static final String LOGIN_IDENTITY_KEY = "XXL_JOB_LOGIN_IDENTITY";
22
 	public static final String LOGIN_IDENTITY_TOKEN;
23
 	public static final String LOGIN_IDENTITY_TOKEN;
23
     static {
24
     static {
30
 
31
 
31
 		LOGIN_IDENTITY_TOKEN = tokenTmp;
32
 		LOGIN_IDENTITY_TOKEN = tokenTmp;
32
     }
33
     }
33
-	
34
+
35
+
36
+
34
 	public static boolean login(HttpServletResponse response, String username, String password, boolean ifRemember){
37
 	public static boolean login(HttpServletResponse response, String username, String password, boolean ifRemember){
35
 
38
 
36
     	// login token
39
     	// login token
56
 		return true;
59
 		return true;
57
 	}
60
 	}
58
 
61
 
62
+
63
+
59
 	@Override
64
 	@Override
60
 	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
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
 
6
 
7
 /**
7
 /**
8
  * Cookie.Util
8
  * Cookie.Util
9
+ *
9
  * @author xuxueli 2015-12-12 18:01:06
10
  * @author xuxueli 2015-12-12 18:01:06
10
  */
11
  */
11
 public class CookieUtil {
12
 public class CookieUtil {
13
+
12
 	// 默认缓存时间,单位/秒, 2H
14
 	// 默认缓存时间,单位/秒, 2H
13
 	private static final int COOKIE_MAX_AGE = 60 * 60 * 2;
15
 	private static final int COOKIE_MAX_AGE = 60 * 60 * 2;
14
 	// 保存路径,根路径
16
 	// 保存路径,根路径
16
 	
18
 	
17
 	/**
19
 	/**
18
 	 * 保存
20
 	 * 保存
21
+	 *
19
 	 * @param response
22
 	 * @param response
20
 	 * @param key
23
 	 * @param key
21
 	 * @param value
24
 	 * @param value
22
 	 * @param ifRemember 
25
 	 * @param ifRemember 
23
 	 */
26
 	 */
24
 	public static void set(HttpServletResponse response, String key, String value, boolean ifRemember) {
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
 	 * @param response
35
 	 * @param response
42
 	 * @param key
36
 	 * @param key
43
 	 * @param value
37
 	 * @param value
44
 	 * @param maxAge
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
 		Cookie cookie = new Cookie(key, value);
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
 		response.addCookie(cookie);
48
 		response.addCookie(cookie);
52
 	}
49
 	}
53
 	
50
 	
54
 	/**
51
 	/**
55
 	 * 查询value
52
 	 * 查询value
53
+	 *
56
 	 * @param request
54
 	 * @param request
57
 	 * @param key
55
 	 * @param key
58
 	 * @return
56
 	 * @return
67
 
65
 
68
 	/**
66
 	/**
69
 	 * 查询Cookie
67
 	 * 查询Cookie
68
+	 *
70
 	 * @param request
69
 	 * @param request
71
 	 * @param key
70
 	 * @param key
72
 	 */
71
 	 */
84
 	
83
 	
85
 	/**
84
 	/**
86
 	 * 删除Cookie
85
 	 * 删除Cookie
86
+	 *
87
 	 * @param request
87
 	 * @param request
88
 	 * @param response
88
 	 * @param response
89
 	 * @param key
89
 	 * @param key
91
 	public static void remove(HttpServletRequest request, HttpServletResponse response, String key) {
91
 	public static void remove(HttpServletRequest request, HttpServletResponse response, String key) {
92
 		Cookie cookie = get(request, key);
92
 		Cookie cookie = get(request, key);
93
 		if (cookie != null) {
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