1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.shiro.subject.support; |
20 | |
|
21 | |
import org.apache.shiro.SecurityUtils; |
22 | |
import org.apache.shiro.UnavailableSecurityManagerException; |
23 | |
import org.apache.shiro.authc.AuthenticationInfo; |
24 | |
import org.apache.shiro.authc.AuthenticationToken; |
25 | |
import org.apache.shiro.authc.HostAuthenticationToken; |
26 | |
import org.apache.shiro.mgt.SecurityManager; |
27 | |
import org.apache.shiro.session.Session; |
28 | |
import org.apache.shiro.subject.PrincipalCollection; |
29 | |
import org.apache.shiro.subject.Subject; |
30 | |
import org.apache.shiro.subject.SubjectContext; |
31 | |
import org.apache.shiro.util.CollectionUtils; |
32 | |
import org.apache.shiro.util.MapContext; |
33 | |
import org.apache.shiro.util.StringUtils; |
34 | |
import org.slf4j.Logger; |
35 | |
import org.slf4j.LoggerFactory; |
36 | |
|
37 | |
import java.io.Serializable; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public class DefaultSubjectContext extends MapContext implements SubjectContext { |
49 | |
|
50 | 1 | private static final String SECURITY_MANAGER = DefaultSubjectContext.class.getName() + ".SECURITY_MANAGER"; |
51 | |
|
52 | 1 | private static final String SESSION_ID = DefaultSubjectContext.class.getName() + ".SESSION_ID"; |
53 | |
|
54 | 1 | private static final String AUTHENTICATION_TOKEN = DefaultSubjectContext.class.getName() + ".AUTHENTICATION_TOKEN"; |
55 | |
|
56 | 1 | private static final String AUTHENTICATION_INFO = DefaultSubjectContext.class.getName() + ".AUTHENTICATION_INFO"; |
57 | |
|
58 | 1 | private static final String SUBJECT = DefaultSubjectContext.class.getName() + ".SUBJECT"; |
59 | |
|
60 | 1 | private static final String PRINCIPALS = DefaultSubjectContext.class.getName() + ".PRINCIPALS"; |
61 | |
|
62 | 1 | private static final String SESSION = DefaultSubjectContext.class.getName() + ".SESSION"; |
63 | |
|
64 | 1 | private static final String AUTHENTICATED = DefaultSubjectContext.class.getName() + ".AUTHENTICATED"; |
65 | |
|
66 | 1 | private static final String HOST = DefaultSubjectContext.class.getName() + ".HOST"; |
67 | |
|
68 | 1 | public static final String SESSION_CREATION_ENABLED = DefaultSubjectContext.class.getName() + ".SESSION_CREATION_ENABLED"; |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | 1 | public static final String PRINCIPALS_SESSION_KEY = DefaultSubjectContext.class.getName() + "_PRINCIPALS_SESSION_KEY"; |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | 1 | public static final String AUTHENTICATED_SESSION_KEY = DefaultSubjectContext.class.getName() + "_AUTHENTICATED_SESSION_KEY"; |
79 | |
|
80 | 1 | private static final transient Logger log = LoggerFactory.getLogger(DefaultSubjectContext.class); |
81 | |
|
82 | |
public DefaultSubjectContext() { |
83 | 48 | super(); |
84 | 48 | } |
85 | |
|
86 | |
public DefaultSubjectContext(SubjectContext ctx) { |
87 | 46 | super(ctx); |
88 | 46 | } |
89 | |
|
90 | |
public SecurityManager getSecurityManager() { |
91 | 92 | return getTypedValue(SECURITY_MANAGER, SecurityManager.class); |
92 | |
} |
93 | |
|
94 | |
public void setSecurityManager(SecurityManager securityManager) { |
95 | 33 | nullSafePut(SECURITY_MANAGER, securityManager); |
96 | 33 | } |
97 | |
|
98 | |
public SecurityManager resolveSecurityManager() { |
99 | 92 | SecurityManager securityManager = getSecurityManager(); |
100 | 92 | if (securityManager == null) { |
101 | 30 | if (log.isDebugEnabled()) { |
102 | 30 | log.debug("No SecurityManager available in subject context map. " + |
103 | |
"Falling back to SecurityUtils.getSecurityManager() lookup."); |
104 | |
} |
105 | |
try { |
106 | 30 | securityManager = SecurityUtils.getSecurityManager(); |
107 | 4 | } catch (UnavailableSecurityManagerException e) { |
108 | 4 | if (log.isDebugEnabled()) { |
109 | 4 | log.debug("No SecurityManager available via SecurityUtils. Heuristics exhausted.", e); |
110 | |
} |
111 | 26 | } |
112 | |
} |
113 | 92 | return securityManager; |
114 | |
} |
115 | |
|
116 | |
public Serializable getSessionId() { |
117 | 46 | return getTypedValue(SESSION_ID, Serializable.class); |
118 | |
} |
119 | |
|
120 | |
public void setSessionId(Serializable sessionId) { |
121 | 0 | nullSafePut(SESSION_ID, sessionId); |
122 | 0 | } |
123 | |
|
124 | |
public Subject getSubject() { |
125 | 282 | return getTypedValue(SUBJECT, Subject.class); |
126 | |
} |
127 | |
|
128 | |
public void setSubject(Subject subject) { |
129 | 17 | nullSafePut(SUBJECT, subject); |
130 | 17 | } |
131 | |
|
132 | |
public PrincipalCollection getPrincipals() { |
133 | 92 | return getTypedValue(PRINCIPALS, PrincipalCollection.class); |
134 | |
} |
135 | |
|
136 | |
public void setPrincipals(PrincipalCollection principals) { |
137 | 0 | if (!CollectionUtils.isEmpty(principals)) { |
138 | 0 | put(PRINCIPALS, principals); |
139 | |
} |
140 | 0 | } |
141 | |
|
142 | |
public PrincipalCollection resolvePrincipals() { |
143 | 92 | PrincipalCollection principals = getPrincipals(); |
144 | |
|
145 | 92 | if (CollectionUtils.isEmpty(principals)) { |
146 | |
|
147 | 92 | AuthenticationInfo info = getAuthenticationInfo(); |
148 | 92 | if (info != null) { |
149 | 34 | principals = info.getPrincipals(); |
150 | |
} |
151 | |
} |
152 | |
|
153 | 92 | if (CollectionUtils.isEmpty(principals)) { |
154 | 58 | Subject subject = getSubject(); |
155 | 58 | if (subject != null) { |
156 | 0 | principals = subject.getPrincipals(); |
157 | |
} |
158 | |
} |
159 | |
|
160 | 92 | if (CollectionUtils.isEmpty(principals)) { |
161 | |
|
162 | 58 | Session session = resolveSession(); |
163 | 58 | if (session != null) { |
164 | 0 | principals = (PrincipalCollection) session.getAttribute(PRINCIPALS_SESSION_KEY); |
165 | |
} |
166 | |
} |
167 | |
|
168 | 92 | return principals; |
169 | |
} |
170 | |
|
171 | |
|
172 | |
public Session getSession() { |
173 | 224 | return getTypedValue(SESSION, Session.class); |
174 | |
} |
175 | |
|
176 | |
public void setSession(Session session) { |
177 | 0 | nullSafePut(SESSION, session); |
178 | 0 | } |
179 | |
|
180 | |
public Session resolveSession() { |
181 | 224 | Session session = getSession(); |
182 | 224 | if (session == null) { |
183 | |
|
184 | 224 | Subject existingSubject = getSubject(); |
185 | 224 | if (existingSubject != null) { |
186 | 50 | session = existingSubject.getSession(false); |
187 | |
} |
188 | |
} |
189 | 224 | return session; |
190 | |
} |
191 | |
|
192 | |
public boolean isSessionCreationEnabled() { |
193 | 46 | Boolean val = getTypedValue(SESSION_CREATION_ENABLED, Boolean.class); |
194 | 46 | return val == null || val; |
195 | |
} |
196 | |
|
197 | |
public void setSessionCreationEnabled(boolean enabled) { |
198 | 0 | nullSafePut(SESSION_CREATION_ENABLED, enabled); |
199 | 0 | } |
200 | |
|
201 | |
public boolean isAuthenticated() { |
202 | 0 | Boolean authc = getTypedValue(AUTHENTICATED, Boolean.class); |
203 | 0 | return authc != null && authc; |
204 | |
} |
205 | |
|
206 | |
public void setAuthenticated(boolean authc) { |
207 | 17 | put(AUTHENTICATED, authc); |
208 | 17 | } |
209 | |
|
210 | |
public boolean resolveAuthenticated() { |
211 | 46 | Boolean authc = getTypedValue(AUTHENTICATED, Boolean.class); |
212 | 46 | if (authc == null) { |
213 | |
|
214 | |
|
215 | 29 | AuthenticationInfo info = getAuthenticationInfo(); |
216 | 29 | authc = info != null; |
217 | |
} |
218 | 46 | if (!authc) { |
219 | |
|
220 | 29 | Session session = resolveSession(); |
221 | 29 | if (session != null) { |
222 | 0 | Boolean sessionAuthc = (Boolean) session.getAttribute(AUTHENTICATED_SESSION_KEY); |
223 | 0 | authc = sessionAuthc != null && sessionAuthc; |
224 | |
} |
225 | |
} |
226 | |
|
227 | 46 | return authc; |
228 | |
} |
229 | |
|
230 | |
public AuthenticationInfo getAuthenticationInfo() { |
231 | 121 | return getTypedValue(AUTHENTICATION_INFO, AuthenticationInfo.class); |
232 | |
} |
233 | |
|
234 | |
public void setAuthenticationInfo(AuthenticationInfo info) { |
235 | 17 | nullSafePut(AUTHENTICATION_INFO, info); |
236 | 17 | } |
237 | |
|
238 | |
public AuthenticationToken getAuthenticationToken() { |
239 | 46 | return getTypedValue(AUTHENTICATION_TOKEN, AuthenticationToken.class); |
240 | |
} |
241 | |
|
242 | |
public void setAuthenticationToken(AuthenticationToken token) { |
243 | 17 | nullSafePut(AUTHENTICATION_TOKEN, token); |
244 | 17 | } |
245 | |
|
246 | |
public String getHost() { |
247 | 46 | return getTypedValue(HOST, String.class); |
248 | |
} |
249 | |
|
250 | |
public void setHost(String host) { |
251 | 0 | if (StringUtils.hasText(host)) { |
252 | 0 | put(HOST, host); |
253 | |
} |
254 | 0 | } |
255 | |
|
256 | |
public String resolveHost() { |
257 | 46 | String host = getHost(); |
258 | |
|
259 | 46 | if (host == null) { |
260 | |
|
261 | 46 | AuthenticationToken token = getAuthenticationToken(); |
262 | 46 | if (token instanceof HostAuthenticationToken) { |
263 | 17 | host = ((HostAuthenticationToken) token).getHost(); |
264 | |
} |
265 | |
} |
266 | |
|
267 | 46 | if (host == null) { |
268 | 45 | Session session = resolveSession(); |
269 | 45 | if (session != null) { |
270 | 0 | host = session.getHost(); |
271 | |
} |
272 | |
} |
273 | |
|
274 | 46 | return host; |
275 | |
} |
276 | |
} |