spring session 考虑问题解答

相关问题
2.Redis容量考虑,由于spring seesion依赖redis,考虑是否和业务redis分开?
Spring session 一次访问会产生3个key,
根据用户量以及程序中session.setAttribute的量来考虑是否需要跟业务redis分开

3.Redis高可用考虑,由于spring seesion依赖redis,如果redis存在单点故障,将会影响到spring session.
在redis层面做高可用,keepalived or Sentinel or  redis3.0 cluster

4.浏览器不支持 cookie 怎么办?
Spring session默认将token写道cookie中,如果浏览器不支持cookie,那将token写到header中,每次请求带上。而外需要在sdk-session.xml增加配置:
<beanname="headerHttpSessionStrategy" class="org.springframework.session.web.http.HeaderHttpSessionStrategy">
</bean>

请求时只要将后端返回的token带上就可以
如:x-auth-token:98cc9e5e-5fc6-4ba9-8d7a-d6677a101820

5.session的失效时间设置问题?
Spring session 默认失效时间是1800秒,也就是30分钟,sdk-session.xml配置参数:
<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
       <property name="maxInactiveIntervalInSeconds" value="1800"></property>
    </bean>
6.Session 事件监听配置HttpSessionListener
SessionListenerDemo实现HttpSessionListener 接口,sdk-session.xml配置参数:

<bean id="listenerAdapter" class="org.springframework.session.web.http.SessionEventHttpSessionListenerAdapter">  
        <constructor-arg name="listeners">  
            <list>  
                <bean class="com.mogoroom.bs.listener.SessionListenerDemo" />  
            </list>  
        </constructor-arg>  
    </bean>
7.跨域问题?
cookie方式:
不同域下所带的cookie是不同的,cookie不支持跨域

Header方式:
请求时只要将后端返回的token带上就可以
如:x-auth-token:98cc9e5e-5fc6-4ba9-8d7a-d6677a101820

8.同域不同工程名问题?
如www.baidu.com/aaa 和 www.baidu.com/bbb 共享session 需要而外在sdk-session.xml心中配置

  <bean  class="org.springframework.session.web.http.CookieHttpSessionStrategy">
        <property name="cookieSerializer" ref="defaultCookieSerializer"></property>
     </bean>
     
     <bean id="defaultCookieSerializer" class="org.springframework.session.web.http.DefaultCookieSerializer">
       <property name="cookiePath" value="/"></property>
     </bean>

猜你喜欢

转载自m635674608.iteye.com/blog/2399093