Linux中session共享

有的项目的所有功能都要进行登录验证,
验证条件是判断 session 中是否有 user 对象,
如果有就认为登录过,如果没有就进行登录

session(会话):
    服务器上的一块存储空间,和客户端一一对应。
    
    一一对应靠的是 sessionId 和 cookie
        sessionId 是每个 session 的唯一标识

cookie:
    服务器发送给浏览器保存的数据,
    浏览器会自动存储服务器发送给他的所有 cookie,默认保存在网站根路径目录下
    浏览器向服务器发请求的时候会携带该网站保存的所有 cookie

使用 redis 实现 session 共享:
redis安装:https://blog.csdn.net/ilovehua521/article/details/84403735

    1. 项目增加依赖:
        spring-session-data-redis 
            通过 spring 实现 session 在 redis 共享的框架
        jedis
            使用 java 操纵 redis 的框架
        
    2. 配置 web.xml
        添加过滤器:
            <filter>
                <filter-name>springSessionRepositoryFilter</filter-name>
                <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
            </filter>

            <filter-mapping>
                <filter-name>springSessionRepositoryFilter</filter-name>
                <url-pattern>/*</url-pattern>
            </filter-mapping>

    3. 配置 spring-dao.xml
        1. 配置 redis 连接池信息
            <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"></bean>
            
        2. 配置 redis 密码    
            <bean id="redisPassword" class="org.springframework.data.redis.connection.RedisPassword">
                <constructor-arg name="thePassword" value="123456"></constructor-arg>
            </bean>
            
        3. 配置 redis 连接信息
            <bean id="redisStandaloneConfiguration" class="org.springframework.data.redis.connection.RedisStandaloneConfiguration">
                <property name="hostName" value="192.168.7.230"/>
                <property name="port" value="6379"/>
                <property name="password" ref="redisPassword"/>
            </bean>    
            
        4. 配置 redis 连接工厂
            <bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
                <constructor-arg name="standaloneConfig" ref="redisStandaloneConfiguration"></constructor-arg>
            </bean>
        
        5. 配置 session-redis 过滤器
            <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>

    4. 存入 session 的数据必须支持序列化
        序列化:对象 --> 二进制
        反序列化:二进制 --> 对象
        
        java 中的 model 类如果需要支持序列化操作
            只需要实现 Serializable 接口即可
            
            这个接口没有任何方法,起到一个标识作用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值