This is the 13th day of my participation in the August More Text Challenge

I. Implementation principle

  1. The first time a user requests a SESSIONID, the server generates a SESSIONID and returns it to the browser. The browser is saved as cookies

  2. Cookies are shared on the same IP(domain name), on different ports, in the same browser. The Cookies of users accessing different IP(domain name) must be different in the same browser. In this case, load balancing should be added in front of cluster applications, such as Nginx and HaProxy.

  3. Normal sessions are maintained by the Servlet container so that sessions cannot be shared. If you want sessions to be shared, you need to store sessions in a unified location, such as Redis. Maintenance is handed over to The Spring Session.

  4. In addition to Cookies that can maintain a Sessionid, Spring Session provides another way to pass a Sessionid using headers. The purpose is to facilitate session sharing for clients such as mobile phones that do not have cookies.

Do not confuse the concept of cross-domain requests with cookies. The same-origin policy requires the same IP address, port, and protocol. Inconsistent requests are cross-domain requests. Cookies can be shared across domains, but not across domain names (IP).

Spring-session is divided into the following core modules:

  • SessionRepositoryFilter: An implementation of Filter in the Servlet specification that replaces HttpSession with Spring Session by using its own two wrappers: It and HttpServletResponse.
  • The wrapper HttpServerletRequest/HttpServletResponse/HttpSessionWrapper: Wrap the original HttpServletRequest, HttpServletResponse, and Spring Session to realize the key of switching sessions and transparent inheritance of HttpSession
  • Session: The Spring Session module
  • Storage SessionRepository: Storage of Spring sessions

2. Integrate Spring Session

1. Introduce maven dependencies for Spring-session-redis

<dependency>
     <groupId>org.springframework.session</groupId>
     <artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
Copy the code

2. Enable Redis httpSession

Annotate the startup class


@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 30 * 60 * 1000)
Copy the code

3. Configure redis link information (application.yml)

spring:

    redis:
      database: 0
      host: 127.0. 01.
      password: 123456
      port: 6379
Copy the code

4. Test

Do the same for both projects and add the following test code:


@RequestMapping(value="/uid",method = RequestMethod.GET)
public @ResponseBody String uid(HttpSession session) {
    UUID uid = (UUID) session.getAttribute("uid");
    if (uid == null) {
        uid = UUID.randomUUID();
    }
    session.setAttribute("uid", uid);
    return session.getId();
}
Copy the code

5. Multiple ports are started for a project

Click Edit Configuration to cancel Single Instance Only and make a copy

Set different port numbers in VM Options under environment option -dserver. port=8889 -dspring.profiles. active= test-ddebug

Visit in turn to see the effect

http://localhost:8888/uid

http://localhost:8889/uid

http://127.0.0.1:8888/uid

http://127.0.0.1:8889/uid