Spring MVC Web 快速接入 Cas 认证

cas-client-core 是 Cas 官方提供的客户端 SDK,可快速的实现统一身份认证平台的集成!

一、快速接入

第1步:引入依赖

在项目依赖管理中引入 cas-client-core 依赖 :

Maven 依赖
<!-- https://mvnrepository.com/artifact/org.jasig.cas.client/cas-client-core -->
<dependency>
    <groupId>org.jasig.cas.client</groupId>
    <artifactId>cas-client-core</artifactId>
    <version>3.6.4</version>
</dependency>
Gradle 依赖
// https://mvnrepository.com/artifact/org.jasig.cas.client/cas-client-core
implementation group: 'org.jasig.cas.client', name: 'cas-client-core', version: '3.6.4'
第2步:项目配置

webapp 类型项目配置,修改web.xml,按照提示修改正确

<listener>
  <listener-class>
    org.jasig.cas.client.session.SingleSignOutHttpSessionListener
  </listener-class>
</listener>
<!--统一注销过滤器,最好配置在系统过滤器前面-->
<filter>
  <filter-name>CAS Single Sign Out Filter</filter-name>
  <filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>CAS Single Sign Out Filter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
<!--认证过滤器配置开始-->
<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>https://cas_server_url/login</param-value>
  </init-param>
  <!--修改客户端ip和端口-->
  <init-param>
    <param-name>serverName</param-name>
    <!--此处修改为应用服务器地址,http://ip:端口即可-->
    <param-value>http://ip:端口</param-value>
  </init-param>
  <init-param>
    <param-name>acceptAnyProxy</param-name>
    <param-value>true</param-value>
  </init-param>
</filter>

<filter>
  <filter-name>CAS Validation Filter</filter-name>
  <filter-class>org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>
  <!--配置正式的认证服务端地址-->
  <init-param>
    <param-name>casServerUrlPrefix</param-name>
    <param-value>https://cas_server_url</param-value>
  </init-param>
  <!--修改客户端ip和端口-->
  <init-param>
    <param-name>serverName</param-name>
    <!--此处修改为应用服务器地址,http://ip:端口即可-->
    <param-value>http://ip:端口</param-value>
  </init-param>
</filter>

<filter>
  <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
  <filter-class>org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>
</filter>

<filter>
  <filter-name>CAS Assertion Thread Local Filter</filter-name>
  <filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class>
</filter>

<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-mapping>
  <filter-name>CAS Assertion Thread Local Filter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

<session-config>
  <tracking-mode>COOKIE</tracking-mode>
</session-config>
<!--认证过滤器配置结束-->
作者:Jeebiz  创建时间:2023-09-26 13:06
最后编辑:Jeebiz  更新时间:2024-05-07 20:29