1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.shiro.web.mgt;
20
21 import org.apache.shiro.mgt.DefaultSubjectFactory;
22 import org.apache.shiro.mgt.SecurityManager;
23 import org.apache.shiro.session.Session;
24 import org.apache.shiro.subject.PrincipalCollection;
25 import org.apache.shiro.subject.Subject;
26 import org.apache.shiro.subject.SubjectContext;
27 import org.apache.shiro.web.subject.WebSubjectContext;
28 import org.apache.shiro.web.subject.support.WebDelegatingSubject;
29
30 import javax.servlet.ServletRequest;
31 import javax.servlet.ServletResponse;
32
33 import org.apache.shiro.web.subject.WebSubject;
34
35 /**
36 * A {@code SubjectFactory} implementation that creates {@link WebDelegatingSubject} instances.
37 * <p/>
38 * {@code WebDelegatingSubject} instances are required if Request/Response objects are to be maintained across
39 * threads when using the {@code Subject} {@link Subject#associateWith(java.util.concurrent.Callable) createCallable}
40 * and {@link Subject#associateWith(Runnable) createRunnable} methods.
41 *
42 * @since 1.0
43 */
44 public class DefaultWebSubjectFactory extends DefaultSubjectFactory {
45
46 public DefaultWebSubjectFactory() {
47 super();
48 }
49
50 public Subject createSubject(SubjectContext context) {
51 //SHIRO-646
52 //Check if the existing subject is NOT a WebSubject. If it isn't, then call super.createSubject instead.
53 //Creating a WebSubject from a non-web Subject will cause the ServletRequest and ServletResponse to be null,
54 // which wil fail when creating a session.
55 boolean isNotBasedOnWebSubject = context.getSubject() != null && !(context.getSubject() instanceof WebSubject);
56 if (!(context instanceof WebSubjectContext) || isNotBasedOnWebSubject) {
57 return super.createSubject(context);
58 }
59 WebSubjectContext wsc = (WebSubjectContext) context;
60 SecurityManager securityManager = wsc.resolveSecurityManager();
61 Session session = wsc.resolveSession();
62 boolean sessionEnabled = wsc.isSessionCreationEnabled();
63 PrincipalCollection principals = wsc.resolvePrincipals();
64 boolean authenticated = wsc.resolveAuthenticated();
65 String host = wsc.resolveHost();
66 ServletRequest request = wsc.resolveServletRequest();
67 ServletResponse response = wsc.resolveServletResponse();
68
69 return new WebDelegatingSubject(principals, authenticated, host, session, sessionEnabled,
70 request, response, securityManager);
71 }
72
73 /**
74 * @deprecated since 1.2 - override {@link #createSubject(org.apache.shiro.subject.SubjectContext)} directly if you
75 * need to instantiate a custom {@link Subject} class.
76 */
77 @Deprecated
78 protected Subject newSubjectInstance(PrincipalCollection principals, boolean authenticated,
79 String host, Session session,
80 ServletRequest request, ServletResponse response,
81 SecurityManager securityManager) {
82 return new WebDelegatingSubject(principals, authenticated, host, session, true,
83 request, response, securityManager);
84 }
85 }