SpringBoot項目集成第三方CAS-client jar包的方法
在企業(yè)級應(yīng)用開發(fā)中,隨著系統(tǒng)數(shù)量增多,用戶在多系統(tǒng)間頻繁登錄的問題日益凸顯。單點登錄(SSO,Single Sign-On)技術(shù)應(yīng)運而生,而 CAS(Central Authentication Service,中央認證服務(wù))作為主流的 SSO 實現(xiàn)方案,被廣泛用于企業(yè)內(nèi)部系統(tǒng)的統(tǒng)一認證。本文將詳細講解 SpringBoot 項目引入第三方 CAS-client jar 包的完整流程,包括兩種集成方式的深度實操、依賴沖突解決、打包配置優(yōu)化及常見問題排查,幫助開發(fā)者高效完成 SSO 對接。
一、背景:為什么需要 CAS-client?
在傳統(tǒng)多系統(tǒng)架構(gòu)中,用戶需在每個系統(tǒng)單獨注冊、登錄,不僅影響用戶體驗,還增加了系統(tǒng)管理成本。CAS 作為一種開源的 SSO 框架,通過 “中央認證服務(wù)器 + 客戶端” 的架構(gòu),實現(xiàn) “一次登錄,多系統(tǒng)訪問”:
- CAS 服務(wù)器:負責統(tǒng)一認證,存儲用戶賬號密碼,生成認證憑證(Ticket);
- CAS-client:集成在業(yè)務(wù)系統(tǒng)(如 SpringBoot 項目)中,負責與 CAS 服務(wù)器通信,攔截未認證請求,驗證用戶憑證,完成登錄狀態(tài)同步。
本文場景為:某企業(yè)需將 SpringBoot 業(yè)務(wù)系統(tǒng)接入現(xiàn)有 CAS 認證體系,需引入第三方提供的cas-client-core-3.2.1.jar(非 Maven 中央倉庫可直接獲取的依賴),因此需通過自定義方式集成 jar 包。
二、SpringBoot 集成 CAS-client jar 包的兩種核心方式
針對非中央倉庫的第三方 jar 包,SpringBoot 項目通常采用 “本地 Maven 倉庫集成” 或 “項目內(nèi) lib 目錄集成” 兩種方式,以下為詳細實操步驟及拓展說明。
方式 1:本地 Maven 倉庫集成(適合個人調(diào)試 / 單系統(tǒng)開發(fā))
將第三方 jar 包安裝到本地 Maven 倉庫,再通過標準<dependency>標簽引入,符合 Maven 依賴管理規(guī)范,適合個人開發(fā)或獨立 demo 調(diào)試場景。
1.1 準備工作
- 下載第三方
cas-client-core-3.2.1.jar,存放路徑示例:D:\libs\cas-client-core-3.2.1.jar; - 確認本地 Maven 環(huán)境配置正常(執(zhí)行
mvn -v可查看版本),若使用自定義 Maven 倉庫(非默認.m2/repository),需提前配置settings.xml(路徑:Maven 安裝目錄 /conf/settings.xml)。
1.2 安裝 jar 包到本地倉庫
通過 Maven 命令行工具,執(zhí)行mvn install:install-file命令,將 jar 包安裝到指定倉庫,命令格式及參數(shù)說明如下:
mvn install:install-file -Dfile=D:\libs\cas-client-core-3.2.1.jar # jar包本地絕對路徑 -DgroupId=org.jasig.cas.client # 依賴的groupId(需與jar包內(nèi)pom一致) -DartifactId=cas-client-core # 依賴的artifactId(需與jar包內(nèi)pom一致) -Dversion=3.2.1 # 依賴版本(需與jar包版本一致) -Dpackaging=jar # 打包類型(jar/war) -DlocalRepositoryPath=E:\maven-repo # 可選:指定安裝到的本地倉庫路徑(多倉庫場景必配)
- 參數(shù)說明:若不指定
-DlocalRepositoryPath,jar 包將默認安裝到user.home/.m2/repository;多倉庫場景下,需確保該路徑與項目使用的 Maven 倉庫一致(可在 IDE 中查看項目 Maven 配置)。
1.3 項目 pom.xml 引入依賴
安裝完成后,在 SpringBoot 項目的pom.xml中添加標準依賴,無需額外配置路徑:
<!-- CAS-client依賴(本地倉庫已存在) -->
<dependency>
<groupId>org.jasig.cas.client</groupId>
<artifactId>cas-client-core</artifactId>
<version>3.2.1</version>
</dependency>1.4 多 Maven 倉庫場景配置補充
若項目同時配置了本地倉庫、私有倉庫(如 Nexus),需在pom.xml或settings.xml中指定倉庫優(yōu)先級,避免依賴拉取失敗。示例:在settings.xml中配置倉庫順序:
<profiles>
<profile>
<id>maven-repo</id>
<repositories>
<!-- 優(yōu)先從自定義本地倉庫拉取 -->
<repository>
<id>local-repo</id>
<url>file://E:\maven-repo</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<!-- 其次從私有倉庫拉取 -->
<repository>
<id>nexus-repo</id>
<url>http://192.168.1.100:8081/repository/maven-public/</url>
</repository>
</repositories>
</profile>
</profiles>
<!-- 激活配置 -->
<activeProfiles>
<activeProfile>maven-repo</activeProfile>
</activeProfiles>方式 2:項目內(nèi) lib 目錄集成(適合團隊協(xié)同開發(fā))
將 jar 包直接放在項目目錄下的lib文件夾中,通過system scope 指定路徑,避免團隊成員重復(fù)安裝本地倉庫,適合多人協(xié)同開發(fā)場景。
2.1 新建項目 lib 目錄
在 SpringBoot 項目根目錄(與src、pom.xml同級)新建lib文件夾,將cas-client-core-3.2.1.jar復(fù)制到該目錄下,目錄結(jié)構(gòu)如下:
your-springboot-project/ ├─ src/ ├─ lib/ │ └─ cas-client-core-3.2.1.jar # 第三方CAS-client jar包 ├─ pom.xml └─ .mvn/
2.2 獲取依賴的 groupId/artifactId(關(guān)鍵步驟)
pom.xml中system scope 依賴需準確配置groupId和artifactId,需從 jar 包內(nèi)的 pom 文件中獲取,步驟如下:
- 右鍵
cas-client-core-3.2.1.jar,用壓縮軟件(如 WinRAR、7-Zip)打開; - 在 jar 包根目錄中找到
pom.xml文件,解壓到本地; - 打開解壓后的
pom.xml,查找以下配置(示例):
<modelVersion>4.0.0</modelVersion> <groupId>org.jasig.cas.client</groupId> <!-- 需復(fù)制的groupId --> <artifactId>cas-client-core</artifactId> <!-- 需復(fù)制的artifactId --> <version>3.2.1</version> <packaging>jar</packaging> <name>Jasig CAS Client for Java - Core</name>
復(fù)制上述groupId和artifactId,用于項目pom.xml配置。
2.3 pom.xml 配置 system scope 依賴
在pom.xml中添加依賴,通過${basedir}變量(表示項目根目錄)指定systemPath,避免硬編碼路徑:
<!-- CAS-client依賴(項目lib目錄) -->
<dependency>
<groupId>org.jasig.cas.client</groupId>
<artifactId>cas-client-core</artifactId>
<version>3.2.1</version>
<scope>system</scope> <!-- 系統(tǒng)級依賴,需指定本地路徑 -->
<!-- 路徑:項目根目錄/lib/cas-client-core-3.2.1.jar -->
<systemPath>${basedir}/lib/cas-client-core-3.2.1.jar</systemPath>
</dependency>- 路徑說明:若
lib目錄放在src/main/resources下,systemPath需改為${basedir}/src/main/resources/lib/cas-client-core-3.2.1.jar,需根據(jù)實際目錄調(diào)整。
三、依賴沖突深度解決(不止于 Shiro-CAS)
實際開發(fā)中還可能遇到版本不一致、重復(fù)引入等問題,需系統(tǒng)化排查與解決。
3.1 查看依賴樹,定位沖突來源
通過 Maven 命令查看項目完整依賴樹,找到引入cas-client-core的所有依賴:
mvn dependency:tree -Dincludes=org.jasig.cas.client:cas-client-core
執(zhí)行后會輸出類似結(jié)果:
[INFO] com.example:springboot-sso:jar:1.0.0
[INFO] +- org.apache.shiro:shiro-cas:jar:1.7.1:compile
[INFO] | \- org.jasig.cas.client:cas-client-core:jar:3.1.10:compile # 沖突版本
[INFO] \- org.jasig.cas.client:cas-client-core:jar:3.2.1:system # 我們引入的版本
可見shiro-cas默認引入了3.1.10版本,與我們需要的3.2.1沖突。
3.2 排除沖突依賴
在引入沖突依賴(如shiro-cas)時,通過<exclusions>標簽排除其自帶的cas-client-core:
<!-- Shiro-CAS依賴,排除內(nèi)置的cas-client-core -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-cas</artifactId>
<version>1.7.1</version>
<exclusions>
<!-- 排除沖突的cas-client-core版本 -->
<exclusion>
<groupId>org.jasig.cas.client</groupId>
<artifactId>cas-client-core</artifactId>
</exclusion>
</exclusions>
</dependency>- 拓展場景:若多個依賴均引入
cas-client-core,需在所有沖突依賴中排除,確保項目僅使用我們指定的版本。
四、打包配置:確保第三方 jar 包被包含(多插件支持)
Maven 默認不打包system scope 的依賴,需在打包插件中配置includeSystemScope=true,否則項目部署后會出現(xiàn)ClassNotFoundException。以下為兩種主流打包插件的配置方案。
4.1 SpringBoot 默認打包插件(spring-boot-maven-plugin)
配置如下(需放在pom.xml的<build><plugins>中):
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.7.0</version> <!-- 與項目SpringBoot版本一致 -->
<configuration>
<!-- 關(guān)鍵配置:包含system scope的依賴 -->
<includeSystemScope>true</includeSystemScope>
<!-- 可選:指定主啟動類 -->
<mainClass>com.example.SpringBootSsoApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal> <!-- 重新打包成可執(zhí)行jar -->
</goals>
</execution>
</executions>
</plugin>4.2 Maven Assembly 插件(適合多模塊打包)
若項目為多模塊架構(gòu),或需自定義打包結(jié)構(gòu),可使用maven-assembly-plugin,配置如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.4.2</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef> <!-- 打包所有依賴 -->
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.example.SpringBootSsoApplication</mainClass> <!-- 主啟動類 -->
</manifest>
</archive>
<!-- 包含system scope依賴 -->
<includeSystemScope>true</includeSystemScope>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>五、常見問題排查與解決方案
5.1 問題 1:啟動報錯 ClassNotFoundException: org.jasig.cas.client.authentication.AuthenticationFilter
可能原因:
systemPath路徑配置錯誤;- 打包時未配置
includeSystemScope=true; - jar 包損壞或版本不匹配。
排查步驟:
- 驗證
systemPath正確性:在 IDE 中點擊${basedir}/lib/cas-client-core-3.2.1.jar,查看是否能跳轉(zhuǎn)至 jar 包; - 檢查打包后的 jar:用壓縮軟件打開目標 jar,查看
BOOT-INF/lib下是否包含cas-client-core-3.2.1.jar; - 重新下載 jar 包:若 jar 包損壞,替換為正常版本。
5.2 問題 2:Maven 倉庫安裝后,項目仍無法拉取依賴
可能原因:
- 安裝路徑與項目使用的 Maven 倉庫不一致;
groupId/artifactId/version與安裝命令不一致。
排查步驟:
- 查看項目 Maven 配置:在 IDE(如 IDEA)中,進入
File > Settings > Build, Execution, Deployment > Build Tools > Maven,確認Local repository路徑與安裝命令中的-DlocalRepositoryPath一致; - 核對依賴坐標:確保
pom.xml中的groupId、artifactId、version與mvn install命令中的參數(shù)完全一致(大小寫敏感)。
六、CAS-client 初步配置與使用(拓展實操)
引入 jar 包后,需簡單配置 CAS-client 才能實現(xiàn) SSO 功能,以下為 SpringBoot 中的基礎(chǔ)配置(application.yml):
# CAS服務(wù)器配置
cas:
server-url-prefix: http://cas.example.com/cas # CAS服務(wù)器地址前綴
server-login-url: http://cas.example.com/cas/login # CAS登錄頁面地址
client-host-url: http://localhost:8080 # 當前SpringBoot項目地址(回調(diào)地址基礎(chǔ))
# CAS-client過濾器配置(SpringBoot方式)
spring:
servlet:
filter:
cas:
authentication-filter:
enabled: true
url-pattern: /* # 攔截所有請求
validation-filter:
enabled: true
url-pattern: /*
ticket-validation-filter:
enabled: true
url-pattern: /*同時,需在啟動類中注冊 CAS 相關(guān)過濾器(示例):
@SpringBootApplication
public class SpringBootSsoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootSsoApplication.class, args);
}
// 注冊CAS認證過濾器
@Bean
public FilterRegistrationBean<AuthenticationFilter> authenticationFilter() {
FilterRegistrationBean<AuthenticationFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new AuthenticationFilter());
registrationBean.addUrlPatterns("/*");
Map<String, String> initParams = new HashMap<>();
initParams.put("casServerLoginUrl", "${cas.server-login-url}");
initParams.put("serverName", "${cas.client-host-url}");
registrationBean.setInitParameters(initParams);
return registrationBean;
}
}七、兩種集成方式對比與選型建議
| 對比維度 | 方式 1:本地 Maven 倉庫 | 方式 2:項目內(nèi) lib 目錄 |
|---|---|---|
| 適用場景 | 個人開發(fā)、demo 調(diào)試 | 團隊協(xié)同開發(fā)、多人共享依賴 |
| 操作復(fù)雜度 | 需執(zhí)行命令安裝,首次配置稍復(fù)雜 | 直接復(fù)制 jar 包,配置簡單 |
| 依賴管理 | 符合 Maven 規(guī)范,便于版本控制 | 依賴脫離 Maven 倉庫,需手動維護版本 |
| 團隊協(xié)作 | 成員需重復(fù)安裝到本地倉庫,效率低 | 提交 lib 目錄到 Git,成員拉取即可使用 |
| 選型建議: |
- 個人開發(fā)或短期 demo:優(yōu)先選擇方式 1,減少項目目錄冗余;
- 團隊協(xié)同或長期項目:優(yōu)先選擇方式 2,或進階方案 —— 搭建私有 Maven 倉庫(如 Nexus),將第三方 jar 包上傳至私有倉庫,團隊成員通過標準依賴引入,兼顧規(guī)范與協(xié)作效率。
八、總結(jié)
本文詳細講解了 SpringBoot 項目集成第三方 CAS-client jar 包的兩種核心方式,從背景原理到實操步驟,再到?jīng)_突解決、打包配置及問題排查,覆蓋了企業(yè)級開發(fā)中的關(guān)鍵場景。在實際項目中,需根據(jù)團隊協(xié)作模式和項目需求選擇合適的集成方式,同時注重依賴管理的規(guī)范性,避免版本沖突和部署問題。后續(xù)可進一步學習 CAS 的高級功能(如單點登出、多因素認證),為企業(yè)應(yīng)用構(gòu)建更安全、更完善的統(tǒng)一認證體系。
相關(guān)文章
Spring Security實現(xiàn)驗證碼登錄功能
這篇文章主要介紹了Spring Security實現(xiàn)驗證碼登錄功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-01-01
SpringBoot整合EasyExcel進行大數(shù)據(jù)處理的方法詳解
EasyExcel是一個基于Java的簡單、省內(nèi)存的讀寫Excel的開源項目。在盡可能節(jié)約內(nèi)存的情況下支持讀寫百M的Excel。本文將在SpringBoot中整合EasyExcel進行大數(shù)據(jù)處理,感興趣的可以了解一下2022-05-05
idea中創(chuàng)建maven的Javaweb工程并進行配置(圖文教程)
這篇文章主要介紹了idea中創(chuàng)建maven的Javaweb工程并進行配置,本文通過圖文并茂的形式給大家介紹的非常詳細,文中給大家提到了tomcat的運行方法,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02
使用自定義參數(shù)解析器同一個參數(shù)支持多種Content-Type
這篇文章主要介紹了使用自定義參數(shù)解析器同一個參數(shù)支持多種Content-Type的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
Java 調(diào)用 HTTP 接口的 7 種方式示例代碼(全網(wǎng)最全指南)
在開發(fā)過程中,調(diào)用 HTTP 接口是最常見的需求之一,本文將詳細介紹 Java 中 7 種主流的調(diào)用 HTTP 接口的方式,包括每種工具的優(yōu)缺點和完整代碼實現(xiàn),感興趣的朋友一起看看吧2025-02-02

