先说多种方式登录的问题,这里使用手机号码和用户名
百度了半天网上能找到的办法都试了,似乎都不好使,要不就是需要修改源码
经过反复查看cas4.0源码,发现可以配置多个身份认证器,而且可以配置多个身份认证器的策略,这里用org.jasig.cas.authentication.AnyAuthenticationPolicy,即任何一个认证器通过就算通过。
这就好办多了,一切问题都好解决,废话不多说,直接上代码
下面是deployerConfigContext.xml
增加一个身份认证器
<bean id="authenticationManager" class="org.jasig.cas.authentication.PolicyBasedAuthenticationManager">
<constructor-arg>
<map>
<entry key-ref="proxyAuthenticationHandler" value-ref="proxyPrincipalResolver" />
<entry key-ref="primaryAuthenticationHandler" value-ref="primaryPrincipalResolver" />
<!--新增的认证器 注意key不能重复,并且key是一个bean-->
<entry key-ref="secondaryAuthenticationHandler" value-ref="secondaryPrincipalResolver" />
</map>
</constructor-arg>
<!--多个认证器的认证策略-->
<property name="authenticationPolicy">
<bean class="org.jasig.cas.authentication.AnyAuthenticationPolicy" />
</property>
</bean>
<bean id="primaryPrincipalResolver"
class="org.jasig.cas.authentication.principal.PersonDirectoryPrincipalResolver" >
<property name="attributeRepository" ref="attributeRepository" />
</bean>
<!--新增的身份认处理器-->
<bean id="secondaryPrincipalResolver"
class="org.jasig.cas.authentication.principal.PersonDirectoryPrincipalResolver" >
<property name="attributeRepository" ref="attributeRepository2" />
</bean>
<bean id="attributeRepository" class="org.jasig.services.persondir.support.jdbc.SingleRowJdbcPersonAttributeDao">
<constructor-arg index="0" ref="dataSource"/>
<constructor-arg index="1" value="select * from USER_INFO where {0}"/>
<property name="queryAttributeMapping">
<map>
<!--这里的key需写username,value对应数据库字段-->
<!--客户端获取信息的时候带过来的用户身份标识对应的用户uid-->
<entry key="username" value="USERNAME"/>
</map>
</property>
<property name="queryType">
<value>OR</value>
</property>
<property name="resultAttributeMapping">
<map>
<!--key为对应的数据库字段名称,value为提供给客户端获取的属性名字,系统会自动填充值-->
<entry key="USERID" value="userId"/>
<entry key="MOBILE" value="mobile"/>
</map>
</property>
</bean>
<bean id="attributeRepository2" class="org.jasig.services.persondir.support.jdbc.SingleRowJdbcPersonAttributeDao">
<constructor-arg index="0" ref="dataSource"/>
<constructor-arg index="1" value="select * from USER_INFO where {0}"/>
<property name="queryAttributeMapping">
<map>
<!--这里的key需写username,value对应数据库字段-->
<!--客户端获取信息的时候带过来的用户身份标识对应的用户uid-->
<entry key="username" value="MOBILE"/>
</map>
</property>
<property name="queryType">
<value>OR</value>
</property>
<property name="resultAttributeMapping">
<map>
<!--key为对应的数据库字段名称,value为提供给客户端获取的属性名字,系统会自动填充值-->
<entry key="USERID" value="userId"/>
<entry key="USERTEL" value="userTel"/>
</map>
</property>
</bean>
<bean id="primaryAuthenticationHandler"
class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler"
p:dataSource-ref="dataSource"
p:passwordEncoder-ref="ignoreCasePasswordEncoder"
p:sql="select USERPASSWORD from USER_INFO where USERNAME=?" />
<bean id="secondaryAuthenticationHandler"
class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler"
p:dataSource-ref="dataSource"
p:passwordEncoder-ref="ignoreCasePasswordEncoder"
p:sql="select USERPASSWORD from USER_INFO where MOBILE=?" />
到这里就搞定了手机号码/用户名登录的问题,然而,又出现了个问题,这里查询了两次数据库登录时间有所延长,报了个错如下:
Cannot create a session after the response has been committed
继续百度,有人遇到过,是因为坑爹的cas在登录后两秒把session给end了,导致session获取不到,解决办法是在cas-servlet.xml 配置timeToDieInSeconds这个值
把时间设置长一些就好了
<bean id="terminateWebSessionListener" class="org.jasig.cas.web.flow.TerminateWebSessionListener"p:timeToDieInSeconds="60"/>