Interface SecurityContributor
- All Superinterfaces:
org.springframework.core.Ordered
public interface SecurityContributor
extends org.springframework.core.Ordered
Feature 自有授權規則的貢獻點
消費端的 security 組態組裝唯一的 SecurityFilterChain,
但不點名各 feature 擁有的路徑。每個 feature 自己宣告它的 matcher,安裝與否隨 feature
進出——移除該 feature 時,它的授權規則跟著消失,不會在 security 組態裡留下懸空的路徑字串。
必要能力因此對選用 feature 沒有任何 import 邊(依賴反轉:貢獻點是框架契約,實作住各 feature)。
為何不是「每個 feature 一條 SecurityFilterChain」
共用路徑前綴(如認證面的 /api/v1/auth)常被多個 feature 的 controller 共用,無法以
securityMatcher 切開。且每條獨立 chain 都得自備 cors / csrf / oauth2ResourceServer /
exceptionHandling,漏一項就是安全洞。故採單一 chain + 貢獻點。
撰寫規則
- matcher 必須與其他 feature 互斥。
authorizeHttpRequests是 first-match-wins,重疊時先註冊者勝。 - 需要收緊另一 feature 已放行的子路徑時,以
getOrder()排在它之前;否則沿用預設。 - 不要在此註冊
anyRequest()——那由消費端的 security 組態最後統一收尾為authenticated()。 - 每個 matcher 都要有明確的 endpoint owner 與 classification;最後的
authenticated()只作 防漏底線,不代表落入其中的 endpoint 已完成安全設計。 - 非敏感資訊與低風險操作優先明確宣告 authenticated policy,並由消費端接上 Audit;只有 敏感資訊或敏感/高影響操作才要求 Authority,避免擴張 capability catalog。
- Public matcher 必須明確
permitAll,且消費端查詢仍須限制 published/expiry/revoked/visibility; feature matcher 不取代 service/policy 的 object/data scope。
消費方式
消費端的 security 組態注入 List<SecurityContributor>(Spring 依 Ordered 排序),
於 authorizeHttpRequests 內先套用全部貢獻者、再註冊自己的基礎規則、最後
anyRequest().authenticated() 收尾。參考接線見參考實作 app-server 的 SecurityConfig。
-
Field Summary
Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE -
Method Summary
Modifier and TypeMethodDescriptionvoidauthorize(org.springframework.security.config.annotation.web.configurers.AuthorizeHttpRequestsConfigurer<org.springframework.security.config.annotation.web.builders.HttpSecurity>.org.springframework.security.config.annotation.web.configurers.AuthorizeHttpRequestsConfigurer.AuthorizationManagerRequestMatcherRegistry registry) 向唯一的 filter chain 註冊本 feature 擁有的路徑授權規則。default intgetOrder()註冊順序;數字小者先註冊、先匹配。matcher 互斥時無影響。
-
Method Details
-
authorize
void authorize(org.springframework.security.config.annotation.web.configurers.AuthorizeHttpRequestsConfigurer<org.springframework.security.config.annotation.web.builders.HttpSecurity>.org.springframework.security.config.annotation.web.configurers.AuthorizeHttpRequestsConfigurer.AuthorizationManagerRequestMatcherRegistry registry) 向唯一的 filter chain 註冊本 feature 擁有的路徑授權規則。- Parameters:
registry-authorizeHttpRequests的 matcher registry,於anyRequest()之前呼叫
-
getOrder
default int getOrder()註冊順序;數字小者先註冊、先匹配。matcher 互斥時無影響。- Specified by:
getOrderin interfaceorg.springframework.core.Ordered
-