• springboot项目中的普通Session和使用redis存储session


     普通session:

    session store type使用来存放session的存储方式,目前Spring boot中只支持Redis方式,

            由于本应用暂无需将session放入redis的需求,故这里就可以将session store type设置为none.

    这里我们将此配置信息放入application.properites之中:

    spring.session.store-type=none

    pom.xml:

    <!-- 引入session jar包 -->
            <dependency>
                <groupId>org.springframework.session</groupId>
                <artifactId>spring-session</artifactId>
            </dependency>

    session 有效时间设置:

    //在程序入口类中添加如下代码:
    //设置session失效时间
        @Bean
        public EmbeddedServletContainerCustomizer containerCustomizer(){
            return new EmbeddedServletContainerCustomizer() {
                @Override
                public void customize(ConfigurableEmbeddedServletContainer container) {
                    container.setSessionTimeout(1800);//单位为S
                }
            };
        }

    使用redis存储session:

    1. pom.xml设置

    <!-- 引入session jar包 -->
            <dependency>
                <groupId>org.springframework.session</groupId>
                <artifactId>spring-session</artifactId>
            </dependency>

    2. application.properties配置

    #session存储格式为redis
    spring.session.store-type=redis
    #redis配置:
    spring.redis.database=0
    spring.redis.host=localhost
    spring.redis.password=
    spring.redis.pool.max-active=8
    spring.redis.pool.max-idle=8
    spring.redis.pool.max-wait=-1
    spring.redis.pool.min-idle=0
    spring.redis.port=6379

    3.测试代码:

    @RequestMapping("/")
    @ResponseBody
    String home(HttpSession session) {
        session.setAttribute("test", new Date());
        return "Hello World!";
    }

    4. 缺点:

    在用redis做session管理的时候,2个ajax在begin_request时间基本一致, 
    但是到达action的时间就有差距了,相差在500毫秒左右(处理session),从而容易造成请求阻塞。

    原文链接

    总结: 对于接口api类型的小项目,可以不配置session。

  • 相关阅读:
    微信Web开发者工具 移动调试 手机连接不上
    js 生成guid 自定义函数
    json
    c# 返回时间差
    Quartz.Net和队列应用demo
    数据库字段数字表示含义的枚举维护
    API文档自动生成,Swagger的配置
    请求资源文件报500错误
    文件上传三:base64文件上传
    文件上传二:FormData上传
  • 原文地址:https://www.cnblogs.com/leeego-123/p/10573300.html
Copyright © 2020-2023  润新知