spring security原理-學習筆記1-整體概覽
- 2019 年 10 月 26 日
- 筆記
整體概述
運行時環境
Spring Security 3.0需要Java 5.0 Runtime Environment或更高版本。
核心組件
SecurityContextHolder,SecurityContext和Authentication Objects
最基本的對象是SecurityContextHolder。這是我們存儲應用程式當前安全上下文的詳細資訊的地方,其中包括當前使用該應用程式的主體的詳細資訊。默認情況下,SecurityContextHolder使用ThreadLocal來存儲這些詳細資訊,這意味著安全上下文始終可用於同一執行執行緒中的方法,即使安全上下文未作為參數顯式傳遞那些方法。如果在處理當前委託人的請求之後小心地清除執行緒,以這種方式使用ThreadLocal是非常安全的。當然,Spring Security會自動為您解決這個問題,因此無需擔心。
獲取有關當前用戶的資訊
在SecurityContextHolder內,我們存儲了當前與應用程式交互的主體的詳細資訊。Spring Security使用Authentication對象來表示此資訊。您通常不需要自己創建Authentication對象,但用戶查詢Authentication對象是相當常見的。您可以使用以下程式碼塊(從應用程式的任何位置)獲取當前經過身份驗證的用戶的名稱
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); if (principal instanceof UserDetails) { String username = ((UserDetails)principal).getUsername(); } else { String username = principal.toString(); } UserDetailsService
用來查詢資料庫,載入用戶資訊的介面
UserDetails loadUserByUsername(String username) throws UsernameNotFoundException;
需要注意
UserDetailsService經常有些混亂。它純粹是用戶數據的DAO,除了將數據提供給框架內的其他組件之外,不執行任何其他功能。特別是,它不會對用戶進行身份驗證,這是由AuthenticationManager完成的。在許多情況下,如果您需要自定義身份驗證過程,直接實現AuthenticationProvider會更有意義。
GrantedAuthority
除了校長之外,Authentication提供的另一個重要方法是getAuthorities()。此方法提供GrantedAuthority個對象的數組。毫不奇怪,GrantedAuthority是授予校長的權力。這些權力通常是「角色」,例如ROLE_ADMINISTRATOR或ROLE_HR_SUPERVISOR。稍後將為web授權,方法授權和域對象授權配置這些角色。Spring Security的其他部分能夠解釋這些權威,並期望它們存在。GrantedAuthority對象通常由UserDetailsService載入。
小結
回顧一下,到目前為止我們看到的Spring Security的主要構建塊是:
SecurityContextHolder,提供SecurityContext的訪問許可權。
SecurityContext,保存Authentication和可能的特定於請求的安全資訊。
Authentication,以特定於Spring Security的方式代表校長。
GrantedAuthority,以反映授予主體的應用程式範圍的許可權。
UserDetails,提供從應用程式的DAO或其他安全數據源構建Authentication對象所需的資訊。
UserDetailsService,在基於String的用戶名(或證書ID等)中傳遞時創建UserDetails。
認證Authentication
在Spring Security內認證的流程。
- 獲取用戶名和密碼並將其合併到UsernamePasswordAuthenticationToken的實例中(我們之前看到的Authentication介面的實例)。
- 令牌被傳遞給AuthenticationManager的實例以進行驗證。
- AuthenticationManager在成功驗證後返回完全填充的Authentication實例。
- 通過調用SecurityContextHolder.getContext().setAuthentication(…)建立安全上下文,傳入返回的身份驗證對象。
例子:
程式碼:
https://github.com/victorsheng/gs-securing-web/blob/master/complete/src/test/java/helloworld/AuthenticationExample.java
結果
Please enter your username: hi Please enter your password: hi1 Authentication failed: Bad Credentials Please enter your username: hi Please enter your password: hi Successfully authenticated. Security context contains: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@441d0230: Principal: hi; Credentials: [PROTECTED]; Authenticated: true; Details: null; Granted Authorities: ROLE_USER web應用中的驗證 Authentication in a Web Application
考慮典型的web應用程式的身份驗證過程:
- 您訪問主頁,然後單擊鏈接。
- 請求轉到伺服器,伺服器確定您已請求受保護的資源。
- 由於您目前尚未通過身份驗證,因此伺服器會發回一個響應,指示您必須進行身份驗證。響應將是HTTP響應程式碼,或重定向到特定的web頁面。
- 根據身份驗證機制,您的瀏覽器將重定向到特定的web頁面,以便您可以填寫表單,或者瀏覽器將以某種方式檢索您的身份(通過BASIC身份驗證對話框,cookie,X. 509證書等)。
- 瀏覽器將向伺服器發迴響應。這將是包含您填寫的表單內容的HTTP POST,或者包含您的身份驗證詳細資訊的HTTP標頭。
- 接下來,伺服器將決定所呈現的憑證是否有效。如果它們有效,則下一步將會發生。如果它們無效,通常會要求您的瀏覽器再次嘗試(因此您將返回上面的第二步)。
- 將重試您進行身份驗證過程的原始請求。希望您已通過足夠授權的許可權進行身份驗證以訪問受保護資源。如果您有足夠的訪問許可權,請求將成功。否則,您將收到HTTP錯誤程式碼403,這意味著「禁止」。
ExceptionTranslationFilter
ExceptionTranslationFilter是一個Spring Security過濾器,負責檢測拋出的任何Spring Security異常。AbstractSecurityInterceptor通常會拋出此類異常,這是授權服務的主要提供者。
AuthenticationEntryPoint
AuthenticationEntryPoint負責上面列表中的第三步。可以想像,每個web應用程式都有一個默認的身份驗證策略(好吧,這可以像Spring Security中的幾乎所有其他配置一樣配置,但現在讓我們保持簡單)。每個主要身份驗證系統都有自己的AuthenticationEntryPoint實現,通常執行步驟3中描述的操作之一。
在請求之間SecurityContext
在Spring Security中,在請求之間存儲SecurityContext的責任落在SecurityContextPersistenceFilter上,默認情況下,該上下文將上下文存儲為HTTP請求之間的HttpSession屬性。
許多其他類型的應用程式(例如,無狀態RESTful web服務)不使用HTTP會話,並將在每個請求上重新進行身份驗證。但是,鏈中包含SecurityContextPersistenceFilter以確保在每次請求後清除SecurityContextHolder仍然很重要。
授權/訪問控制 Access-Control (Authorization) in Spring Security
負責在Spring Security中做出訪問控制決策的主介面是AccessDecisionManager。它有一個decide方法,它接受一個代表請求訪問的主體的Authentication對象,一個「安全對象」(見下文)和一個適用於該對象的安全元數據屬性列表(例如角色列表)這是獲得訪問所必需的。
aop
可以選擇使用AspectJ或Spring AOP執行方法授權,也可以選擇使用過濾器執行web請求授權。您可以將這些方法中的零個,一個,兩個或三個一起使用。主流使用模式是執行一些web請求授權,以及服務層上的一些Spring AOP方法調用授權。
AbstractSecurityInterceptor

AbstractSecurityInterceptor為處理安全對象請求提供了一致的工作流程,通常:
- 查找與當前請求關聯的「配置屬性」
- 將安全對象,當前Authentication和配置屬性提交到AccessDecisionManager以進行授權決策
- (可選)更改發生調用的Authentication
- 允許安全對象調用繼續(假設已授予訪問許可權)
- 調用返回後,調用AfterInvocationManager(如果已配置)。如果調用引發異常,則不會調用AfterInvocationManager。
Configuration Attributes
RunAsManager
AfterInvocationManager
Security interceptors and the "secure object" model

本地化
參考
英文版文檔
https://docs.spring.io/spring-security/site/docs/5.0.5.RELEASE/reference/htmlsingle/#overall-architecture
中文版文檔
https://www.springcloud.cc/spring-security.html
