CAS进行https到http的改造方案,结合cookie源码分析

时间:2023-03-08 22:21:15
CAS进行https到http的改造方案,结合cookie源码分析

先说具体的改造方案:

服务端:

一、CAS Server端的修改
1、找到cas\WEB-INF\deployerConfigContext.xml
对以下Bean增加参数p:requireSecure="false",该参数表示是否需要安全验证,即HTTPS,false为不采用,加上去之后如下:
<!-- Required for proxy ticket mechanism. -->
<bean id="proxyAuthenticationHandler"
class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"
p:httpClient-ref="httpClient"
p:requireSecure="false" />

2、找到cas\WEB-INF\spring-configuration\ticketGrantingTicketCookieGenerator.xml

<bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
p:cookieSecure="false"
p:cookieMaxAge="-1"
p:cookieName="CASTGC"
p:cookiePath="/cas" />
参数p:cookieSecure="true",同理为HTTPS验证相关,true为采用HTTPS验证,与deployerConfigContext.xml的参数保持一致。
参数p:cookieMaxAge="-1",简单说是COOKIE的最大生命周期,-1为无生命周期,即只在当前打开的IE窗口有效,IE关闭或重新打开其它窗口,仍会要求验证。可以根据需要修改为大于0的数字,比如3600等,意思是在3600秒内,打开任意IE窗口,都不需要验证。
3、找到cas\WEB-INF\spring-configuration\warnCookieGenerator.xml
<bean id="warnCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
p:cookieSecure="false"
p:cookieMaxAge="-1"
p:cookieName="CASPRIVACY"
p:cookiePath="/cas" />
两个参数与上面同理。应保持一致。

改造的原因:

为什么要吧cookieSecure的属性设置成false呢?让我们去Cookie.class的源码里面去看一下:
    /**
* Indicates to the browser whether the cookie should only be sent
* using a secure protocol, such as HTTPS or SSL.
*
* <p>The default value is <code>false</code>.
*
* @param flag if <code>true</code>, sends the cookie from the browser
* to the server only when using a secure protocol;
* if <code>false</code>, sent on any protocol
*
* @see #getSecure
*
*/ public void setSecure(boolean flag) {
secure = flag;
}

看一下方法前面的注释就明白了。如果设置成true,那么只有使用了安全协议才会从浏览器把cookie发送给server。默认是设置成false的。而cas默认是支持https协议的,cas中给默认设置成了true。如果想改造成http的,就得把cookie的这个属性设置成false。

而cas中涉及到cookie相关的就是这两个bean了。

客户端:

把对应filter中的https连接修改成http:
<filter>
<filter-name>CAS Single Sign Out Filter</filter-name>
<filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>
<init-param>
<param-name>casServerLoginUrl</param-name>
<param-value>http://**.**.com/logout</param-value>
<!--这里的server是服务端的IP -->
</init-param>
</filter>
<filter-mapping>
<filter-name>CAS Single Sign Out Filter</filter-name>
<url-pattern>*.htm</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>CAS Single Sign Out Filter</filter-name>
<url-pattern>*.json</url-pattern>
</filter-mapping>

现在就可以走http协议了