昨天介紹了 Spring Security 框架與驗證、授權的幾個主要功能,今天開始預計要整合這些知識來完成實作的內容!
但在實作前還是要花點篇幅來介紹專案設定與依賴管理,幫助自己了解整個專案的運作機制,也大概知道一下自己未來實作時的功能出自於那些套件。
至於環境建置的過程、編輯器的介紹等內容相信在網路上都有許多前輩已經分享非常完善充足的知識了,本次參賽就跳過該環節,著眼於專案本身的介紹。
本專案為多模組 (Multi-module) 的結構,接下來要實作的 Auth Service 為專案下的其中一個服務。
實作時會給予專案層跟每個服務層各自的設定檔(pom.xml
),其作用如下:
pom.xml
:負責定義所有子模組都應遵守的共同規範,例如共用的套件版本、Java 版本等。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
本服務設定檔內容與說明如下:
...
<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>
auth-service
會繼承專案的設定,包含專案資訊(groupId、version)、Java與套件版本。<dependencies>
:定義專案套件清單<dependency>
:定義各專案套件<build>
<plugins>
: 用來設定建置過程中會使用到的 Maven 外掛。spring-boot-maven-plugin:
作用應為 repackage,將服務打包成 Fat JAR
除了使用到 Spring Security 外,也先引入了其他可能會使用到的套件,簡單說明這些套件內容:
spring-boot-starter-web
spring-boot-starter-data-jpa
spring-boot-starter-security
postgresql
spring-boot-starter-test
spring-boot-starter-validation
@NotBlank
, @Email
等註解) 以及其參考實作 Hibernate Validator (真正執行驗證規則的引擎)。本來預計今天想進入實作註冊功能的環節,但因為篇幅的考量還是將專案與服務的基礎設定先在今天做說明,因為覺得理解設定檔如何協調專案與模組分工也是很重要的一環。
明天希望可以進入實作註冊功能的環節,預計從建立檔案架構開始說明~