J.J. Huang   2019-04-28   Spring Boot   瀏覽次數:次  

SpringBoot - 第三十九章 | Spring Security 安全控制

📑 目錄
  1. 環境準備
    1. 建立 DemoController
    2. 建立 index.html、hello.html
    3. 測試
  2. 整合Spring Security
    1. 加入pom的依賴
    2. 修改 DemoController
    3. 建立 WebSecurityConfig
    4. 建立 login.html
  3. 測試
    1. 修改 hello.html
    2. 測試

我們在編寫Web應用時,經常需要對頁面做一些安全控制,比如:對於沒有訪問權限的用戶需要轉到登錄表單頁面。要實現訪問控制的方法多種多樣,可以通過Aop、攔截器實現,也可以通過框架實現(如:Apache Shiro、Spring Security)。

以下會使用到Thymeleaf模板引擎,可以先行閱讀SpringBoot - 第四章 | Web開發

環境準備

建立 DemoController

  • /:映射到index.html
  • /hello:映射到hello.html

建立 index.html、hello.html

  • Path : /chapter-39/src/main/resources/templates

測試

啟動應用,http://localhost:8080/ ,並點擊 「這裡」 打個招呼!​‌‌‌​​‌‌​‌‌‌​​​​​‌‌‌​​‌​​‌‌​‌​​‌​‌‌​‌‌‌​​‌‌​​‌‌‌​‌‌​​​‌​​‌‌​‌‌‌‌​‌‌​‌‌‌‌​‌‌‌​‌​​​​‌​‌‌​‌​​‌‌​​‌​​​‌‌​​​​​​‌‌​​​‌​​‌‌‌​​‌​​‌‌​​​​​​‌‌​‌​​​​‌‌​​‌​​​‌‌‌​​​​​‌​‌‌​‌​‌‌‌​​‌‌​‌‌‌​​​​​‌‌‌​​‌​​‌‌​‌​​‌​‌‌​‌‌‌​​‌‌​​‌‌‌​‌‌​​​‌​​‌‌​‌‌‌‌​‌‌​‌‌‌‌​‌‌‌​‌​​​​‌‌​​‌‌​​‌‌‌​​‌

SpringBoot - 第三十九章 - 圖 1 (01)

SpringBoot - 第三十九章 - 圖 2 (02)

在index.html中提供到/hello的連結,顯然在這裡沒有任何安全控制,所以點擊連結後就可以直接跳轉到hello.html頁面。​‌‌‌​​‌‌​‌‌‌​​​​​‌‌‌​​‌​​‌‌​‌​​‌​‌‌​‌‌‌​​‌‌​​‌‌‌​‌‌​​​‌​​‌‌​‌‌‌‌​‌‌​‌‌‌‌​‌‌‌​‌​​​​‌​‌‌​‌​​‌‌​​‌​​​‌‌​​​​​​‌‌​​​‌​​‌‌‌​​‌​​‌‌​​​​​​‌‌​‌​​​​‌‌​​‌​​​‌‌‌​​​​​‌​‌‌​‌​‌‌‌​​‌‌​‌‌‌​​​​​‌‌‌​​‌​​‌‌​‌​​‌​‌‌​‌‌‌​​‌‌​​‌‌‌​‌‌​​​‌​​‌‌​‌‌‌‌​‌‌​‌‌‌‌​‌‌‌​‌​​​​‌‌​​‌‌​​‌‌‌​​‌


整合Spring Security

針對/hello頁面進行權限控制,必須是授權用戶才能訪問。當沒有權限的用戶訪問後,跳轉到登錄頁面。

加入pom的依賴

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

修改 DemoController

/login請求映射至login.html

建立 WebSecurityConfig

建立Spring Security的配置類​‌‌‌​​‌‌​‌‌‌​​​​​‌‌‌​​‌​​‌‌​‌​​‌​‌‌​‌‌‌​​‌‌​​‌‌‌​‌‌​​​‌​​‌‌​‌‌‌‌​‌‌​‌‌‌‌​‌‌‌​‌​​​​‌​‌‌​‌​​‌‌​​‌​​​‌‌​​​​​​‌‌​​​‌​​‌‌‌​​‌​​‌‌​​​​​​‌‌​‌​​​​‌‌​​‌​​​‌‌‌​​​​​‌​‌‌​‌​‌‌‌​​‌‌​‌‌‌​​​​​‌‌‌​​‌​​‌‌​‌​​‌​‌‌​‌‌‌​​‌‌​​‌‌‌​‌‌​​​‌​​‌‌​‌‌‌‌​‌‌​‌‌‌‌​‌‌‌​‌​​​​‌‌​​‌‌​​‌‌‌​​‌

建立 login.html

  • Path : /chapter-39/src/main/resources/templates

測試

  • 訪問 http://localhost:8080/ ,可以正常訪問。
    SpringBoot - 第三十九章 - 圖 3 (03)

  • 點擊「這裡」http://localhost:8080/hello 的時候被重定向到了 http://localhost:8080/login
    SpringBoot - 第三十九章 - 圖 4 (04)

  • 輸入user/password登入,呈現hello頁面
    SpringBoot - 第三十九章 - 圖 5 (05)​‌‌‌​​‌‌​‌‌‌​​​​​‌‌‌​​‌​​‌‌​‌​​‌​‌‌​‌‌‌​​‌‌​​‌‌‌​‌‌​​​‌​​‌‌​‌‌‌‌​‌‌​‌‌‌‌​‌‌‌​‌​​​​‌​‌‌​‌​​‌‌​​‌​​​‌‌​​​​​​‌‌​​​‌​​‌‌‌​​‌​​‌‌​​​​​​‌‌​‌​​​​‌‌​​‌​​​‌‌‌​​​​​‌​‌‌​‌​‌‌‌​​‌‌​‌‌‌​​​​​‌‌‌​​‌​​‌‌​‌​​‌​‌‌​‌‌‌​​‌‌​​‌‌‌​‌‌​​​‌​​‌‌​‌‌‌‌​‌‌​‌‌‌‌​‌‌‌​‌​​​​‌‌​​‌‌​​‌‌‌​​‌

  • 訪問 http://localhost:8080/login?logout ,完成登出操作。
    SpringBoot - 第三十九章 - 圖 6 (06)

  • 輸入錯誤帳號/密碼的頁面 http://localhost:8080/login?error
    SpringBoot - 第三十九章 - 圖 7 (07)

根據配置,Spring Security提供了一個過濾器來攔截請求並驗證用戶身份。如果用戶身份認證失敗,頁面就重定向到/login?error,並且頁面中會展現相應的錯誤訊息。若用戶想要登出,可以通過訪問/login?logout請求,在完成登出之後,頁面展現相應的成功消息。​‌‌‌​​‌‌​‌‌‌​​​​​‌‌‌​​‌​​‌‌​‌​​‌​‌‌​‌‌‌​​‌‌​​‌‌‌​‌‌​​​‌​​‌‌​‌‌‌‌​‌‌​‌‌‌‌​‌‌‌​‌​​​​‌​‌‌​‌​​‌‌​​‌​​​‌‌​​​​​​‌‌​​​‌​​‌‌‌​​‌​​‌‌​​​​​​‌‌​‌​​​​‌‌​​‌​​​‌‌‌​​​​​‌​‌‌​‌​‌‌‌​​‌‌​‌‌‌​​​​​‌‌‌​​‌​​‌‌​‌​​‌​‌‌​‌‌‌​​‌‌​​‌‌‌​‌‌​​​‌​​‌‌​‌‌‌‌​‌‌​‌‌‌‌​‌‌‌​‌​​​​‌‌​​‌‌​​‌‌‌​​‌

為了讓整個過程更完成,我們可以修改hello.html,讓它輸出一些內容,並提供“登出”的連結。

修改 hello.html

測試

SpringBoot - 第三十九章 - 圖 8 (08)

SpringBoot - 第三十九章 - 圖 9 (09)​‌‌‌​​‌‌​‌‌‌​​​​​‌‌‌​​‌​​‌‌​‌​​‌​‌‌​‌‌‌​​‌‌​​‌‌‌​‌‌​​​‌​​‌‌​‌‌‌‌​‌‌​‌‌‌‌​‌‌‌​‌​​​​‌​‌‌​‌​​‌‌​​‌​​​‌‌​​​​​​‌‌​​​‌​​‌‌‌​​‌​​‌‌​​​​​​‌‌​‌​​​​‌‌​​‌​​​‌‌‌​​​​​‌​‌‌​‌​‌‌‌​​‌‌​‌‌‌​​​​​‌‌‌​​‌​​‌‌​‌​​‌​‌‌​‌‌‌​​‌‌​​‌‌‌​‌‌​​​‌​​‌‌​‌‌‌‌​‌‌​‌‌‌‌​‌‌‌​‌​​​​‌‌​​‌‌​​‌‌‌​​‌

這邊僅僅做了一些簡單的使用,當然Spring Security的功能不僅僅只有這些,更多的功能可以參考Spring Security Reference

註:以上參考了
Spring Security Reference
Spring Security 无法登陆,报错:There is no PasswordEncoder mapped for the id “null”
程序猿DDSpring Boot中使用Spring Security进行安全控制 文章。