SpringBoot整合Redis的方法
SpringBoot整合Redis的方法
本篇内容介绍了“SpringBoot整合Redis的方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
架构
方案
使用redis
集中存储,实现分布式集群共享用户信息,这里我们采用第三方开源插件crazycake
来实现,pom.xml
引入:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.crazycake</groupId><artifactId>shiro-redis</artifactId><version>3.2.3</version></dependency>
配置application.properties
:
#Redis#数据库索引(默认为0)redis.database=0#服务器地址变更为自己的redis.host=127.0.0.1#服务器连接端口redis.port=6379#服务器连接密码,如果不设置密码注释掉即可#redis.password=#连接超时时间(毫秒)redis.timeout=30000
本来crazycake
插件已经实现了RedisManager
,但是参数不可配,这里我们需要自己重写一下:
publicclassRedisManagerextendsWorkAloneRedisManagerimplementsIRedisManager{privateRedisPropertiesredis;privateJedisPooljedisPool;publicRedisManager(RedisPropertiesredis){this.redis=redis;}privatevoidinit(){synchronized(this){if(this.jedisPool==null){this.jedisPool=newJedisPool(this.getJedisPoolConfig(),redis.getHost(),redis.getPort(),redis.getTimeout(),redis.getPassword(),redis.getDatabase());}}}@OverrideprotectedJedisgetJedis(){if(this.jedisPool==null){this.init();}returnthis.jedisPool.getResource();}}
参数配置RedisProperties
:
@Data@ConfigurationProperties(prefix="redis")publicclassRedisProperties{privateStringhost;privateintport;privateinttimeout;privateStringpassword;privateintdatabase;}
配置ShiroConfig
:
/***Shiro权限配置*一定要配置@Configuration和@EnableConfigurationProperties注解*/@Configuration@EnableConfigurationProperties({RedisProperties.class})publicclassShiroConfig{privateRedisPropertiesredis;publicShiroConfig(RedisPropertiesredis){this.redis=redis;}@BeanpublicUserRealmuserRealm(){returnnewUserRealm();}@BeanpublicShiroFilterFactoryBeanshiroFilterFactoryBean(SecurityManagersecurityManager){ShiroFilterFactoryBeanshiroFilterFactoryBean=newShiroFilterFactoryBean();shiroFilterFactoryBean.setSecurityManager(securityManager);shiroFilterFactoryBean.setLoginUrl("/index.html");shiroFilterFactoryBean.setUnauthorizedUrl("/403");//拦截器Map<String,String>filterChainDefinitionMap=newLinkedHashMap<>();/***静态文件*/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);returnshiroFilterFactoryBean;}@BeanpublicSessionsSecurityManagersecurityManager(){DefaultWebSecurityManagersecurityManager=newDefaultWebSecurityManager();securityManager.setRealm(userRealm());securityManager.setCacheManager(cacheManager());securityManager.setSessionManager(sessionManager());returnsecurityManager;}@BeanpublicDefaultWebSessionManagersessionManager(){DefaultWebSessionManagersessionManager=newDefaultWebSessionManager();sessionManager.setSessionIdUrlRewritingEnabled(false);sessionManager.setSessionDAO(redisSessionDAO());returnsessionManager;}@BeanpublicShiroDialectshiroDialect(){returnnewShiroDialect();}/***cacheManager缓存redis实现*@return*/publicRedisCacheManagercacheManager(){RedisCacheManagerredisCacheManager=newRedisCacheManager();redisCacheManager.setRedisManager(redisManager());returnredisCacheManager;}/***配置shiroredisManager*@return*/publicRedisManagerredisManager(){RedisManagerredisManager=newRedisManager(redis);returnredisManager;}/***RedisSessionDAOshirosessionDao层的实现*原理就是重写AbstractSessionDAO*有兴趣的小伙伴自行阅读源码*/@BeanpublicRedisSessionDAOredisSessionDAO(){RedisSessionDAOredisSessionDAO=newRedisSessionDAO();redisSessionDAO.setRedisManager(redisManager());returnredisSessionDAO;}
“SpringBoot整合Redis的方法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注恰卡编程网网站,小编将为大家输出更多高质量的实用文章!
推荐阅读
-
vue动态添加删除输入框(springboot vue怎么让数据库显示出来)
springbootvue怎么让数据库显示出来?一般情况下是前端调阅后端接口,来获取到数据库的数据,后端哪里会把数据库的数据整理...
-
php如何让Swoole/Pool进程池实现Redis持久连接
php如何让Swoole/Pool进程池实现Redis持久连接本篇...
-
php操作redis大全记录
php连接redis测试˂?php$redis=newRedis();$redis-˃conne...
-
PHP经典高级工程师面试题
1.PHP如何实现不用自带的cookie函数为客户端下发cookie。对于分布式系统,如何来保存session值...
-
PHP操作Redis数据库
-
php利用redis防止商品超发来限制抢购,简单又实用
-
php如何实现秒杀功能?php+redis模拟简单抢购场景,快来看看吧
-
PHP高级工程师面试题
-
Laravel结合Redis发送邮箱验证码
-
使用redis缓存实现多服务器PHP sessions共享