iT邦幫忙

2025 iThome 鐵人賽

DAY 4
0
佛心分享-SideProject30

吃出一個SideProject!系列 第 4

Day4:Auth Service - 專案設定與依賴管理

  • 分享至 

  • xImage
  •  

昨天介紹了 Spring Security 框架與驗證、授權的幾個主要功能,今天開始預計要整合這些知識來完成實作的內容!

但在實作前還是要花點篇幅來介紹專案設定與依賴管理,幫助自己了解整個專案的運作機制,也大概知道一下自己未來實作時的功能出自於那些套件。

至於環境建置的過程、編輯器的介紹等內容相信在網路上都有許多前輩已經分享非常完善充足的知識了,本次參賽就跳過該環節,著眼於專案本身的介紹。

專案架構與設定檔的關聯

本專案為多模組 (Multi-module) 的結構,接下來要實作的 Auth Service 為專案下的其中一個服務。

實作時會給予專案層跟每個服務層各自的設定檔(pom.xml),其作用如下:

  • 專案層 pom.xml:負責定義所有子模組都應遵守的共同規範,例如共用的套件版本、Java 版本等。
  • 服務層 pom.xml:僅引入自己所需要的套件,其他模組間共同的資訊,透過專案層的設定前引入。

這麼做的好處,是可以透過在專案層統一管理共同資訊與套件版本,使各服務套件版本保持一致,降低未來進版維護成本。

專案層 pom.xml

本專案設定檔內容與說明如下:

...
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.3.3</version> <relativePath/> 
    </parent>

	<!--整個專案的資訊-->
    <groupId>your-company-name</groupId>
    <artifactId>your-project-name</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <packaging>pom</packaging>

    <properties>
        <java.version>21</java.version>
    </properties>

    <modules>
        <module>auth-service</module>
    </modules>

</project>
  • <parent>:宣告繼承關係。

    以本專案來說,即是繼承了 spring-boot-starter-parent 的設定。除其他建置設定外(如預設的 Java 編譯器版本),最主要想取得其版本管理內容(<dependencyManagement>),其中定義了數百個常用套件庫的推薦版本號,讓我們不用手動管理常見套件的版本,減少相依性問題的發生。

  • <packaging>:定義這個 Maven 模組最終要被打包成什麼形式。

    此處設定為 pom ,表明此模組設定為專案層設定,最後不會被打包。

  • <properties>:定義全專案共用的變數。

    <java.version> 除定義專案本身使用的Java版本外, spring-boot-starter-parent 也會透過這個設定,來調整整個專案的 Java 編譯器原始碼和目標碼版本,進而影響本專案下的子模組。

  • <modules>:宣告子模組

    此處加入了本階段要實作 auth-service

Auth Service pom.xml

本服務設定檔內容與說明如下:

...
    <parent>
    	<!--整個專案的資訊-->
	    <groupId>your-company-name</groupId>
	    <artifactId>your-project-name</artifactId>
	    <version>1.0.0-SNAPSHOT</version>
    </parent>
    
    <!--該服務的名稱,groupId 與 version 自動繼承自專案-->
    <artifactId>auth-service</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  • <parent>
    這裡指向了我們剛剛定義的專案層 pom,表示 auth-service 會繼承專案的設定,包含專案資訊(groupId、version)、Java與套件版本。
  • <dependencies>:定義專案套件清單
  • <dependency>:定義各專案套件
  • <build>
    • 作用:定義專案的建置規則,也就是「如何將原始碼打包成最終產品」。
    • <plugins>: 用來設定建置過程中會使用到的 Maven 外掛。
    • spring-boot-maven-plugin:作用應為 repackage,將服務打包成 Fat JAR

Auth Service 套件依賴說明

除了使用到 Spring Security 外,也先引入了其他可能會使用到的套件,簡單說明這些套件內容:

  • spring-boot-starter-web
    建構 Web 應用程式與 RESTful API 的起手式。整合了 Spring MVC 框架、內嵌的 Tomcat 伺服器以及 Jackson 的 JSON 處理函式庫,幫助我們快速處理 HTTP 請求。
  • spring-boot-starter-data-jpa
    存取關聯式資料庫的「JPA 全家餐」,讓我們能用物件導向的方式操作資料庫。以 Hibernate 作為 JPA 的核心實作,並透過 Spring Data JPA 提供更便利的 Repository 介面,同時也內建了資料庫連線池。
  • spring-boot-starter-security
    為程式提供身份驗證 (Authentication) 與授權 (Authorization) 的核心套件。包含了核心安全邏輯、Web 環境下的防護機制 (如 Filter Chain),並允許我們以 Java Class 的方式進行客製化設定。
  • postgresql
    PostgreSQL 的 JDBC 驅動程式 讓 Java 知道「如何與 PostgreSQL 資料庫溝通」。
  • spring-boot-starter-test
    撰寫測試案例的必備工具包。整合了 JUnit 5 測試框架、Spring Boot Test 的整合測試支援、AssertJ 的流暢斷言語法以及 Mockito 的 Mock 功能。
  • spring-boot-starter-validation
    提供標準化的資料驗證功能。引入了 Jakarta Bean Validation API (如@NotBlank, @Email 等註解) 以及其參考實作 Hibernate Validator (真正執行驗證規則的引擎)。

本來預計今天想進入實作註冊功能的環節,但因為篇幅的考量還是將專案與服務的基礎設定先在今天做說明,因為覺得理解設定檔如何協調專案與模組分工也是很重要的一環。
明天希望可以進入實作註冊功能的環節,預計從建立檔案架構開始說明~


上一篇
Day3:Auth Service- Spring Security 介紹
下一篇
Day5:Auth Service - 實作註冊功能(資料層)
系列文
吃出一個SideProject!8
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言