zoukankan      html  css  js  c++  java
  • Session共享问题有哪些解决方案?

    原创itcats_cn 最后发布于2018-09-07 01:08:09 阅读数 229 收藏
    展开
    1、使用模拟spring-session+ redis【可靠】

    2、使用token重写session【可靠】

    3、使用cookie,不安全

    4、使用nginx负载均衡策略,ip_hash绑定,不存在session共享问题

    5、使用数据库同步session,对数据库有压力

    6、tomcat配置session共享

    利用cookie同步session数据原理图如下


    缺点:安全性差、http请求都需要带参数增加了带宽消耗

    使用spring-session把session存放在Redis
    1、创建一个springboot项目

    2、引入maven依赖

    <!--spring boot 与redis应用基本环境配置 -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-redis</artifactId>
    </dependency>
    <!--spring session 与redis应用基本环境配置,需要开启redis后才可以使用,不然启动Spring boot会报错 -->
    <dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-data-redis</artifactId>
    </dependency>
    3、创建SessionConfig.java

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
    import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

    //这个类用配置redis服务器的连接
    //maxInactiveIntervalInSeconds为SpringSession的过期时间(单位:秒)
    @EnableRedisHttpSession(maxInactiveIntervalInSeconds = 1800)
    public class SessionConfig {

    // 冒号后的值为没有配置文件时,自动装载的默认值
    @Value("${redis.hostname:localhost}")
    String HostName;
    @Value("${redis.port:6379}")
    int Port;
    @Value("${redis.password}")
    String password;

    @Bean
    public JedisConnectionFactory connectionFactory() {
    JedisConnectionFactory connection = new JedisConnectionFactory();
    connection.setPort(Port);
    connection.setHostName(HostName);
    connection.setPassword(password);
    return connection;
    }
    }
     

    4、初始化Session

    //初始化Session配置
    public class SessionInitializer extends AbstractHttpSessionApplicationInitializer{
    public SessionInitializer() {
    super(SessionConfig.class);
    }
    }
     

    5、控制器层代码

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.SpringApplication;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;

    @RestController
    public class SessionController {

    @Value("${server.port}")
    private String PORT;

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

    @RequestMapping("/index")
    public String index() {
    return "index:" + PORT;
    }

    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request, String sessionKey, String sessionValue) {
    HttpSession session = request.getSession(true);
    session.setAttribute(sessionKey, sessionValue);
    return "success,port:" + PORT;
    }

    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request, String sessionKey) {
    HttpSession session =null;
    try {
    session = request.getSession(false);
    } catch (Exception e) {
    e.printStackTrace();
    }
    String value=null;
    if(session!=null){
    value = (String) session.getAttribute(sessionKey);
    }
    return "sessionValue:" + value + ",port:" + PORT;
    }

    }
     

    6、配置application.properties

    ########################################################
    ###Redis (RedisConfiguration)
    ########################################################
    spring.redis.database=0
    spring.redis.host=10.37.129.3
    spring.redis.port=6379
    spring.redis.password=123456
    spring.redis.pool.max-idle=8
    spring.redis.pool.min-idle=0
    spring.redis.pool.max-active=8
    spring.redis.pool.max-wait=-1
    spring.redis.timeout=5000


    redis.hostname=10.37.129.3
    redis.port=6379
    redis.password=123456

    #path
    server.context-path=/
    ————————————————
    版权声明:本文为CSDN博主「itcats_cn」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/itcats_cn/article/details/82470408

  • 相关阅读:
    Python numpy.transpose 详解
    如何理解张量tensor
    tensorflow中张量的理解
    Theano入门——CIFAR-10和CIFAR-100数据集
    阻止form表单提交的问题
    webp图片优化
    Css控制网页变灰
    express-session相关用法
    REM+SVG Sprite,web app案例
    HTML 5 Audio/Video DOM canplaythrough 事件在移动端遇到的坑
  • 原文地址:https://www.cnblogs.com/fengff/p/12576736.html
Copyright © 2011-2022 走看看