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 realize session under SpringBoot

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

Share

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

This article mainly introduces the relevant knowledge of how to achieve session under SpringBoot, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this SpringBoot article on how to achieve session. Let's take a look at it.

Related concept 1.HTTP is a stateless protocol

Stateless means that the protocol has no memory function for transactions. The lack of state means that if the previous information is required for subsequent processing, the previous information must be retransmitted, which may result in an increase in the amount of data transmitted per connection. On the other hand, when the server does not need the previous information, the reply is faster. Intuitively, each request is independent and has no direct connection to the previous request or the subsequent request.

two。 Which methods can achieve stateful connection

Cookies

Session

Application

For example:

However, the store in order to increase revenue. She wants to encourage customers to buy. So I'm telling you, as long as you buy more than five bottles of beer in a month, I'll give you a glass.

Let's take a look at this situation. How can we achieve this?

A: issue a magnetic card to the customer, which contains the customer's past purchase information.

So the store can know. This is cookie.

B: give the customer a unique number. The customer's consumption information specified by the number is stored in the server of the store. This is session.

Finally, the store can decide as a whole whether to send five bottles of wine glasses or six bottles. This is application.

In fact, these mechanisms add something to the stateless traditional purchase process to make the whole process stateful. This is the case with Web applications.

3.Session is maintained by the Web server side (Tomcat)

Session is managed by the Web container, that is, a session is stored on only one machine and is suitable for single applications.

However, with the development of architecture and the continuous evolution to micro-service distributed cluster, the traditional Session can not work. In order to solve the problem that all servers share a set of Session,Session, they need to be stored in a common session repository (Session Repository), and all servers access the same repository, so that the state of all servers is consistent.

The warehouses supported by Spring Session include Reids, MongoDB and JDBC.

Cookie is maintained by the client (browser)

Session corresponds to a browser window, and when the browser closes, the Session disappears

Realization method

The main purpose of this article is to sort out the methods of SpringBoot to maintain session.

Spring Session's implementation of Session sharing can be easily integrated with Spring Secuity, such as findSessionsByUserName,rememberMe, and limit the number of sessions that can be online at the same time for the same account (for example, setting it to 1 can achieve the effect of dropping the previous login), and so on.

Realization method

Implementation method: SpringSession+redis (can realize the sharing between different servers of session, suitable for multi-machine deployment)

Realization principle

The server first looks up the corresponding value of cookie (sessionid).

According to the sessionid, the session data of the corresponding id is obtained from the server-side session storage and returned.

If the sessionid is not found, the server creates a session, generates the cookie corresponding to the sessionid, and writes it to the response header.

How to insert a beautiful piece of code into the implementation code

POM dependence

/ / related dependency redis.clients jedis org.springframework.session spring-session-core org.springframework.boot spring-boot-starter-data-redis org.springframework.session spring-session-data-redis

Application.yml

/ / configure redis spring: redis: host: localhost port: 6379 database: 0 jedis: pool: max-active: 100 max-wait: 10 max-idle: 10 min-idle: 10

RedisHttpSessionConfiguration.java

/ * enable RedisHttpSession * / @ Configuration@EnableRedisHttpSessionpublic class RedisHttpSessionConfiguration {}

SessionController.java

@ RestControllerpublic class SessionController {@ RequestMapping ("/ session") public Object springSession (@ RequestParam ("username") String username, HttpServletRequest request, HttpSession session) {Cookie [] cookies = request.getCookies () If (cookies! = null & & cookies.length > 0) {for (Cookie cookie: cookies) {if (cookie.getName (). Contains ("JSESSION")) {System.out.println (cookie.getName () + "=" + cookie.getValue ());} Object value = session.getAttribute ("username") If (value = = null) {System.out.println ("user does not exist"); session.setAttribute ("username", "{username:'" + username+ "', age: 28}");} else {System.out.println ("user exists");} return "username=" + value;}}

Access port 8080 to view

This is the end of the article on "how to achieve session under SpringBoot". Thank you for reading! I believe you all have a certain understanding of "how to achieve session under SpringBoot". If you want to learn more, you are welcome to follow the industry information channel.

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

Development

Wechat

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

12
Report