Struts2框架使用CAS,充当CAS-Client端

胡新
2023-12-01

前言

最近在学习sso(单点登入),通过cas来实现。由于我们使用的是Struts2框架,所以我希望Struts2项目充当client端。奈何现在官方给的示例都是基于Spring Mvc、SpringBoot的。查了半天才找到些头绪,特写此文。毕竟我也经常看别人的,没有文章很焦灼,哈哈哈哈。。。


一、cas是什么?

cas:https://github.com/apereo/cas
java-cas-client:https://github.com/apereo/java-cas-client
sso-cas基础知识概括:https://juejin.im/post/6844903509272297480#heading-13

二、使用

1.Maven引入

代码如下(示例):

		<!--cas客户端 -->
		<dependency>
			<groupId>org.jasig.cas.client</groupId>
			<artifactId>cas-client-core</artifactId>
			<version>3.4.1</version>
		</dependency>

2.web.xml配置

代码如下(示例):

<!-- Struts2的过滤器配置 -->
	<filter>
		<filter-name>struts-prepare</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class>		
	</filter>
	<!-- Struts2过滤器拦截所有的.action请求 -->
	<filter-mapping>
		<filter-name>struts-prepare</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!--登出过滤器 -->
	<filter>
		<filter-name>CAS Single Sign Out Filter</filter-name>
		<filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>
		<init-param>
			<param-name>casServerUrlPrefix</param-name>
			<param-value>http://127.0.0.1:8443/cas</param-value>
		</init-param>
	</filter>

	<!--登出监听器 -->
	<listener>
		<listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
	</listener>

	<!--认证过滤器 -->
	<filter>
		<filter-name>CAS Authentication Filter</filter-name>
		<filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
		<init-param>
			<param-name>casServerLoginUrl</param-name>
			<param-value>http://127.0.0.1:8443/cas/login</param-value>
		</init-param>
		<init-param>
			<param-name>serverName</param-name>
			<param-value>http://127.0.0.1:8888</param-value>
		</init-param>
		<!--忽略验证的url,多个url使用"|"分割 -->
		<init-param>
			<param-name>ignorePattern</param-name>
			<param-value>/logout/success</param-value>
		</init-param>
	</filter>

	<!--ticke验证过滤器 -->
	<filter>
		<filter-name>CAS Validation Filter</filter-name>
		<filter-class>org.jasig.cas.client.validation.Cas30ProxyReceivingTicketValidationFilter</filter-class>
		<init-param>
			<param-name>casServerUrlPrefix</param-name>
			<param-value>http://127.0.0.1:8443/cas</param-value>
		</init-param>
		<init-param>
			<param-name>serverName</param-name>
			<param-value>http://127.0.0.1:8888</param-value>
		</init-param>
		<init-param>
			<param-name>redirectAfterValidation</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>useSession</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>authn_method</param-name>
			<param-value>mfa-duo</param-value>
		</init-param>
	</filter>

	<!--wrapper过滤器 -->
	<filter>
		<filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
		<filter-class>org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>
	</filter>



	<filter-mapping>
		<filter-name>CAS Single Sign Out Filter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<filter-mapping>
		<filter-name>CAS Validation Filter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<filter-mapping>
		<filter-name>CAS Authentication Filter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<filter-mapping>
		<filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<filter>
        <filter-name>struts-execute</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts-execute</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

启动cas服务端,再启动本项目,访问就能联动cas了。至于为什么,还是看这篇文章吧。struts2自定义过滤器
部署cas服务端请看:https://blog.csdn.net/oumuv/article/details/83377945


总结

还是要自己多去熟悉文档呀,才能从容应对改变。得亏有这些资料 我也就是做个汇总,希望能帮到诸位。

 类似资料: