preface
































Tomcat’s redis session is shared





Prepare the jar package

  • Jedis – 2.9.0. Jar
  • The Commons – pool2-2.9.0. Jar
  • Tomcat – redis – session – manager – 2.0.0. Jar

After downloading it, you can put it into the Tomcat lib directory. I have not studied the version of jar package carefully. The above list is a combination of possible versions. The main one is tomcat-redis-session-manager.jar, and you can guess how it works. Tomcat is supposed to fetch and save sessions in one interface. Like this:

interface SessionManager {
    Session getSession(a);
    void saveSession(a);
}
Copy the code

The jar package tomcat-redis-session-manager implements this interface, obtains the session from Redis, saves the session to redis. Commons-pool2-2.9.0. jar would not have been needed otherwise. With the JAR package in place, the configuration can begin.

Modify the context. The XML

<Context>
    <! -- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <! -- Uncomment this to disable session persistence across Tomcat restarts -->
    <! -- <Manager pathname="" /> -->
    <! -- Uncomment this to enable Comet connection tacking (provides events on session expiration as well as webapp lifecycle)  -->
    <! -- <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" /> -->
	<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
	<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
	   host="127.0.0.1"
	   port="6379"
	   database="0"
	   maxInactiveInterval="60" />
</Context>
Copy the code

In context.xml, insert two nodes, Valve and Manager, and restart Tomcat. The session is shared.

  • valve

When I first saw this node, I thought it was value. I looked at it carefully and then confirmed it was “Valve”. After staring at it for a while, I realized it was really “Valve”. And the mechanism behind the valve, also not small, see this blog (www.cnblogs.com/benwu/artic)…

  • manager

Configure redis connection information, redis reading and writing are also completed by this class.

Configuration – free Springboot SpringSession





Then prepare two denpendency in pom.xml

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

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

Add redis connection parameters and session storage mode to application.properties

spring.redis.host=localhost
spring.redis.port=6379

spring.session.store-type=redis
Copy the code

Is over!

  • Principle of speculation

SpringBoot comes with Tomcat built in, so the session sharing process isn’t that different from what we saw in the previous chapter. Introducing the denpendency of spring-boot-starter-data-redis is just like introducing jedis. Jar to add, delete, and check redis. Introducing spring-session-data-redis is like introducing tomcat-redis-session-manager.jar to customize the storage and reading methods of sessions.

3. Some simple scenarios








public class LoginInterceptor implements HandlerInterceptor {

    @Autowired
    private RedisTemplate redisTemplate;

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (request.getRequestURI().contains("/login") || request.getRequestURI().contains("/error")) {
            return true;
        }

        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        Object attribute = operations.get(HelloController.LOGIN_KEY);

        if (attribute ==null || StringUtils.isBlank(attribute.toString())) {
            response.sendRedirect(request.getContextPath() + "/login");
            return false;
        }

        return true; }}Copy the code