1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.shiro.web.session.mgt;
20
21 import org.apache.shiro.authz.AuthorizationException;
22 import org.apache.shiro.session.Session;
23 import org.apache.shiro.session.SessionException;
24 import org.apache.shiro.session.mgt.SessionContext;
25 import org.apache.shiro.session.mgt.SessionKey;
26 import org.apache.shiro.web.session.HttpServletSession;
27 import org.apache.shiro.web.util.WebUtils;
28
29 import javax.servlet.ServletRequest;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpSession;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public class ServletContainerSessionManager implements WebSessionManager {
55
56
57
58
59
60 public ServletContainerSessionManager() {
61 }
62
63 public Session start(SessionContext context) throws AuthorizationException {
64 return createSession(context);
65 }
66
67 public Session getSession(SessionKey key) throws SessionException {
68 if (!WebUtils.isHttp(key)) {
69 String msg = "SessionKey must be an HTTP compatible implementation.";
70 throw new IllegalArgumentException(msg);
71 }
72
73 HttpServletRequest request = WebUtils.getHttpRequest(key);
74
75 Session session = null;
76
77 HttpSession httpSession = request.getSession(false);
78 if (httpSession != null) {
79 session = createSession(httpSession, request.getRemoteHost());
80 }
81
82 return session;
83 }
84
85 private String getHost(SessionContext context) {
86 String host = context.getHost();
87 if (host == null) {
88 ServletRequest request = WebUtils.getRequest(context);
89 if (request != null) {
90 host = request.getRemoteHost();
91 }
92 }
93 return host;
94
95 }
96
97
98
99
100 protected Session createSession(SessionContext sessionContext) throws AuthorizationException {
101 if (!WebUtils.isHttp(sessionContext)) {
102 String msg = "SessionContext must be an HTTP compatible implementation.";
103 throw new IllegalArgumentException(msg);
104 }
105
106 HttpServletRequest request = WebUtils.getHttpRequest(sessionContext);
107
108 HttpSession httpSession = request.getSession();
109
110
111
112
113 String host = getHost(sessionContext);
114
115 return createSession(httpSession, host);
116 }
117
118 protected Session createSession(HttpSession httpSession, String host) {
119 return new HttpServletSession(httpSession, host);
120 }
121
122
123
124
125
126
127
128
129 public boolean isServletContainerSessions() {
130 return true;
131 }
132
133 }