为了方便,这里直接使用Spring Boot教程(十六):Spring Boot集成shiro章节的源码。
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-cacheartifactId>
dependency>
<dependency>
<groupId>net.sf.ehcachegroupId>
<artifactId>ehcacheartifactId>
dependency>
在resources
目录下,创ehcache.xml
配置文件,内容如下:
<ehcache updateCheck="false" dynamicConfig="false">
<diskStore path="java.io.tmpdir"/>
<cache name="authorizationCache"
maxEntriesLocalHeap="2000"
eternal="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="1800"
overflowToDisk="false"
statistics="true">
cache>
<cache name="authenticationCache"
maxEntriesLocalHeap="2000"
eternal="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="1800"
overflowToDisk="false"
statistics="true">
cache>
<defaultCache name="defaultCache"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
maxElementsOnDisk="100000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"/>
ehcache>
package com.songguoliang.springboot.shiro;
import com.songguoliang.springboot.entity.SysUser;
import com.songguoliang.springboot.service.SysPermissionService;
import com.songguoliang.springboot.service.SysUserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
/**
* @Description
* @Author sgl
* @Date 2018-06-11 17:07
*/
public class UserRealm extends AuthorizingRealm {
private static final Logger LOGGER = LoggerFactory.getLogger(UserRealm.class);
/**
* 注意此处需要添加@Lazy注解,否则SysUserService缓存注解、事务注解不生效
*/
@Autowired
@Lazy
private SysUserService sysUserService;
@Autowired
@Lazy
private SysPermissionService sysPermissionService;
public UserRealm(CacheManager cacheManager, CredentialsMatcher matcher) {
super(cacheManager, matcher);
}
//省略代码……
}
由于Spring和Shiro都各自维护了自己的Cache抽象,为防止UserRealm注入的service里缓存注解和事务注解失效,所以定义自己的CacheManager处理缓存。
ShiroSpringCacheManager类内容如下:
package com.songguoliang.springboot.shiro;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.cache.CacheManager;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @Description
* @Author sgl
* @Date 2018-06-05 11:56
*/
public class ShiroSpringCacheManager implements CacheManager {
private org.springframework.cache.CacheManager cacheManager;
public ShiroSpringCacheManager(org.springframework.cache.CacheManager cacheManager) {
this.cacheManager = cacheManager;
}
@Override
public Cache getCache(String name) throws CacheException {
org.springframework.cache.Cache cache = cacheManager.getCache(name);
return new ShiroSpringCache<>(cache);
}
class ShiroSpringCache implements Cache {
private org.springframework.cache.Cache springCache;
public ShiroSpringCache(org.springframework.cache.Cache springCache) {
if (springCache == null) {
throw new IllegalArgumentException("Cache argument cannot be null.");
}
this.springCache = springCache;
}
@Override
public V get(K key) throws CacheException {
org.springframework.cache.Cache.ValueWrapper valueWrapper = springCache.get(key);
if (valueWrapper != null) {
return (V) valueWrapper.get();
}
return null;
}
@Override
public V put(K key, V value) throws CacheException {
V previous = get(key);
springCache.put(key, value);
return previous;
}
@Override
public V remove(K key) throws CacheException {
V previous = get(key);
springCache.evict(key);
return previous;
}
@Override
public void clear() throws CacheException {
springCache.clear();
}
@Override
public int size() {
Object nativeCache = springCache.getNativeCache();
if (nativeCache instanceof Ehcache) {
Ehcache ehcache = (Ehcache) nativeCache;
return ehcache.getSize();
}
throw new UnsupportedOperationException("invoke spring cache abstract size method not supported");
}
@Override
public Set keys() {
Object nativeCache = springCache.getNativeCache();
if (nativeCache instanceof Ehcache) {
Ehcache ehcache = (Ehcache) nativeCache;
return new HashSet<>(ehcache.getKeys());
}
throw new UnsupportedOperationException("invoke spring cache abstract keys method not supported");
}
@Override
public Collection values() {
Object nativeCache = springCache.getNativeCache();
if (nativeCache instanceof Ehcache) {
Ehcache ehcache = (Ehcache) nativeCache;
List keys = ehcache.getKeys();
Map
1、 在ShiroConfig类里添加以下配置:
/**
* 缓存管理器
* @param cacheManager
* @return
*/
@Bean
public ShiroSpringCacheManager shiroSpringCacheManager(org.springframework.cache.CacheManager cacheManager) {
return new ShiroSpringCacheManager(cacheManager);
}
@Bean
@DependsOn("lifecycleBeanPostProcessor")
public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
defaultAdvisorAutoProxyCreator.setProxyTargetClass(true);
return defaultAdvisorAutoProxyCreator;
}
2、修改原有配置如下:
/**
* 自定义realm
*
* @return
*/
@Bean
public UserRealm userRealm(CacheManager cacheManager) {
UserRealm userRealm = new UserRealm(cacheManager, hashedCredentialsMatcher());
userRealm.setAuthenticationCacheName("authenticationCache");
userRealm.setAuthorizationCacheName("authorizationCache");
return userRealm;
}
/**
* 安全管理器
* 注:使用shiro-spring-boot-starter 1.4时,返回类型是SecurityManager会报错,直接引用shiro-spring则不报错
*
* @return
*/
@Bean
public DefaultWebSecurityManager securityManager(CacheManager cacheManager) {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
securityManager.setRealm(userRealm(cacheManager));
securityManager.setCacheManager(cacheManager);
return securityManager;
}
3、完整的ShiroConfig类内容:
package com.songguoliang.springboot.shiro;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.mgt.SecurityManager;
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 org.springframework.context.annotation.DependsOn;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* @Description
* @Author sgl
* @Date 2018-06-11 17:23
*/
@Configuration
public class ShiroConfig {
/**
* 缓存管理器
* @param cacheManager
* @return
*/
@Bean
public ShiroSpringCacheManager shiroSpringCacheManager(org.springframework.cache.CacheManager cacheManager) {
return new ShiroSpringCacheManager(cacheManager);
}
/**
* 凭证匹配器
*
* @return
*/
@Bean
public HashedCredentialsMatcher hashedCredentialsMatcher() {
HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
//md5加密1次
hashedCredentialsMatcher.setHashAlgorithmName("md5");
hashedCredentialsMatcher.setHashIterations(1);
return hashedCredentialsMatcher;
}
/**
* 自定义realm
*
* @return
*/
@Bean
public UserRealm userRealm(CacheManager cacheManager) {
UserRealm userRealm = new UserRealm(cacheManager, hashedCredentialsMatcher());
userRealm.setAuthenticationCacheName("authenticationCache");
userRealm.setAuthorizationCacheName("authorizationCache");
return userRealm;
}
/**
* 安全管理器
* 注:使用shiro-spring-boot-starter 1.4时,返回类型是SecurityManager会报错,直接引用shiro-spring则不报错
*
* @return
*/
@Bean
public DefaultWebSecurityManager securityManager(CacheManager cacheManager) {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
securityManager.setRealm(userRealm(cacheManager));
securityManager.setCacheManager(cacheManager);
return securityManager;
}
/**
* 设置过滤规则
*
* @param securityManager
* @return
*/
@Bean
public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(securityManager);
shiroFilterFactoryBean.setLoginUrl("/login");
shiroFilterFactoryBean.setSuccessUrl("/");
shiroFilterFactoryBean.setUnauthorizedUrl("/unauth");
//注意此处使用的是LinkedHashMap,是有顺序的,shiro会按从上到下的顺序匹配验证,匹配了就不再继续验证
//所以上面的url要苛刻,宽松的url要放在下面,尤其是"/**"要放到最下面,如果放前面的话其后的验证规则就没作用了。
Map filterChainDefinitionMap = new LinkedHashMap<>();
filterChainDefinitionMap.put("/static/**", "anon");
filterChainDefinitionMap.put("/login", "anon");
filterChainDefinitionMap.put("/captcha.jpg", "anon");
filterChainDefinitionMap.put("/favicon.ico", "anon");
filterChainDefinitionMap.put("/**", "authc");
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
return shiroFilterFactoryBean;
}
@Bean
@DependsOn("lifecycleBeanPostProcessor")
public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
defaultAdvisorAutoProxyCreator.setProxyTargetClass(true);
return defaultAdvisorAutoProxyCreator;
}
}
至此,我们以将shiro和ehcache集成完毕,有个需要特别注意的是,UserRealm里注入的SysUserService等service,需要延迟注入,所以都要添加@Lazy注解(如果不加需要自己延迟注入),否则会导致该service里的@Cacheable缓存注解、@Transactional事务注解等失效。
源码:
github
码云