前面介紹了Spring Boot 使用JWT實現Token驗證,其實Spring Boot 有完整的安全認證框架:Spring Security。接下來我們介紹如何集成Security 實現安全驗證。
一、Security簡介
安全對於企業來說至關重要,必要的安全認證為企業阻擋了外部非正常的訪問,保證了企業內部數據的安全。
當前,數據安全問題越來越受到行業內公司的重視。數據泄漏很大一部分原因是非正常許可權訪問導致的,於是使用合適的安全框架保護企業服務的安全變得非常緊迫。在Java領域,Spring Security無疑是最佳選擇之一。
Spring Security 是 Spring 家族中的一個安全管理框架,能夠基於 Spring 的企業應用系統提供聲明式的安全訪問控制解決方案。它提供了一組可以在Spring應用系統中靈活配置的組件,充分利用了 Spring的IoC、DI和AOP等特性,為應用系統提供聲明式的安全訪問控制功能,減少了為企業系統安全控制編寫大量重複代碼的工作。
二、Spring Boot對Security的支持
雖然,在Spring Boot出現之前,Spring Security已經發展多年,但是使用並不廣泛。安全管理這個領域一直是Shiro的天下,因為相對於Shiro,在項目中集成Spring Security還是一件麻煩的事情,所以Spring Security雖然比Shiro強大,但是卻沒有Shiro受歡迎。
隨著Spring Boot的出現,Spring Boot 對Spring Security 提供了自動化配置方案,可以零配置使用 Spring Security。這使得Spring Security重新煥發新的活力。
Spring Boot 提供了集成 Spring Security 的組件包
spring-boot-starter-security,方便我們在 Spring Boot 項目中使用 Spring Security進行許可權控制。
三、集成Security
在Spring Boot 項目中集成Spring Boot Security 非常簡單,只需在項目中增加Spring Boot Security的依賴即可。下面通過示例演示Spring Boot中基礎Security的登錄驗證。
1. 添加依賴
Spring Boot 提供了集成 Spring Security 的組件包
spring-boot-starter-security,方便我們在 Spring Boot 項目中使用 Spring Security。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>上面除了引入Security組件外,因為我們要做Web系統的許可權驗證,所以還添加了Web和Thymeleaf組件。
2. 配置登錄用戶名和密碼
用戶名和密碼在application.properties中進行配置。
# security
spring.security.user.name=admin
spring.security.user.password=admin在application.properties配置文件中增加了管理員的用戶名和密碼。
3. 添加Controller
創建SecurityController 類,在類中添加訪問頁面的入口。
@Controller
public class SecurityController {
@RequestMapping("/")
public String index() {
return "index";
}
}4. 創建前端頁面
在resources/templates 目錄下創建頁面 index.html,這個頁面就是具體的需要增加許可權控制的頁面,只有登錄了才能進入此頁。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<body>
<h1>Hello</h1>
<p>我是登錄後才可以看的頁面</p>
</body>
</html>5. 測試驗證
配置完成後,重啟項目,訪問地址:http://localhost:8080/,頁面會自動彈出一個登錄框,如下圖所示。

系統自動跳轉到Spring Security默認的登錄頁面,輸入之前配置的用戶名和密碼就可以登錄系統,登錄後的頁面如下圖所示。

通過上面的示例,我們看到Spring Security自動給所有訪問請求做了登錄保護,實現了頁面許可權控制。
四、登錄驗證
前面演示了在Spring Boot項目中集成Spring Security 實現簡單的登錄驗證功能,在實際項目使用過程中,可能有的功能頁面不需要進行登錄驗證,而有的功能頁面只有進行登錄驗證才能訪問。下面通過完整的示常式序演示如何實現Security的登錄認證。
1. 創建頁面content.html
先創建頁面content.html,此頁面只有登錄用戶才可查看,否則會跳轉到登錄頁面,登錄成功後才能訪問。示例代碼如下:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<body>
<h1>content</h1>
<p>我是登錄後才可以看的頁面</p>
<form method="post" action="/logout">
<button type="submit">退出</button>
</form>
</body>
</html>在上面的示例中,我們看到退出使用post請求,因為Security退出請求默認只支持post 。
2. 修改index.html 頁面
修改之前的index.html頁面,增加登錄按鈕。
<p>點擊 <a th:href="@{/content}">這裡</a>進入管理頁面</p>在上面的示例中,index頁面屬於公共頁面,無許可權驗證,從index頁面進入content頁面時需要登錄驗證。
3. 修改Controller控制器
修改之前的SecurityController控制器,增加content頁面路由地址,示例代碼如下:
@RequestMapping("/")
public String index() {
return "index";
}
@RequestMapping("/content")
public String content() {
return "content";
}4. 創建 SecurityConfig 類
創建 Security的配置文件SecurityConfig類,它繼承於
WebSecurityConfigurerAdapter,現自定義許可權驗證配置。示例代碼如下:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.permitAll()
.and()
.logout()
.permitAll()
.and()
.csrf()
.ignoringAntMatchers("/logout");
}
}在上面的示常式序中,SecurityConfig類中配置 index.html 可以直接訪問,但 content.html 需要登錄後才可以查看,沒有登錄的自動跳轉到登錄頁面。
- @EnableWebSecurity:開啟 Spring Security 許可權控制和認證功能。
- antMatchers(“/”, “/home”).permitAll():配置不用登錄可以訪問的請求。
- anyRequest().authenticated():表示其他的請求都必須有許可權認證。
- formLogin():定製登錄信息。
- loginPage(“/login”):自定義登錄地址,若注釋掉,則使用默認登錄頁面。
- logout():退出功能,Spring Security自動監控了/logout。
- ignoringAntMatchers(“/logout”):Spring Security 默認啟用了同源請求控制,在這裡選擇忽略退出請求的同源限制。
5. 測試驗證
修改完成之後重啟項目,訪問地址http://localhost:8080/可以看到 index 頁面的內容,單擊鏈接跳轉到content頁面時會自動跳轉到登錄頁面,登錄成功後才會自動跳轉到
http://localhost:8080/content,在 content 頁面單擊「退出」按鈕,會退出登錄狀態,跳轉到登錄頁面並提示已經退出。

登錄、退出、請求受限頁面退出後跳轉到登錄頁面是常用的安全控制案例,也是賬戶系統基本的安全保障。
原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/226051.html
微信掃一掃
支付寶掃一掃