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 integrate spring-session in springboot

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In this issue, the editor will bring you about how to integrate spring-session in springboot. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Integration of spring-sessionspring-session in springboot

It can replace HttpSesession. And the changes are minimal and transparent to the application. The underlying layer can use memory, Redis and so on to store Session information. Session sharing can be achieved through Redis, and Session can be shared by all nodes in a cluster environment.

Document https://docs.spring.io/spring-session/docs/current/reference/html5/

SpringBoot integration

With spring-session-data-redis, be sure to integrate redis into the project first.

Dependent on org.springframework.session spring-session-data-redis configuration

Configuration class: RedisSessionProperties

Spring:session: timeout: 1800 # session expiration time (in seconds) store-type: REDIS # session storage type, enumerate redis: namespace: "spring:session" # session namespace flush-mode: IMMEDIATE # flush mode, enumerate: ON_SAVE, IMMEDIATE cleanup-cron: "0 *" # regularly clean up `cron` expressions for expired session tasks

About spring.session.redis.flush-mode

ON_SAVE

Data from session is synchronized to redis only when the SessionRepository.save (Session) method is called. In web applications, synchronization begins only after the response to the request is completed. That is, the session data is cached locally before the response is executed.

IMMEDIATE

When SessionRepository.createSession () is executed, the session data is synchronized into the redis; when set/remove and other operations are performed on the attribute of the session, the data in the session is also synchronized into the redis. It is synchronized in real time.

Use

Use and need to modify something, as usual. Just get the Session of Servlet.

Controllerimport javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@RequestMapping ("/ test") @ Controllerpublic class TestController {@ GetMapping ("/ session") public ModelAndView session (HttpServletRequest request) {HttpSession httpSession = request.getSession () / / whether the newly created true System.out.println (httpSession.isNew ()); / / the implementation class org.springframework.session.web.http.SessionRepositoryFilter$SessionRepositoryRequestWrapper$HttpSessionWrapper System.out.println (httpSession.getClass () .getName ()) / / write data to session (underlying redis storage) httpSession.setAttribute ("name", "SpringBoot Chinese community"); return new ModelAndView ("test/test") }} View Test Hello ${name}

Modification of Cookie attribute

By default, the client uses Cookie to store the session id

Looking at the response information, you can see that cookieConnection: keep-aliveContent-Encoding: gzipContent-Language: zh-CNContent-Type: text/html;charset=UTF-8Date: Thu with session set, 17 Oct 2019 08:57:07 GMTServer: nginxSet-Cookie: PHPSESSIONID=Y2YwMDM1YjctMjBiYy00OWRiLWI5NGItZjFmNDU4ZDcxNThm; Max-Age=36000; Expires=Thu, 17 Oct 2019 18:57:07 GMT; Path=/; HttpOnly; SameSite=LaxTransfer-Encoding: chunked can modify cookie through configuration. Server: servlet: session: cookie: name: PHPSESSIONID # cookie name domain: # Domain path: # path comment: # Note httpOnly: # whether it is only used for http transmission secure: # whether to transmit maxAge: # only in the case of SSL The lifecycle can also modify cookie information through configuration.

Customize CookieSerializer to IOC.

@ Beanpublic CookieSerializer cookieSerializer () {DefaultCookieSerializer serializer = new DefaultCookieSerializer (); serializer.setCookieName ("JSESSIONID"); serializer.setCookiePath ("/"); serializer.setDomainNamePattern ("^. +?\\. (\\ w +\. [Amurz] +) $"); return serializer;} index of Session

The popular understanding is that you can index key and session and get session based on some key.

FindByIndexNameSessionRepository

Requirements: get its session according to the user id

After the user has successfully logged in, deposit its id in Session with a fixed key

Value only accepts strings

@ AutowiredFindByIndexNameSessionRepository

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