mirror of
https://gitee.com/jamen/slife.git
synced 2026-10-30 22:03:40 +00:00
shiro cache manager config
This commit is contained in:
+6
-5
@@ -1,4 +1,4 @@
|
||||
package com.slife.shiro;
|
||||
package com.slife.config.shiro;
|
||||
|
||||
import com.slife.constant.Global;
|
||||
import com.slife.constant.Setting;
|
||||
@@ -6,6 +6,7 @@ import com.slife.entity.SysRole;
|
||||
import com.slife.entity.SysUser;
|
||||
import com.slife.service.ISysRoleService;
|
||||
import com.slife.service.ISysUserService;
|
||||
import com.slife.shiro.ShiroUser;
|
||||
import com.slife.util.ApplicationContextRegister;
|
||||
import com.slife.util.Encodes;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -33,7 +34,7 @@ public class AuthRealm extends AuthorizingRealm {
|
||||
HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(Setting.HASH_ALGORITHM);
|
||||
matcher.setHashIterations(Setting.HASH_INTERATIONS);
|
||||
setCredentialsMatcher(matcher);
|
||||
log.info("---------密码校验器 -matcher----------------");
|
||||
log.info("[AuthRealm#initCredentialsMatcher] ---> set credentials matcher");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -45,7 +46,7 @@ public class AuthRealm extends AuthorizingRealm {
|
||||
ISysUserService sysUserService = ApplicationContextRegister.getBean(ISysUserService.class);
|
||||
SysUser sysUser = sysUserService.getByLoginName(loginName);
|
||||
if (sysUser != null) {
|
||||
log.info(sysUser.getName() + "登录");
|
||||
log.info("[AuthRealm#doGetAuthenticationInfo] --> {} login", sysUser.getName());
|
||||
if (Global.NO.equals(sysUser.getLoginFlag())) {
|
||||
throw new DisabledAccountException();
|
||||
}
|
||||
@@ -54,7 +55,7 @@ public class AuthRealm extends AuthorizingRealm {
|
||||
sysUser.getPassword().substring(16), ByteSource.Util.bytes(salt),
|
||||
getName());
|
||||
} else {
|
||||
log.info(sysUser.getName()+"登录失败");
|
||||
log.error("[AuthRealm#doGetAuthenticationInfo] --> {} login error", sysUser.getName());
|
||||
throw new UnknownAccountException();
|
||||
}
|
||||
}
|
||||
@@ -64,7 +65,7 @@ public class AuthRealm extends AuthorizingRealm {
|
||||
*/
|
||||
@Override
|
||||
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
|
||||
log.info("登录授权");
|
||||
log.info("[AuthRealm#doGetAuthorizationInfo] --> authorization");
|
||||
ShiroUser shiroUser = (ShiroUser) principals.getPrimaryPrincipal();
|
||||
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.slife.config.shiro;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.sf.ehcache.CacheManager;
|
||||
import org.apache.shiro.cache.ehcache.EhCacheManager;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
|
||||
/**
|
||||
* @author felixu
|
||||
* @date 2019.03.14
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
public class CacheConfig {
|
||||
|
||||
@Bean()
|
||||
@ConditionalOnProperty(prefix = "slife.shiro", havingValue = "ehcacheManager", name = "cacheManager")
|
||||
public EhCacheManager ehCacheManager() {
|
||||
log.info("[CacheConfig#ehCacheManager] ---> init ehCacheManager");
|
||||
CacheManager cacheManager = CacheManager.getCacheManager("slife");
|
||||
EhCacheManager ehCacheManager = new EhCacheManager();
|
||||
ehCacheManager.setCacheManager(cacheManager);
|
||||
ehCacheManager.setCacheManagerConfigFile("classpath:cache/ehcache-shiro.xml");
|
||||
return ehCacheManager;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(prefix = "slife.shiro", havingValue = "redisCacheManager", name = "cacheManager")
|
||||
public RedisCacheManager redisCacheManager(RedisTemplate<String, String> redisTemplate) {
|
||||
log.info("[CacheConfig#redisCacheManager] ---> init redisCacheManager");
|
||||
return new RedisCacheManager(redisTemplate);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package com.slife.config.shiro;
|
||||
|
||||
|
||||
import org.apache.shiro.cache.Cache;
|
||||
import org.apache.shiro.cache.CacheException;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author felixu
|
||||
* @date 2019.01.03
|
||||
*/
|
||||
public class RedisCache<K, V> implements Cache<K, V> {
|
||||
|
||||
private static final String REDIS_SHIRO_CACHE = "shiro-cache:";
|
||||
private String cacheKey;
|
||||
private RedisTemplate<K, V> redisTemplate;
|
||||
private long globExpire = 30;
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public RedisCache(String name, RedisTemplate client) {
|
||||
this.cacheKey = REDIS_SHIRO_CACHE + name + ":";
|
||||
this.redisTemplate = client;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V get(K key) throws CacheException {
|
||||
redisTemplate.boundValueOps(getCacheKey(key)).expire(globExpire, TimeUnit.MINUTES);
|
||||
return redisTemplate.boundValueOps(getCacheKey(key)).get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public V put(K key, V value) throws CacheException {
|
||||
V old = get(key);
|
||||
redisTemplate.boundValueOps(getCacheKey(key)).set(value);
|
||||
return old;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V remove(K key) throws CacheException {
|
||||
V old = get(key);
|
||||
redisTemplate.delete(getCacheKey(key));
|
||||
return old;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() throws CacheException {
|
||||
redisTemplate.delete(keys());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return keys().size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<K> keys() {
|
||||
return redisTemplate.keys(getCacheKey("*"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<V> values() {
|
||||
Set<K> set = keys();
|
||||
List<V> list = new ArrayList<>();
|
||||
for (K s : set) {
|
||||
list.add(get(s));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private K getCacheKey(Object k) {
|
||||
return (K) (this.cacheKey + k);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.slife.config.shiro;
|
||||
|
||||
|
||||
import org.apache.shiro.cache.Cache;
|
||||
import org.apache.shiro.cache.CacheException;
|
||||
import org.apache.shiro.cache.CacheManager;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
|
||||
|
||||
/**
|
||||
* @author felixu
|
||||
* @date 2019.01.03
|
||||
*/
|
||||
public class RedisCacheManager implements CacheManager {
|
||||
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
public RedisCacheManager(RedisTemplate redisTemplate) {
|
||||
this.redisTemplate = redisTemplate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <K, V> Cache<K, V> getCache(String name) throws CacheException {
|
||||
return new RedisCache<K, V>(name, redisTemplate);
|
||||
}
|
||||
}
|
||||
+2
-13
@@ -8,7 +8,7 @@ package com.slife.config.shiro;
|
||||
* Describe:
|
||||
*/
|
||||
|
||||
import com.slife.shiro.AuthRealm;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.shiro.cache.CacheManager;
|
||||
import org.apache.shiro.mgt.SecurityManager;
|
||||
import org.apache.shiro.mgt.SessionsSecurityManager;
|
||||
@@ -25,7 +25,6 @@ import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.filter.DelegatingFilterProxy;
|
||||
|
||||
import javax.servlet.DispatcherType;
|
||||
import java.util.EnumSet;
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -35,22 +34,12 @@ import java.util.LinkedHashMap;
|
||||
* @author Administrator
|
||||
*
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
@Configuration
|
||||
public class ShiroConfig {
|
||||
|
||||
private Logger logger = org.slf4j.LoggerFactory.getLogger(getClass());
|
||||
|
||||
// @Bean
|
||||
// public FilterRegistrationBean delegatingFilterProxy(){
|
||||
// FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
|
||||
// DelegatingFilterProxy proxy = new DelegatingFilterProxy();
|
||||
// proxy.setTargetFilterLifecycle(true);
|
||||
// proxy.setTargetBeanName("shiroFilter");
|
||||
// filterRegistrationBean.setFilter(proxy);
|
||||
// return filterRegistrationBean;
|
||||
// }
|
||||
|
||||
@Bean
|
||||
public FilterRegistrationBean<DelegatingFilterProxy> delegatingFilterProxy(){
|
||||
FilterRegistrationBean<DelegatingFilterProxy> filterRegistrationBean = new FilterRegistrationBean<>();
|
||||
+79
-79
@@ -1,80 +1,80 @@
|
||||
package com.slife.config.cache;
|
||||
|
||||
import org.apache.shiro.cache.ehcache.EhCacheManager;
|
||||
import org.crazycake.shiro.RedisCacheManager;
|
||||
import org.crazycake.shiro.RedisManager;
|
||||
import org.slf4j.Logger;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
|
||||
/**
|
||||
* ehcache配置
|
||||
*
|
||||
* @author chen
|
||||
*/
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
public class CacheConfig {
|
||||
|
||||
|
||||
|
||||
private Logger logger = org.slf4j.LoggerFactory.getLogger(getClass());
|
||||
|
||||
@Bean(name = "ehCacheManager")
|
||||
public EhCacheManager ehCacheManager() {
|
||||
logger.info("--------------ehCacheManager init---------------");
|
||||
EhCacheManager cacheManager = new EhCacheManager();
|
||||
cacheManager.setCacheManagerConfigFile("classpath:cache/ehcache-shiro.xml");
|
||||
logger.info("--------------ehCacheManager init---------------" + cacheManager);
|
||||
return cacheManager;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
// @Bean(name = "slifeRedisCacheManager")
|
||||
// @Primary
|
||||
// public SlifeRedisCacheManager slifeRedisCacheManager() {
|
||||
// logger.info("--------------redis cache init---------------");
|
||||
// SlifeRedisCacheManager slifeRedisCacheManager = new SlifeRedisCacheManager();
|
||||
// slifeRedisCacheManager.setRedisTemplate(redisTemplate);
|
||||
// logger.info("--------------redis cache ---------------" + slifeRedisCacheManager);
|
||||
// return slifeRedisCacheManager;
|
||||
//package com.slife.config.cache;
|
||||
//
|
||||
//import org.apache.shiro.cache.ehcache.EhCacheManager;
|
||||
//import org.crazycake.shiro.RedisCacheManager;
|
||||
//import org.crazycake.shiro.RedisManager;
|
||||
//import org.slf4j.Logger;
|
||||
//import org.springframework.beans.factory.annotation.Autowired;
|
||||
//import org.springframework.beans.factory.annotation.Value;
|
||||
//import org.springframework.cache.annotation.EnableCaching;
|
||||
//import org.springframework.context.annotation.Bean;
|
||||
//import org.springframework.context.annotation.Configuration;
|
||||
//import org.springframework.context.annotation.Primary;
|
||||
//import org.springframework.data.redis.core.RedisTemplate;
|
||||
//
|
||||
///**
|
||||
// * ehcache配置
|
||||
// *
|
||||
// * @author chen
|
||||
// */
|
||||
//@Configuration
|
||||
//@EnableCaching
|
||||
//public class CacheConfig {
|
||||
//
|
||||
//
|
||||
//
|
||||
// private Logger logger = org.slf4j.LoggerFactory.getLogger(getClass());
|
||||
//
|
||||
// @Bean(name = "ehCacheManager")
|
||||
// public EhCacheManager ehCacheManager() {
|
||||
// logger.info("--------------ehCacheManager init---------------");
|
||||
// EhCacheManager cacheManager = new EhCacheManager();
|
||||
// cacheManager.setCacheManagerConfigFile("classpath:cache/ehcache-shiro.xml");
|
||||
// logger.info("--------------ehCacheManager init---------------" + cacheManager);
|
||||
// return cacheManager;
|
||||
// }
|
||||
|
||||
|
||||
// @Bean(name = "redisCacheManager")
|
||||
// @Primary
|
||||
// public RedisCacheManager redisCacheManager() {
|
||||
// logger.info("--------------redis cache init---------------");
|
||||
// RedisCacheManager redisCacheManager = new RedisCacheManager();
|
||||
// redisCacheManager.setRedisManager(redisManager());
|
||||
// logger.info("--------------redis cache ---------------" + redisCacheManager);
|
||||
// return redisCacheManager;
|
||||
// }
|
||||
|
||||
|
||||
@Value("${spring.redis.host}")
|
||||
private String host;
|
||||
// @Value("${spring.redis.password}")
|
||||
// private String password;
|
||||
@Value("${spring.redis.port}")
|
||||
private int port;
|
||||
@Value("${spring.redis.database}")
|
||||
private int database;
|
||||
|
||||
// public RedisManager redisManager() {
|
||||
// RedisManager redisManager = new RedisManager();
|
||||
// redisManager.setHost(host);
|
||||
//// redisManager.setPassword(password);
|
||||
// redisManager.setPort(port);
|
||||
// redisManager.setDatabase(database);
|
||||
// return redisManager;
|
||||
// }
|
||||
|
||||
}
|
||||
//
|
||||
// @Autowired
|
||||
// private RedisTemplate redisTemplate;
|
||||
//
|
||||
//// @Bean(name = "slifeRedisCacheManager")
|
||||
//// @Primary
|
||||
//// public SlifeRedisCacheManager slifeRedisCacheManager() {
|
||||
//// logger.info("--------------redis cache init---------------");
|
||||
//// SlifeRedisCacheManager slifeRedisCacheManager = new SlifeRedisCacheManager();
|
||||
//// slifeRedisCacheManager.setRedisTemplate(redisTemplate);
|
||||
//// logger.info("--------------redis cache ---------------" + slifeRedisCacheManager);
|
||||
//// return slifeRedisCacheManager;
|
||||
//// }
|
||||
//
|
||||
//
|
||||
//// @Bean(name = "redisCacheManager")
|
||||
//// @Primary
|
||||
//// public RedisCacheManager redisCacheManager() {
|
||||
//// logger.info("--------------redis cache init---------------");
|
||||
//// RedisCacheManager redisCacheManager = new RedisCacheManager();
|
||||
//// redisCacheManager.setRedisManager(redisManager());
|
||||
//// logger.info("--------------redis cache ---------------" + redisCacheManager);
|
||||
//// return redisCacheManager;
|
||||
//// }
|
||||
//
|
||||
//
|
||||
// @Value("${spring.redis.host}")
|
||||
// private String host;
|
||||
//// @Value("${spring.redis.password}")
|
||||
//// private String password;
|
||||
// @Value("${spring.redis.port}")
|
||||
// private int port;
|
||||
// @Value("${spring.redis.database}")
|
||||
// private int database;
|
||||
//
|
||||
//// public RedisManager redisManager() {
|
||||
//// RedisManager redisManager = new RedisManager();
|
||||
//// redisManager.setHost(host);
|
||||
////// redisManager.setPassword(password);
|
||||
//// redisManager.setPort(port);
|
||||
//// redisManager.setDatabase(database);
|
||||
//// return redisManager;
|
||||
//// }
|
||||
//
|
||||
//}
|
||||
|
||||
@@ -59,6 +59,9 @@ mybatis-plus:
|
||||
configuration:
|
||||
map-underscore-to-camel-case: true
|
||||
cache-enabled: false
|
||||
slife:
|
||||
shiro:
|
||||
cacheManager: ehcacheManager
|
||||
#security:
|
||||
# basic:
|
||||
# enabled: false
|
||||
|
||||
Reference in New Issue
Block a user