001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019 package org.apache.shiro.web.mgt;
020
021 import org.apache.shiro.mgt.DefaultSubjectFactory;
022 import org.apache.shiro.mgt.SecurityManager;
023 import org.apache.shiro.session.Session;
024 import org.apache.shiro.subject.PrincipalCollection;
025 import org.apache.shiro.subject.Subject;
026 import org.apache.shiro.subject.SubjectContext;
027 import org.apache.shiro.web.subject.WebSubjectContext;
028 import org.apache.shiro.web.subject.support.WebDelegatingSubject;
029
030 import javax.servlet.ServletRequest;
031 import javax.servlet.ServletResponse;
032
033 /**
034 * A {@code SubjectFactory} implementation that creates {@link WebDelegatingSubject} instances.
035 * <p/>
036 * {@code WebDelegatingSubject} instances are required if Request/Response objects are to be maintained across
037 * threads when using the {@code Subject} {@link Subject#associateWith(java.util.concurrent.Callable) createCallable}
038 * and {@link Subject#associateWith(Runnable) createRunnable} methods.
039 *
040 * @see #newSubjectInstance(org.apache.shiro.subject.PrincipalCollection, boolean, String, org.apache.shiro.session.Session, org.apache.shiro.mgt.SecurityManager)
041 * @since 1.0
042 */
043 public class DefaultWebSubjectFactory extends DefaultSubjectFactory {
044
045 public DefaultWebSubjectFactory() {
046 super();
047 }
048
049 public Subject createSubject(SubjectContext context) {
050 if (!(context instanceof WebSubjectContext)) {
051 return super.createSubject(context);
052 }
053 WebSubjectContext wsc = (WebSubjectContext) context;
054 SecurityManager securityManager = wsc.resolveSecurityManager();
055 Session session = wsc.resolveSession();
056 PrincipalCollection principals = wsc.resolvePrincipals();
057 boolean authenticated = wsc.resolveAuthenticated();
058 String host = wsc.resolveHost();
059 ServletRequest request = wsc.resolveServletRequest();
060 ServletResponse response = wsc.resolveServletResponse();
061 return newSubjectInstance(principals, authenticated, host, session, request, response, securityManager);
062 }
063
064 protected Subject newSubjectInstance(PrincipalCollection principals, boolean authenticated,
065 String host, Session session,
066 ServletRequest request, ServletResponse response,
067 SecurityManager securityManager) {
068 return new WebDelegatingSubject(principals, authenticated, host, session, request, response, securityManager);
069 }
070 }