报错为 EhCacheManager 的bean,存在重名的情况。
原因是2.5版本后,系统只支持singlton模式
解决办法
<properties> <java.version>1.8</java.version> <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version> <ehcache.version>2.4.8</ehcache.version> </properties>
<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>${ehcache.version}</version> </dependency>
编写config类
package cn.taotao.config; import java.io.IOException; import java.util.LinkedHashMap; import javax.inject.Qualifier; import javax.inject.Singleton; import org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy; import org.apache.shiro.authc.pam.ModularRealmAuthenticator; import org.apache.shiro.cache.Cache; import org.apache.shiro.cache.CacheManager; import org.apache.shiro.cache.ehcache.EhCacheManager; import org.apache.shiro.io.ResourceUtils; import org.apache.shiro.mgt.RememberMeManager; import org.apache.shiro.spring.LifecycleBeanPostProcessor; import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor; import org.apache.shiro.spring.web.ShiroFilterFactoryBean; import org.apache.shiro.web.mgt.DefaultWebSecurityManager; import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import cn.taotao.shiro.factory.FilterChainDefinitionMapBuilder; import cn.taotao.shiro.realms.ShiroRealm; import cn.taotao.shiro.service.MyHashedCredentialsMatcher; @Configuration public class ShiroConfig { private EhCacheManager cacheManager = null; @Bean(name = "cacheManager") public EhCacheManager cacheManager() { if(cacheManager==null) { cacheManager = new EhCacheManager(); cacheManager.setCacheManagerConfigFile("classpath:ehcache-taotao.xml"); } return cacheManager; } @Bean public ModularRealmAuthenticator authenticator() { ModularRealmAuthenticator authenticator = new ModularRealmAuthenticator(); authenticator.setAuthenticationStrategy(new AtLeastOneSuccessfulStrategy()); return authenticator; } @Bean public ShiroRealm jdbcRealm(CacheManager cacheManager) { ShiroRealm jdbRealm = new ShiroRealm(); // MyHashedCredentialsMatcher credentialsMatcher = null ; jdbRealm.setCredentialsMatcher(credentialsMatcher(cacheManager)); return jdbRealm; } @Bean public MyHashedCredentialsMatcher credentialsMatcher(CacheManager cacheManager) { // CacheManager cacheManager = null; MyHashedCredentialsMatcher credentialsMatcher = new MyHashedCredentialsMatcher((org.apache.shiro.cache.CacheManager) cacheManager); credentialsMatcher.setHashIterations(1024); credentialsMatcher.setHashAlgorithmName("MD5"); credentialsMatcher.setStoredCredentialsHexEncoded(true); return credentialsMatcher; } @Bean public DefaultWebSecurityManager securityManager(CacheManager cacheManager) { DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); // securityManager.setCacheManager(cacheManager()); securityManager.setAuthenticator(authenticator()); securityManager.setRealm(jdbcRealm(cacheManager)); RememberMeManager rememberMeManager = null; // rememberMeManager. securityManager.setRememberMeManager(rememberMeManager); return securityManager; } @Bean public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() { return new LifecycleBeanPostProcessor(); } @Bean public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() { DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator(); // defaultAdvisorAutoProxyCreator. return defaultAdvisorAutoProxyCreator; } @Bean public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(CacheManager cacheManager) { AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor(); authorizationAttributeSourceAdvisor.setSecurityManager(securityManager(cacheManager)); return authorizationAttributeSourceAdvisor; } @Bean(value = "shiroFilter") public ShiroFilterFactoryBean shiroFilter(FilterChainDefinitionMapBuilder filterChainDefinitionMapBuilder, CacheManager cacheManager) { ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean(); shiroFilter.setSecurityManager(securityManager(cacheManager)); shiroFilter.setLoginUrl("/admin116/login"); shiroFilter.setSuccessUrl("/admin116.jsp"); shiroFilter.setUnauthorizedUrl("/unauthorized.jsp"); shiroFilter.setFilterChainDefinitionMap(filterChainDefinitionMapBuilder.buildFilterChainDefinitionMap()); System.out.println(); return shiroFilter; } }
使用redis做缓存
引入pom
<dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> <version>2.4.2</version> </dependency>
配置过滤器
在web.xml 文件中
<filter> <filter-name>springSessionRepositoryFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSessionRepositoryFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
这个过滤器,要放在最先的位置,在所有过滤器之前。
就可以了。无需再多配置。