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