|
@@ -1,80 +1,87 @@
|
1
|
|
-package com.yingying.tourist.request;
|
2
|
|
-
|
3
|
|
-import com.alibaba.fastjson.JSONObject;
|
4
|
|
-import com.yingying.tourist.business.BusinessConstant;
|
5
|
|
-import com.yingying.tourist.business.BusinessYml;
|
6
|
|
-import com.yingying.tourist.common.UrlUtils;
|
7
|
|
-import lombok.extern.slf4j.Slf4j;
|
8
|
|
-import org.apache.commons.lang3.StringUtils;
|
9
|
|
-import org.springframework.core.annotation.Order;
|
10
|
|
-import org.springframework.stereotype.Component;
|
11
|
|
-import org.springframework.web.util.WebUtils;
|
12
|
|
-
|
13
|
|
-import javax.annotation.Resource;
|
14
|
|
-import javax.servlet.*;
|
15
|
|
-import javax.servlet.annotation.WebFilter;
|
16
|
|
-import javax.servlet.http.Cookie;
|
17
|
|
-import javax.servlet.http.HttpServletRequest;
|
18
|
|
-import javax.servlet.http.HttpServletResponse;
|
19
|
|
-import java.io.IOException;
|
20
|
|
-import java.util.UUID;
|
21
|
|
-
|
22
|
|
-@Component
|
23
|
|
-@Order(2)
|
24
|
|
-@WebFilter(filterName = "SessionFilter", urlPatterns = "/*")
|
25
|
|
-@Slf4j
|
26
|
|
-public class SessionFilter implements Filter {
|
27
|
|
-
|
28
|
|
- @Resource
|
29
|
|
- private BusinessYml businessYml;
|
30
|
|
-
|
31
|
|
- @Override
|
32
|
|
- public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
33
|
|
- // 检查sessionId的cookie是否存在,不存在即为新连接,需要初始化sessionId到cookie中
|
34
|
|
- checkRemoteSession((HttpServletRequest) request, (HttpServletResponse) response);
|
35
|
|
- chain.doFilter(request, response);
|
36
|
|
- }
|
37
|
|
-
|
38
|
|
- @Override
|
39
|
|
- public void destroy() {
|
40
|
|
- }
|
41
|
|
-
|
42
|
|
- @Override
|
43
|
|
- public void init(FilterConfig filterConfig) throws ServletException {
|
44
|
|
-
|
45
|
|
- }
|
46
|
|
-
|
47
|
|
- private void checkRemoteSession(HttpServletRequest request, HttpServletResponse response) {
|
48
|
|
- String sessionKey = null;
|
49
|
|
- String token = request.getParameter("token");
|
50
|
|
- log.info("token------>>>first:[{}]",token);
|
51
|
|
- if (StringUtils.isEmpty(token)) {
|
52
|
|
- String payload = UrlUtils.getRequestPayload(request);
|
53
|
|
- if (!StringUtils.isEmpty(payload)) {
|
54
|
|
- JSONObject jsonObject = JSONObject.parseObject(payload);
|
55
|
|
- token = jsonObject.getString("token");
|
56
|
|
- }
|
57
|
|
- }
|
58
|
|
-
|
59
|
|
- if (StringUtils.isNotEmpty(token)) {
|
60
|
|
- sessionKey = token;
|
61
|
|
- log.info("sessionKey------>>>second:[{}]",sessionKey);
|
62
|
|
- } else {
|
63
|
|
- Cookie sessionKeyCookie = WebUtils.getCookie(request, SessionAuthorization.SESSION_KEY_COOKIE_NAME);
|
64
|
|
- if (sessionKeyCookie == null) {
|
65
|
|
- sessionKey = UUID.randomUUID().toString();
|
66
|
|
- sessionKeyCookie = new Cookie(SessionAuthorization.SESSION_KEY_COOKIE_NAME, sessionKey);
|
67
|
|
- } else {
|
68
|
|
- sessionKey = sessionKeyCookie.getValue();
|
69
|
|
- }
|
70
|
|
- log.info("sessionKey------>>>third:[{}]",sessionKey);
|
71
|
|
- sessionKeyCookie.setPath("/");
|
72
|
|
- sessionKeyCookie.setDomain(businessYml.getCookieDomain());
|
73
|
|
- sessionKeyCookie.setMaxAge(SessionAuthorization.DEFAULT_SESSION_COOKIE_EXPIRE);
|
74
|
|
- sessionKeyCookie.setHttpOnly(true);
|
75
|
|
- response.addCookie(sessionKeyCookie);
|
76
|
|
- }
|
77
|
|
- RequestSessionKey.putSessionKey(sessionKey);
|
78
|
|
- }
|
79
|
|
-
|
|
1
|
+package com.yingying.tourist.request;
|
|
2
|
+
|
|
3
|
+import java.io.IOException;
|
|
4
|
+import java.util.UUID;
|
|
5
|
+
|
|
6
|
+import javax.annotation.Resource;
|
|
7
|
+import javax.servlet.Filter;
|
|
8
|
+import javax.servlet.FilterChain;
|
|
9
|
+import javax.servlet.FilterConfig;
|
|
10
|
+import javax.servlet.ServletException;
|
|
11
|
+import javax.servlet.ServletRequest;
|
|
12
|
+import javax.servlet.ServletResponse;
|
|
13
|
+import javax.servlet.annotation.WebFilter;
|
|
14
|
+import javax.servlet.http.Cookie;
|
|
15
|
+import javax.servlet.http.HttpServletRequest;
|
|
16
|
+import javax.servlet.http.HttpServletResponse;
|
|
17
|
+
|
|
18
|
+import org.apache.commons.lang3.StringUtils;
|
|
19
|
+import org.springframework.core.annotation.Order;
|
|
20
|
+import org.springframework.stereotype.Component;
|
|
21
|
+import org.springframework.web.util.WebUtils;
|
|
22
|
+
|
|
23
|
+import com.alibaba.fastjson.JSONObject;
|
|
24
|
+import com.yingying.tourist.business.BusinessYml;
|
|
25
|
+import com.yingying.tourist.common.UrlUtils;
|
|
26
|
+
|
|
27
|
+import lombok.extern.slf4j.Slf4j;
|
|
28
|
+
|
|
29
|
+@Component
|
|
30
|
+@Order(2)
|
|
31
|
+@WebFilter(filterName = "SessionFilter", urlPatterns = "/*")
|
|
32
|
+@Slf4j
|
|
33
|
+public class SessionFilter implements Filter {
|
|
34
|
+
|
|
35
|
+ @Resource
|
|
36
|
+ private BusinessYml businessYml;
|
|
37
|
+
|
|
38
|
+ @Override
|
|
39
|
+ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
|
40
|
+ // 检查sessionId的cookie是否存在,不存在即为新连接,需要初始化sessionId到cookie中
|
|
41
|
+ checkRemoteSession((HttpServletRequest) request, (HttpServletResponse) response);
|
|
42
|
+ chain.doFilter(request, response);
|
|
43
|
+ }
|
|
44
|
+
|
|
45
|
+ @Override
|
|
46
|
+ public void destroy() {
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+ @Override
|
|
50
|
+ public void init(FilterConfig filterConfig) throws ServletException {
|
|
51
|
+
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ private void checkRemoteSession(HttpServletRequest request, HttpServletResponse response) {
|
|
55
|
+ String sessionKey = null;
|
|
56
|
+ String token = request.getParameter("token");
|
|
57
|
+ log.info("token------>>>first:[{}]",token);
|
|
58
|
+ if (StringUtils.isEmpty(token)) {
|
|
59
|
+ String payload = UrlUtils.getRequestPayload(request);
|
|
60
|
+ if (!StringUtils.isEmpty(payload)) {
|
|
61
|
+ JSONObject jsonObject = JSONObject.parseObject(payload);
|
|
62
|
+ token = jsonObject.getString("token");
|
|
63
|
+ }
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ if (StringUtils.isNotEmpty(token)) {
|
|
67
|
+ sessionKey = token;
|
|
68
|
+ log.info("sessionKey------>>>second:[{}]",sessionKey);
|
|
69
|
+ } else {
|
|
70
|
+ Cookie sessionKeyCookie = WebUtils.getCookie(request, SessionAuthorization.SESSION_KEY_COOKIE_NAME);
|
|
71
|
+ if (sessionKeyCookie == null) {
|
|
72
|
+ sessionKey = UUID.randomUUID().toString();
|
|
73
|
+ sessionKeyCookie = new Cookie(SessionAuthorization.SESSION_KEY_COOKIE_NAME, sessionKey);
|
|
74
|
+ } else {
|
|
75
|
+ sessionKey = sessionKeyCookie.getValue();
|
|
76
|
+ }
|
|
77
|
+ log.info("sessionKey------>>>third:[{}]",sessionKey);
|
|
78
|
+ sessionKeyCookie.setPath("/");
|
|
79
|
+ sessionKeyCookie.setDomain(businessYml.getCookieDomain());
|
|
80
|
+ sessionKeyCookie.setMaxAge(SessionAuthorization.DEFAULT_SESSION_COOKIE_EXPIRE);
|
|
81
|
+ sessionKeyCookie.setHttpOnly(true);
|
|
82
|
+ response.addCookie(sessionKeyCookie);
|
|
83
|
+ }
|
|
84
|
+ RequestSessionKey.putSessionKey(sessionKey);
|
|
85
|
+ }
|
|
86
|
+
|
80
|
87
|
}
|