java 集群 内存共享_使用spring security与redis实现session的共享存储和集群部署

一、架构介绍

1)产品采用集群部署,使用redis作为session的共享存储

2)使用spring security作为应用系统安全管理

3)使用spring session作为session管理

4)使用nginx作为负载均衡,tomcat作为应用服务器

5)负载均衡使用round-robbin算法,不使用sticky session

二、web.xml配置

web.xml肯定需要配置的,主要配置两个方面:

1)配置session的存储方式,通过配置该过滤器将session的存储委托给spring-data的redis来存储

2)配置spring security的安全控制

springSessionRepositoryFilter

org.springframework.web.filter.DelegatingFilterProxy

springSessionRepositoryFilter

/*

springSecurityFilterChain

org.springframework.web.filter.DelegatingFilterProxy

注意:springSessionRepositoryFilter必须在springSecurityFilterChain之前,否则配置无效。

三、spring security 配置

1)注意session-fixation-protection 需要设置为 none,否则spring-security在收到NGINX转发请求时,其中一台主机会存在cookie无效的情况。具体原因还不清楚,但这个问题需要注意。

authentication-failure-url="/main/login?error=true"

authentication-success-handler-ref="authenticationSuccessHandler"

authentication-failure-handler-ref="authenticationFailureHandler"

username-parameter="j_username" password-parameter="j_password"

default-target-url="/main/homepage" />

四、spring-data的redis配置

这里使用jedis客户端连接redis服务器,具体配置如下:

class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">

五、session超时控制

class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">

六、NIGNX的集群配置

使用round-robbin 加权重的分配方式upstream tomcats {

#server 172.16.32.251:9090 weight=10;

#server 172.16.32.251:8080 weight=10;

server 172.16.32.252:8888 weight=10;

server 172.16.32.252:8889 weight=10;

}

proxy_pass配置location / {

proxy_pass http://tomcats;

include proxy.conf;

}

proxy_set_headerproxy_set_header   Accept-Encoding '';

proxy_set_header   Host   $host;

proxy_set_header   Referer $http_referer;

proxy_set_header   Cookie $http_cookie;

proxy_set_header   X-Real-IP  $remote_addr;

proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;

小结:

1)其实配置起来挺简单的,但是需要注意的细节还挺多,尤其是NGINX这块

2)spring-security的默认使用cookie作为session产生的策略CookieHttpSessionStrategy,session-fixation-protection的安全保护策略会spring认为cookie无效,暂时不知道是因为NGINX的设置问题还是cookie的设置问题。

3)web.xml中filter的顺序会影响session的共享存储。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值