SpringSession(redis)

pom.xml


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.3.RELEASE
         
    
    com.wzq
    session
    0.0.1-SNAPSHOT
    session
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.springframework.session
            spring-session-data-redis
        

        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



application.yml
server:
  servlet:
    session:
      timeout: 3600s
spring:
  application:
    name: sesion
  session:
    store-type: redis
    redis:
      namespace: spring:session
    timeout: 3600s
  redis:
    database: 1
    host: 192.168.0.45
    port: 6379
    password: 123456
    timeout: 20000
    lettuce:
      pool:
        pool:
          max-active: 8
          max-wait: -1
          max-idle: 8
          min-idle: 0


run.class
package com.wzq.session;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

@SpringBootApplication
/**
 * maxInactiveIntervalInSeconds 设置session超时时间 默认 30分钟
 */
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 5000)
public class SessionApplication {

    public static void main(String[] args) {
        SpringApplication.run(SessionApplication.class, args);
    }

}

test方法
package com.wzq.session.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: 测试Controller
 * @author: Wzq
 * @create: 2020-09-03 14:37
 */
@RestController
@RequestMapping
public class TestController {

    /**
     * 登录
     * @param session
     * @return
     */
    @RequestMapping("login")
    public String login(HttpSession session,String msg){
        session.setAttribute("msg",msg);
        return session.getId();
    }

    /**
     * 测试方法
     * @param session
     * @return
     */
    @RequestMapping("test")
    public Map test(HttpSession session){
        String sessionId = session.getId();
        Object msg = session.getAttribute("msg");
        Map map = new HashMap<>();
        map.put("sessionId", sessionId);
        map.put("msg", msg);
        return map;
    }

}

你可能感兴趣的:(SpringSession(redis))