Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of integrating Redis by SpringBoot

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "SpringBoot's method of integrating Redis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Architecture

Scheme

Use redis centralized storage to achieve distributed clusters to share user information. Here, we use a third-party open source plug-in crazycake, and pom.xml introduces:

Org.springframework.boot spring-boot-starter-data-redis org.crazycake shiro-redis 3.2.3

Configure application.properties:

# Redis# database index (default is 0) redis.database=0# server address is changed to its own redis.host=127.0.0.1# server connection port redis.port=6379# server connection password. If you do not set the password to comment out, you can # redis.password=# connection timeout (milliseconds) redis.timeout=30000

Originally, the crazycake plug-in has implemented RedisManager, but the parameters are not matched. Here, we need to rewrite it ourselves:

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 ();}}

Parameter configuration RedisProperties:

@ Data@ConfigurationProperties (prefix = "redis") public class RedisProperties {private String host; private int port; private int timeout; private String password; private int database;}

Configure ShiroConfig:

/ * * Shiro permission configuration * be sure to configure @ Configuration and @ EnableConfigurationProperties annotations * / @ 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"); / / interceptor Map filterChainDefinitionMap = new LinkedHashMap (); / * * static file * / filterChainDefinitionMap.put ("/ file/**", "anon") / * sign in and register * / filterChainDefinitionMap.put ("/ register.shtml", "anon"); filterChainDefinitionMap.put ("/ login.shtml", "anon"); / * * manage backend * / 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 cache redis implementation * @ return * / public RedisCacheManager cacheManager () {RedisCacheManager redisCacheManager = new RedisCacheManager () RedisCacheManager.setRedisManager (redisManager ()); return redisCacheManager;} / * configure shiro redisManager * @ return * / public RedisManager redisManager () {RedisManager redisManager = new RedisManager (redis); return redisManager } / * * RedisSessionDAO shiro sessionDao layer implementation * principle is to rewrite AbstractSessionDAO * interested partners read the source code * / @ Bean public RedisSessionDAO redisSessionDAO () {RedisSessionDAO redisSessionDAO = new RedisSessionDAO (); redisSessionDAO.setRedisManager (redisManager ()); return redisSessionDAO;} this is the end of the introduction of "SpringBoot Integration Redis method". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report