使用nginx-sticky实现CAS集群
参考文档http://www.ttlsa.com/nginx/nginx-modules-nginx-sticky-module/
简介:这种方法实现CAS集群是当应用请求通过nginx转发到认证中心时候,会在客服端cookie中存入一个md5码,nginx通过这个md5码对应后后台的CAS上,以后所有的认证服务都会在同一台CAS上。
1. nginx配置
注意事项
在安装nginx-sticky-module-1.1之前要修改一下内容:
把ngx_http_sticky_misc.c 的281行修改如下
原digest->len = ngx_sock_ntop(in,digest
->data, len, 1);
改后digest->len = ngx_sock_ntop(in,sizeof(struct sockaddr_in),digest
->data, len, 1);
避免编译模块的时候出现问题。
安装看nginx安装文档
CAS票据存储仓库代码的重写
重写默认的票据仓库,将ticket存储到redis中。
public class RedisTicketRegistry extends AbstractDistributedTicketRegistry {
private static final Logger LOGGER = LoggerFactory
.getLogger(RedisTicketRegistry.class);
@Resource
private RedisDB redisDB;
@Resource
private RedisTemplate<String, Object> redisTemplate;
@Override
protected void updateTicket(Ticket ticket) {
LOGGER.info("updateTicket:is" + ticket);addTicket(ticket);
}
@Override
protected boolean needsCallback() {
return false;
}
@Override
public void addTicket(Ticket ticket) {
String key = ticket.getId() ;
LOGGER.info("key:is" + key);
redisDB.addObject(key, ticket, 60 * 10);
LOGGER.info("\n addTicket--getKey:is" + redisDB.getObject(key));
}
@Override
public Ticket getTicket(String s) {
LOGGER.info("\n getTicket--getKey:is" + redisDB.getObject(s));
Ticket t = (Ticket)redisDB.getObject(s);
if (t != null) {
Ticket a = getProxiedTicketInstance(t);
return getProxiedTicketInstance(t);
}
return null;
}
@Override
public boolean deleteTicket(String s) {
if (s == null) {
return false;
}
LOGGER.info("\n deleteTicket"+s);
redisDB.del(s);
return true;
}
@Override
public Collection<Ticket> getTickets() {
throw new UnsupportedOperationException("GetTickets not supported.");
}
}
修改ticketRegistry.xml
<bean id="ticketRegistry" class="org.jasig.cas.ticket.registry.DefaultTicketRegistry" />
讲默认的票据仓库修改成重写的方法。
session共享集群
- 使用tomcat-redis-session-manager