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.spring.config;
20  
21  import org.apache.shiro.authc.Authenticator;
22  import org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy;
23  import org.apache.shiro.authc.pam.AuthenticationStrategy;
24  import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
25  import org.apache.shiro.authz.Authorizer;
26  import org.apache.shiro.authz.ModularRealmAuthorizer;
27  import org.apache.shiro.authz.permission.PermissionResolver;
28  import org.apache.shiro.authz.permission.RolePermissionResolver;
29  import org.apache.shiro.cache.CacheManager;
30  import org.apache.shiro.config.Ini;
31  import org.apache.shiro.event.EventBus;
32  import org.apache.shiro.mgt.*;
33  import org.apache.shiro.realm.Realm;
34  import org.apache.shiro.realm.text.IniRealm;
35  import org.apache.shiro.session.mgt.DefaultSessionManager;
36  import org.apache.shiro.session.mgt.SessionFactory;
37  import org.apache.shiro.session.mgt.SessionManager;
38  import org.apache.shiro.session.mgt.SimpleSessionFactory;
39  import org.apache.shiro.session.mgt.eis.MemorySessionDAO;
40  import org.apache.shiro.session.mgt.eis.SessionDAO;
41  import org.springframework.beans.factory.annotation.Autowired;
42  import org.springframework.beans.factory.annotation.Value;
43  
44  import java.util.List;
45  
46  /**
47   * @since 1.4.0
48   */
49  public class AbstractShiroConfiguration {
50  
51      @Autowired(required = false)
52      protected CacheManager cacheManager;
53  
54      @Autowired(required = false)
55      protected RolePermissionResolver rolePermissionResolver;
56  
57      @Autowired(required = false)
58      protected PermissionResolver permissionResolver;
59  
60      @Autowired
61      protected EventBus eventBus;
62  
63      @Value("#{ @environment['shiro.sessionManager.deleteInvalidSessions'] ?: true }")
64      protected boolean sessionManagerDeleteInvalidSessions;
65  
66  
67      protected SessionsSecurityManager securityManager(List<Realm> realms) {
68          SessionsSecurityManager securityManager = createSecurityManager();
69          securityManager.setAuthenticator(authenticator());
70          securityManager.setAuthorizer(authorizer());
71          securityManager.setRealms(realms);
72          securityManager.setSessionManager(sessionManager());
73          securityManager.setEventBus(eventBus);
74  
75          if (cacheManager != null) {
76              securityManager.setCacheManager(cacheManager);
77          }
78  
79          return securityManager;
80      }
81  
82      protected SessionManager sessionManager() {
83          DefaultSessionManager sessionManager = new DefaultSessionManager();
84          sessionManager.setSessionDAO(sessionDAO());
85          sessionManager.setSessionFactory(sessionFactory());
86          sessionManager.setDeleteInvalidSessions(sessionManagerDeleteInvalidSessions);
87          return sessionManager;
88      }
89  
90  
91      protected SessionsSecurityManager createSecurityManager() {
92          DefaultSecurityManager securityManager = new DefaultSecurityManager();
93          securityManager.setSubjectDAO(subjectDAO());
94          securityManager.setSubjectFactory(subjectFactory());
95  
96          RememberMeManager rememberMeManager = rememberMeManager();
97          if (rememberMeManager != null) {
98              securityManager.setRememberMeManager(rememberMeManager);
99          }
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 }