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.spring.config;
020
021import org.apache.shiro.authc.Authenticator;
022import org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy;
023import org.apache.shiro.authc.pam.AuthenticationStrategy;
024import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
025import org.apache.shiro.authz.Authorizer;
026import org.apache.shiro.authz.ModularRealmAuthorizer;
027import org.apache.shiro.authz.permission.PermissionResolver;
028import org.apache.shiro.authz.permission.RolePermissionResolver;
029import org.apache.shiro.cache.CacheManager;
030import org.apache.shiro.config.Ini;
031import org.apache.shiro.event.EventBus;
032
033import org.apache.shiro.mgt.DefaultSecurityManager;
034import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
035import org.apache.shiro.mgt.DefaultSubjectDAO;
036import org.apache.shiro.mgt.DefaultSubjectFactory;
037import org.apache.shiro.mgt.RememberMeManager;
038import org.apache.shiro.mgt.SessionStorageEvaluator;
039import org.apache.shiro.mgt.SessionsSecurityManager;
040import org.apache.shiro.mgt.SubjectDAO;
041import org.apache.shiro.mgt.SubjectFactory;
042import org.apache.shiro.realm.Realm;
043import org.apache.shiro.realm.text.IniRealm;
044import org.apache.shiro.session.mgt.DefaultSessionManager;
045import org.apache.shiro.session.mgt.SessionFactory;
046import org.apache.shiro.session.mgt.SessionManager;
047import org.apache.shiro.session.mgt.SimpleSessionFactory;
048import org.apache.shiro.session.mgt.eis.MemorySessionDAO;
049import org.apache.shiro.session.mgt.eis.SessionDAO;
050import org.springframework.beans.factory.annotation.Autowired;
051import org.springframework.beans.factory.annotation.Value;
052
053import java.util.List;
054
055/**
056 * @since 1.4.0
057 */
058public class AbstractShiroConfiguration {
059
060    @Autowired(required = false)
061    protected CacheManager cacheManager;
062
063    @Autowired(required = false)
064    protected RolePermissionResolver rolePermissionResolver;
065
066    @Autowired(required = false)
067    protected PermissionResolver permissionResolver;
068
069    @Autowired
070    protected EventBus eventBus;
071
072    @Value("#{ @environment['shiro.sessionManager.deleteInvalidSessions'] ?: true }")
073    protected boolean sessionManagerDeleteInvalidSessions;
074
075
076    protected SessionsSecurityManager securityManager(List<Realm> realms) {
077        SessionsSecurityManager securityManager = createSecurityManager();
078        securityManager.setAuthenticator(authenticator());
079        securityManager.setAuthorizer(authorizer());
080        securityManager.setRealms(realms);
081        securityManager.setSessionManager(sessionManager());
082        securityManager.setEventBus(eventBus);
083
084        if (cacheManager != null) {
085            securityManager.setCacheManager(cacheManager);
086        }
087
088        return securityManager;
089    }
090
091    protected SessionManager sessionManager() {
092        DefaultSessionManager sessionManager = new DefaultSessionManager();
093        sessionManager.setSessionDAO(sessionDAO());
094        sessionManager.setSessionFactory(sessionFactory());
095        sessionManager.setDeleteInvalidSessions(sessionManagerDeleteInvalidSessions);
096        return sessionManager;
097    }
098
099
100    protected SessionsSecurityManager createSecurityManager() {
101        DefaultSecurityManager securityManager = new DefaultSecurityManager();
102        securityManager.setSubjectDAO(subjectDAO());
103        securityManager.setSubjectFactory(subjectFactory());
104
105        RememberMeManager rememberMeManager = rememberMeManager();
106        if (rememberMeManager != null) {
107            securityManager.setRememberMeManager(rememberMeManager);
108        }
109
110        return securityManager;
111    }
112
113    protected RememberMeManager rememberMeManager() {
114        return null;
115    }
116
117    protected SubjectDAO subjectDAO() {
118        DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO();
119        subjectDAO.setSessionStorageEvaluator(sessionStorageEvaluator());
120        return subjectDAO;
121    }
122
123    protected SessionStorageEvaluator sessionStorageEvaluator() {
124        return new DefaultSessionStorageEvaluator();
125    }
126
127    protected SubjectFactory subjectFactory() {
128        return new DefaultSubjectFactory();
129    }
130
131
132    protected SessionFactory sessionFactory() {
133        return new SimpleSessionFactory();
134    }
135
136    protected SessionDAO sessionDAO() {
137        return new MemorySessionDAO();
138    }
139
140    protected Authorizer authorizer() {
141        ModularRealmAuthorizer authorizer = new ModularRealmAuthorizer();
142
143        if (permissionResolver != null) {
144            authorizer.setPermissionResolver(permissionResolver);
145        }
146
147        if (rolePermissionResolver != null) {
148            authorizer.setRolePermissionResolver(rolePermissionResolver);
149        }
150
151        return authorizer;
152    }
153
154    protected AuthenticationStrategy authenticationStrategy() {
155        return new AtLeastOneSuccessfulStrategy();
156    }
157
158    protected Authenticator authenticator() {
159        ModularRealmAuthenticator authenticator = new ModularRealmAuthenticator();
160        authenticator.setAuthenticationStrategy(authenticationStrategy());
161        return authenticator;
162    }
163
164    protected Realm iniRealmFromLocation(String iniLocation) {
165        Ini ini = Ini.fromResourcePath(iniLocation);
166        return new IniRealm(ini);
167    }
168}