由于公司需要cas集成微信。但是在网上没有找到相应的示例。然后我就跑到官网上去找了一下CAS怎么集成OAuth的。下面的官网加上我自己的改动。
1、在cas-server-webapp中的pom.xml中加入以下dependency用于支持oauth.
<dependency> <groupId>org.jasig.cas</groupId> <artifactId>cas-server-support-pac4j</artifactId> <version>${cas.version}</version> </dependency>
2、在CAS service端为了把属性传递到CAS client端,我们需要在deployerConfigContext.xml文件中配置以下信息:
<bean id="serviceRegistryDao" class="org.jasig.cas.services.InMemoryServiceRegistryDaoImpl"> <property name="registeredServices"> <list> <bean class="org.jasig.cas.services.RegisteredServiceImpl"> <property name="id" value="0" /> <property name="name" value="HTTP" /> <property name="description" value="Only Allows HTTP Urls" /> <property name="serviceId" value="http://**" /> <property name="evaluationOrder" value="10000001" /> <property name="allowedAttributes"> <list> <!-- weixin --> <value>openid</value> <value>nickname</value> <value>and so on</value> ...
3、在cas-server-support-pac4j项目的pom.xml增加必需的pac4j-* libraries
<dependency> <groupId>org.pac4j</groupId> <artifactId>pac4j-oauth</artifactId> <version>${pac4j.version}</version> </dependency>
4、在applicationContext.xml添加对应的oauth的clients。并把clients增加到对应的org.pac4j.core.client.Clients中,同样也是在applicationContext.xml
<bean id="clients" class="org.pac4j.core.client.Clients"> <property name="callbackUrl" value="https://login.nmall.com/cas/login" /> <property name="clients"> <list> <ref bean="weiXin" /> <ref bean="qq" /> </list> </property> </bean> <bean id="weiXin" class="org.jasig.cas.support.pac4j.plugin.weixin.WeiXinClient"> <property name="key" value="yourkey" /> <property name="secret" value="yousecret" /> </bean> <bean id="qq" class="org.jasig.cas.support.pac4j.plugin.qq.QqClient"> <property name="key" value="yourkey" /> <property name="secret" value="yousecret" /> </bean>
5、把处理oauth的client action添加到webflow中在login-webflow.xml中,这clientAction添加在webflow的最前面.它的任务是微信oauth用户验证的callback的调用.
<action-state id="clientAction"> <evaluate expression="clientAction" /> <transition on="success" to="sendTicketGrantingTicket" /> <transition on="error" to="ticketGrantingTicketCheck" /> <transition on="stop" to="stopWebflow" /> </action-state> <view-state id="stopWebflow" />
clientAction这个bean必须定义在cas-servlet.xml,并且需要注入clients.
<bean id="clientAction" class="org.jasig.cas.support.pac4j.web.flow.ClientAction">
<constructor-arg index="0" ref="centralAuthenticationSe