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