SpringBoot整合Redis的方法
本篇内容介绍了“SpringBoot整合redis的方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
成都创新互联是一家专注于网站建设、成都网站设计与策划设计,柳江网站建设哪家好?成都创新互联做网站,专注于网站建设十年,网设计领域的专业建站公司;建站业务涵盖:柳江等地区。柳江做网站价格咨询:028-86922220
架构
方案
使用 redis
集中存储,实现分布式集群共享用户信息,这里我们采用第三方开源插件crazycake
来实现,pom.xml
引入:
org.springframework.boot spring-boot-starter-data-redis org.crazycake shiro-redis 3.2.3
配置 application.properties
:
# Redis # 数据库索引(默认为0) redis.database=0 # 服务器地址 变更为自己的 redis.host=127.0.0.1 # 服务器连接端口 redis.port=6379 # 服务器连接密码,如果不设置密码注释掉即可 # redis.password= # 连接超时时间(毫秒) redis.timeout=30000
本来crazycake
插件已经实现了RedisManager
,但是参数不可配,这里我们需要自己重写一下:
public class RedisManager extends WorkAloneRedisManager implements IRedisManager { private RedisProperties redis; private JedisPool jedisPool; public RedisManager(RedisProperties redis) { this.redis = redis; } private void init() { synchronized(this) { if (this.jedisPool == null) { this.jedisPool = new JedisPool(this.getJedisPoolConfig(), redis.getHost(), redis.getPort(), redis.getTimeout(), redis.getPassword(), redis.getDatabase()); } } } @Override protected Jedis getJedis() { if (this.jedisPool == null) { this.init(); } return this.jedisPool.getResource(); } }
参数配置 RedisProperties
:
@Data @ConfigurationProperties(prefix = "redis") public class RedisProperties { private String host; private int port; private int timeout; private String password; private int database; }
配置 ShiroConfig
:
/** * Shiro权限配置 * 一定要配置 @Configuration 和 @EnableConfigurationProperties 注解 */ @Configuration @EnableConfigurationProperties({RedisProperties.class}) public class ShiroConfig { private RedisProperties redis; public ShiroConfig(RedisProperties redis) { this.redis = redis; } @Bean public UserRealm userRealm() { return new UserRealm(); } @Bean public ShiroFilterFactoryBean shiroFilterFactoryBean (SecurityManager securityManager) { ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); shiroFilterFactoryBean.setSecurityManager(securityManager); shiroFilterFactoryBean.setLoginUrl("/index.html"); shiroFilterFactoryBean.setUnauthorizedUrl("/403"); // 拦截器 MapfilterChainDefinitionMap = new LinkedHashMap<>(); /** * 静态文件 */ filterChainDefinitionMap.put("/file/**","anon"); /** * 登录注册 */ filterChainDefinitionMap.put("/register.shtml","anon"); filterChainDefinitionMap.put("/login.shtml","anon"); /** * 管理后台 */ filterChainDefinitionMap.put("/sys/**", "roles[admin]"); filterChainDefinitionMap.put("/**", "authc"); shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap); return shiroFilterFactoryBean; } @Bean public SessionsSecurityManager securityManager() { DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); securityManager.setRealm(userRealm()); securityManager.setCacheManager(cacheManager()); securityManager.setSessionManager(sessionManager()); return securityManager; } @Bean public DefaultWebSessionManager sessionManager() { DefaultWebSessionManager sessionManager = new DefaultWebSessionManager(); sessionManager.setSessionIdUrlRewritingEnabled(false); sessionManager.setSessionDAO(redisSessionDAO()); return sessionManager; } @Bean public ShiroDialect shiroDialect(){ return new ShiroDialect(); } /** * cacheManager 缓存 redis实现 * @return */ public RedisCacheManager cacheManager() { RedisCacheManager redisCacheManager = new RedisCacheManager(); redisCacheManager.setRedisManager(redisManager()); return redisCacheManager; } /** * 配置shiro redisManager * @return */ public RedisManager redisManager() { RedisManager redisManager = new RedisManager(redis); return redisManager; } /** * RedisSessionDAO shiro sessionDao层的实现 * 原理就是重写 AbstractSessionDAO * 有兴趣的小伙伴自行阅读源码 */ @Bean public RedisSessionDAO redisSessionDAO() { RedisSessionDAO redisSessionDAO = new RedisSessionDAO(); redisSessionDAO.setRedisManager(redisManager()); return redisSessionDAO; }
“SpringBoot整合Redis的方法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!
分享标题:SpringBoot整合Redis的方法
转载源于:http://lswzjz.com/article/pieiph.html