注意:本系列采用的CAS版本是4.1.xx,其它版本有可能不适用。
1、啰嗦一下原理
在spring中,读取属性文件properties替换占位符可以使用 PropertySourcesPlaceholderConfigurer 或 PropertyPlaceholderConfigurer 两个类,它们的父类 PropertyResourceConfigurer 实现了 BeanFactoryPostProcessor接口,该接口的postProcessBeanFactory方法在spring容器初始化时,将会在注册BeanDefinition后调用,提供了一种动态修改BeanDefinition的属性的方法。BeanDefinition就是对xml文件中,bean定义的封装。 PropertySourcesPlaceholderConfigurer 或 PropertyPlaceholderConfigurer 两个类将会动态替换占位符${}。
cas-server-webapp工程在web.xml中提供了spring的配置文件加载位置
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/cas-servlet.xml, /WEB-INF/cas-servlet-*.xml</param-value>
</init-param>
看一下WEB-INF/cas-servlet.xml文件,这里引入了其它的配置文件 propertyFileConfigurer.xml
<import resource="spring-configuration/propertyFileConfigurer.xml"/>
看一下 propertyFileConfigurer.xml,这里给出了配置文件的引入地址
<util:properties id="casProperties" location="${cas.properties.filepath:/WEB-INF/cas.properties}"/>
<context:property-placeholder properties-ref="casProperties"/>
我们可以知道,/WEB-INF/cas.properties就是cas的属性文件。
2、修改配置
2.1 修改默认语言
cas使用了spring mvc的国际化解决方案,即使用了LocaleResolver解析器解析消息,它的配置在/WEB-INF/cas-servlet.xml文件中。p:defaultLocale指定了默认的语言,这里使用了占位符${locale.default},然后在cas.properties文件中添加了需要使用的国际化语言文件解析消息。
<!-- Locale Resolver -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"
p:defaultLocale="${locale.default}" />
${locale.default}需要在cas.properties文件中添加
# 默认语言
locale.default=zh_CN
清单:修改前的登录页
清单:修改后的登录页
2.2 修改默认登录页
请参考 文章《单点登录 - 定制CAS服务器的登录页》 https://my.oschina.net/thinwonton/blog/1417879
2.3 支持HTTP协议
为了支持客户端HTTP协议,需要修改下面4处地方:
(1)修改WEB-INF/deployerConfigContext.xml
<!-- Required for proxy ticket mechanism. -->
<bean id="proxyAuthenticationHandler"
class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"
p:httpClient-ref="supportsTrustStoreSslSocketFactoryHttpClient" p:requireSecure="false" />
<!-- Required for proxy ticket mechanism. -->
<bean id="proxyAuthenticationHandler"
class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"
p:httpClient-ref="supportsTrustStoreSslSocketFactoryHttpClient" p:requireSecure="false" />
(2)修改WEB-INF/spring-configuration/ticketGrantingTicketCookieGenerator.xml
<bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
c:casCookieValueManager-ref="cookieValueManager"
p:cookieSecure="false"
p:cookieMaxAge="-1"
p:cookieName="TGC"
p:cookiePath=""/>
将p:cookieSecure="true"修改为p:cookieSecure="false"
(3)修改WEB-INF/spring-configuration/warnCookieGenerator.xml
<bean id="warnCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
p:cookieHttpOnly="true"
p:cookieSecure="false"
p:cookieMaxAge="-1"
p:cookieName="CASPRIVACY"
p:cookiePath=""/>
将p:cookieSecure="true"修改为p:cookieSecure="false"
(4)修改注册服务WEB-INF/classes/services/HTTPSandIMAPS-10000001.json
"serviceId" : "^(https|imaps)://.*" 修改为
{
"@class" : "org.jasig.cas.services.RegexRegisteredService",
"serviceId" : "^(https|imaps|http)://.(jd.edu|taobao.edu)",
"name" : "HTTPS and IMAPS",
"id" : 10000001,
"description" : "This service definition authorized all application urls that support HTTPS and IMAPS protocols.",
"proxyPolicy" : {
"@class" : "org.jasig.cas.services.RefuseRegisteredServiceProxyPolicy"
}
serviceId 这个属性很重要,它限制了可以访问CAS服务器的URL地址,在生产环境中建议配置下具体支持哪些服务。