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

How to configure Redis to manage session in springBoot

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

Share

Shulou(Shulou.com)06/02 Report--

It is believed that many inexperienced people have no idea about how to configure Redis to manage session in springBoot. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

1. Introduce maven dependency

Org.springframework.boot spring-boot-starter-data-redis org.springframework.session spring-session-data-redis

two。 Write key configuration

Spring.session.store-type=redis # configuration redis manages session#redis connection information spring.redis.host=127.0.0.1spring.redis.port=6379spring.redis.timeout=600000spring.redis.password=

3. Add key configuration code

@ Configuration@EnableRedisHttpSession (maxInactiveIntervalInSeconds=60*60*4,redisNamespace = "spring:session:tjzz:application") / / set the session expiration time, set the storage path @ ConfigurationProperties (prefix = "spring.redis") public class SessionConfig {@ Value ("${spring.redis.host}") private String host; @ Value ("${spring.redis.port}") private int port; @ Value ("${spring.redis.timeout}") private int timeout @ Value ("${spring.redis.password}") private String password; / * * configure redis serialization method * * / @ Bean (name = "springSessionDefaultRedisSerializer") public GenericJackson2JsonRedisSerializer getGenericJackson2JsonRedisSerializer () {return new GenericJackson2JsonRedisSerializer ();} / * * cookie Base64 decoding * * / @ Bean public DefaultCookieSerializer getDefaultCookieSerializer () {DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer (); cookieSerializer.setUseBase64Encoding (false) Return cookieSerializer;}}

4. Test class

@ RestControllerpublic class SessionController {@ RequestMapping (value = "/ get-session-id.action", method = RequestMethod.GET) public String getSessionId () {HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes ()) .getRequest (); HttpSession session = request.getSession (); String sessionId= "; Object temp=session.getAttribute (" USER "); / / USER System.out.println (temp.toString ()) where userCode is stored in session SessionId=session.getId (); return sessionId;}} after reading the above, have you mastered how to configure Redis to manage session in springBoot? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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