CAS票据之ST与TGT过期策略详细说明

转载自:http://blog.yoodb.com/yoodb/article/detail/1225

        CAS的核心就是Ticket中文称之为票据,以及在Ticket之上的一系列逻辑处理操作。CAS的主要包含票据有TGT、ST、PGT、PGTIOU、PT,其中TGT、ST是CAS1.0协议中就有的票据,PGT、PGTIOU、PT是CAS2.0协议中新增的的票据,目前CAS最新版本已经到了CAS4.0。cas分为服务端和客户端两部分组成,下简单说一说CAS认证过程:

1)用户访问cas-client,被拦截跳转到cas-server进行登录,输入正确的用户信息

2)登录成功后,cas-server签发一个TGC票据,写入浏览器同时生成一个TGT对象,放入自己的缓存,TGT对象的ID就是cookie的值,并再次跳转到cas-client,同时携带着ST票据

cas-client发现有ST票据则拿着ST票据去cas-server验证,如果验证通过,则返回用户名信息

3)cas-client登录成功,用户访问另一个cas-client2时,也会被拦截再次跳转到cas-server发现TGC票据生成的TGT对象的ID值存在则直接验证通过,签发一个ST票据给cas-client2。


关于TGT和ST术语解释

•  TGT(Ticket Grangting Ticket)

        TGT是CAS为用户签发的登录票据,拥有了TGT,用户就可以证明自己在CAS成功登录过。TGT封装了Cookie值以及此Cookie值对应的用户信息。用户在CAS认证成功后,CAS生成cookie(叫TGC),写入浏览器,同时生成一个TGT对象,放入自己的缓存,TGT对象的ID就是cookie的值。当HTTP再次请求到来时,如果传过来的有CAS生成的cookie,则CAS以此cookie值为key查询缓存中有无TGT ,如果有的话,则说明用户之前登录过,如果没有,则用户需要重新登录。

•  ST(Service Ticket)

        ST是CAS为用户签发的访问某一service的票据。用户访问service时,service发现用户没有ST,则要求用户去CAS获取ST。用户向CAS发出获取ST的请求,如果用户的请求中包含cookie,则CAS会以此cookie值为key查询缓存中有无TGT,如果存在TGT,则用此TGT签发一个ST,返回给用户。用户凭借ST去访问service,service拿ST去CAS验证,验证通过后,允许用户访问资源。


下面为大家说一说ST票据和TGT票据的过期策略,上述已经简单介绍ST票据和TGT票据的含义,这里就不做说明了,打开cas-server工程查找ticketExpirationPolicies.xml配置文件,配置具体内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?xml version= "1.0"  encoding= "UTF-8" ?>
<!--
     Licensed to Jasig under one or  more  contributor license
     agreements. See the NOTICE  file  distributed with this work
     for  additional information regarding copyright ownership.
     Jasig licenses this  file  to you under the Apache License,
     Version 2.0 (the  "License" ); you may not use this  file
     except  in  compliance with the License.  You may obtain a
     copy of the License at the following location:
       http: //www .apache.org /licenses/LICENSE-2 .0
     Unless required by applicable law or agreed to  in  writing,
     software distributed under the License is distributed on an
     "AS IS"  BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     KIND, either express or implied.  See the License  for  the
     specific language governing permissions and limitations
     under the License.
-->
<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"
        xmlns:c= "http://www.springframework.org/schema/c"  xmlns:util= "http://www.springframework.org/schema/util"
        xsi:schemaLocation="http: //www .springframework.org /schema/beans
                            http: //www .springframework.org /schema/beans/spring-beans .xsd
                            http: //www .springframework.org /schema/util
                            http: //www .springframework.org /schema/util/spring-util .xsd">
     <description>
         Assignment of expiration policies  for  the different tickets generated by CAS including ticket granting ticket
         (TGT), service ticket (ST), proxy granting ticket (PGT), and proxy ticket (PT).
         These expiration policies determine how long the ticket they are assigned to can be used and even how often they
         can be used before becoming expired / invalid.
     < /description >
     <!-- Expiration policies -->
     <util:constant  id = "SECONDS"  static-field= "java.util.concurrent.TimeUnit.SECONDS" />
     <bean  id = "serviceTicketExpirationPolicy"  class= "org.jasig.cas.ticket.support.MultiTimeUseOrTimeoutExpirationPolicy"
           c:numberOfUses= "1"  c:timeToKill= "${st.timeToKillInSeconds:10}"  c:timeUnit-ref= "SECONDS" />
     <!-- TicketGrantingTicketExpirationPolicy: Default as of 3.5 -->
     <!-- Provides both idle and hard timeouts,  for  instance 2 hour sliding window with an 8 hour max lifetime -->
     <bean  id = "grantingTicketExpirationPolicy"  class= "org.jasig.cas.ticket.support.TicketGrantingTicketExpirationPolicy"
           p:maxTimeToLiveInSeconds= "${tgt.maxTimeToLiveInSeconds:28800}"
           p:timeToKillInSeconds= "${tgt.timeToKillInSeconds:7200}" />
< /beans >


TGT票据过期配置,默认时间是两小时,当用户在2个小时(7200秒)之内不动移动鼠标或者进行系统超过8个小时(28800秒),则tgt过期,具体配置如下:

1
2
3
4
<bean  id = "grantingTicketExpirationPolicy"  class= "org.jasig.cas.ticket.support.TicketGrantingTicketExpirationPolicy"
           p:maxTimeToLiveInSeconds= "${tgt.maxTimeToLiveInSeconds:28800}"
           p:timeToKillInSeconds= "${tgt.timeToKillInSeconds:7200}" />
< /beans >


ST票据过期配置,默认时间是10秒钟,使用次数为1 次或者超过10秒没有应用均会引起st过期,具体配置如下:

1
2
<bean  id = "serviceTicketExpirationPolicy"  class= "org.jasig.cas.ticket.support.MultiTimeUseOrTimeoutExpirationPolicy"
           c:numberOfUses= "1"  c:timeToKill= "${st.timeToKillInSeconds:10}"  c:timeUnit-ref= "SECONDS" />





推荐↓↓↓↓↓↓ 
这里写图片描述

更多推荐:微信公众号《优哉游哉》 
关注微信公众号“优哉游哉”(w_z90110),回复关键字领取资料:如Hadoop,Dubbo,CAS源码等等,免费领取资料视频和项目等。 
微信公众号涵盖:程序人生、搞笑视频、算法与数据结构、黑客技术与网络安全、前端开发JavaPythonRedis缓存、spring源码、各大主流框架、Web开发、大数据技术、Storm、Hadoop、MapReduce、Spark、elasticsearch、单点登录统一认证、分布式框架、集群、安卓开发、iOS开发、C/C++、.NET、LinuxMySQLOracle、NoSQL非关系型数据库、运维等。


发布了35 篇原创文章 · 获赞 48 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/afreon/article/details/53183157