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 */
019package org.apache.shiro.web.mgt;
020
021import org.apache.shiro.mgt.DefaultSubjectFactory;
022import org.apache.shiro.mgt.SecurityManager;
023import org.apache.shiro.session.Session;
024import org.apache.shiro.subject.PrincipalCollection;
025import org.apache.shiro.subject.Subject;
026import org.apache.shiro.subject.SubjectContext;
027import org.apache.shiro.web.subject.WebSubjectContext;
028import org.apache.shiro.web.subject.support.WebDelegatingSubject;
029
030import javax.servlet.ServletRequest;
031import 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 * @since 1.0
041 */
042public class DefaultWebSubjectFactory extends DefaultSubjectFactory {
043
044    public DefaultWebSubjectFactory() {
045        super();
046    }
047
048    public Subject createSubject(SubjectContext context) {
049        if (!(context instanceof WebSubjectContext)) {
050            return super.createSubject(context);
051        }
052        WebSubjectContext wsc = (WebSubjectContext) context;
053        SecurityManager securityManager = wsc.resolveSecurityManager();
054        Session session = wsc.resolveSession();
055        boolean sessionEnabled = wsc.isSessionCreationEnabled();
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
062        return new WebDelegatingSubject(principals, authenticated, host, session, sessionEnabled,
063                request, response, securityManager);
064    }
065
066    /**
067     * @deprecated since 1.2 - override {@link #createSubject(org.apache.shiro.subject.SubjectContext)} directly if you
068     *             need to instantiate a custom {@link Subject} class.
069     */
070    @Deprecated
071    protected Subject newSubjectInstance(PrincipalCollection principals, boolean authenticated,
072                                         String host, Session session,
073                                         ServletRequest request, ServletResponse response,
074                                         SecurityManager securityManager) {
075        return new WebDelegatingSubject(principals, authenticated, host, session, true,
076                request, response, securityManager);
077    }
078}