Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to configure Redis to manage session in springBoot

Shulou Source: shulou.com Published: 2022-06-02 10:33:44 10月13日 Update

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!

Tags: Configuration management key content method more problem storage helpless for this code information reason this sequence skill way time article experience Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno MariaDB Linux macOS Shulou Technology