View Javadoc
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  import org.apache.shiro.web.subject.WebSubject;
33  
34  /**
35   * A {@code SubjectFactory} implementation that creates {@link WebDelegatingSubject} instances.
36   * <p/>
37   * {@code WebDelegatingSubject} instances are required if Request/Response objects are to be maintained across
38   * threads when using the {@code Subject} {@link Subject#associateWith(java.util.concurrent.Callable) createCallable}
39   * and {@link Subject#associateWith(Runnable) createRunnable} methods.
40   *
41   * @since 1.0
42   */
43  public class DefaultWebSubjectFactory extends DefaultSubjectFactory {
44  
45      public DefaultWebSubjectFactory() {
46          super();
47      }
48  
49      public Subject createSubject(SubjectContext context) {
50          //SHIRO-646
51          //Check if the existing subject is NOT a WebSubject. If it isn't, then call super.createSubject instead.
52          //Creating a WebSubject from a non-web Subject will cause the ServletRequest and ServletResponse to be null, which wil fail when creating a session.
53          boolean isNotBasedOnWebSubject = context.getSubject() != null && !(context.getSubject() instanceof WebSubject);
54          if (!(context instanceof WebSubjectContext) || isNotBasedOnWebSubject) {
55              return super.createSubject(context);
56          }
57          WebSubjectContext/../../org/apache/shiro/web/subject/WebSubjectContext.html#WebSubjectContext">WebSubjectContext wsc = (WebSubjectContext) context;
58          SecurityManager securityManager = wsc.resolveSecurityManager();
59          Session session = wsc.resolveSession();
60          boolean sessionEnabled = wsc.isSessionCreationEnabled();
61          PrincipalCollection principals = wsc.resolvePrincipals();
62          boolean authenticated = wsc.resolveAuthenticated();
63          String host = wsc.resolveHost();
64          ServletRequest request = wsc.resolveServletRequest();
65          ServletResponse response = wsc.resolveServletResponse();
66  
67          return new WebDelegatingSubject(principals, authenticated, host, session, sessionEnabled,
68                  request, response, securityManager);
69      }
70  
71      /**
72       * @deprecated since 1.2 - override {@link #createSubject(org.apache.shiro.subject.SubjectContext)} directly if you
73       *             need to instantiate a custom {@link Subject} class.
74       */
75      @Deprecated
76      protected Subject newSubjectInstance(PrincipalCollection principals, boolean authenticated,
77                                           String host, Session session,
78                                           ServletRequest request, ServletResponse response,
79                                           SecurityManager securityManager) {
80          return new WebDelegatingSubject(principals, authenticated, host, session, true,
81                  request, response, securityManager);
82      }
83  }