賈琳 一名系統架構師的征途
阿新 • • 發佈:2018-12-24
TGT、ST資料共享
多臺CAS伺服器共享TGT、ST資料,由於TGT、ST資料沒有儲存到SESSION中,所以需要單獨處理TGT、ST資料的共享。可採用的方式有多種,例如:redis、memcached等NOSQL資料庫,登入量小的可以選擇關係型資料庫等。Redis:
1、繼承AbstractDistributedTicketRegistry類,重寫ticket的增刪改查方法
import java.util.Collection; import org.apache.commons.lang.SerializationUtils; import org.jasig.cas.ticket.Ticket; import org.jasig.cas.ticket.TicketGrantingTicket; import org.jasig.cas.ticket.registry.AbstractDistributedTicketRegistry; import redis.clients.jedis.Jedis; public class RedisTicketRegistry extends AbstractDistributedTicketRegistry { //st ticket 最大空閒時間 private int stMaxFreeTime; //tgt ticket 最大空閒時間 private int tgtMaxFreeTime; private static Jedis jedis = new Jedis("localhost"); /** * 新增票據 * Description * @param ticket * @see org.jasig.cas.ticket.registry.TicketRegistry#addTicket(org.jasig.cas.ticket.Ticket) */ @Override public void addTicket(Ticket ticket) { // TODO Auto-generated method stub String key = ticket.getId(); int seconds = 0; if(ticket instanceof TicketGrantingTicket){ seconds = this.getTgtMaxFreeTime(); } else { seconds = this.getStMaxFreeTime(); } System.out.println("---------------add Tikcet into redis------------------- start"); jedis.setex(SerializationUtils.serialize(key), seconds, SerializationUtils.serialize(ticket)); System.out.println("---------------add Ticket into redis------------------- end"); } /** * 刪除票據 * Description * @param ticketId * @return * @see org.jasig.cas.ticket.registry.TicketRegistry#deleteTicket(java.lang.String) */ @Override public boolean deleteTicket(String ticketId) { // TODO Auto-generated method stub if(ticketId==null){ return false; } System.out.println("----------------delete Ticket from redis-------------- start"); jedis.del(SerializationUtils.serialize(ticketId)); System.out.println("----------------delete Ticket from redis-------------- end"); return true; } /** * 獲取票據 * Description * @param ticketId * @return * @see org.jasig.cas.ticket.registry.TicketRegistry#getTicket(java.lang.String) */ @Override public Ticket getTicket(String ticketId) { // TODO Auto-generated method stub System.out.println("---------------get Ticket byte[] ---------------- start"); byte []tempArr = jedis.get(SerializationUtils.serialize(ticketId)); Ticket ticket = null; if(tempArr!=null){ System.out.println("--------------------deserialize to Ticket--------------- start"); ticket = (Ticket) SerializationUtils.deserialize(tempArr); } System.out.println("-----------------get Tikcet -------------------- end"); return ticket; } @Override public Collection<Ticket> getTickets() { // TODO Auto-generated method stub throw new UnsupportedOperationException("----------GetTickets not supported----------------"); } @Override public boolean needsCallback() { // TODO Auto-generated method stub return false; } /** * 修改票據 * Description * @param ticket * @see org.jasig.cas.ticket.registry.AbstractDistributedTicketRegistry#updateTicket(org.jasig.cas.ticket.Ticket) */ @Override public void updateTicket(Ticket ticket) { // TODO Auto-generated method stub this.addTicket(ticket); } /** * @return the stMaxFreeTime */ public int getStMaxFreeTime() { return stMaxFreeTime; } /** * @param stMaxFreeTime the stMaxFreeTime to set */ public void setStMaxFreeTime(int stMaxFreeTime) { this.stMaxFreeTime = stMaxFreeTime; } /** * @return the tgtMaxFreeTime */ public int getTgtMaxFreeTime() { return tgtMaxFreeTime; } /** * @param tgtMaxFreeTime the tgtMaxFreeTime to set */ public void setTgtMaxFreeTime(int tgtMaxFreeTime) { this.tgtMaxFreeTime = tgtMaxFreeTime; } }
2、修改spring-configuration/ticketRegistry.xml,去掉以前的ticketRegistry,改成自己的
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <description> Configuration for the default TicketRegistry which stores the tickets in-memory and cleans them out as specified intervals. </description> <bean id="ticketRegistry" class="ths.jdp.cas.extend.RedisTicketRegistry" p:stMaxFreeTime="3600" p:tgtMaxFreeTime="3600" /> </beans>
3、cas/WEB-INF/lib/下增加jedis-2.9.0.jar,如果Tomcat採用的redis session共享,Tomcat/lib已存在jedis相關jar
4、CentralAuthenticationServiceImpl類315行增加下圖中所示程式碼,解決Cas叢集時登出bug
314 final ServiceTicket serviceTicket = ticketGrantingTicket.grantServiceTicket(generatedServiceTicketId, service,this.serviceTicketExpirationPolicy, credentials != null);
315 this.serviceTicketRegistry.addTicket(ticketGrantingTicket);
316 this.serviceTicketRegistry.addTicket(serviceTicket);