SpringSecurity的集成以及相關(guān)組件的介紹
Spring Boot Security支持
在企業(yè)應(yīng)用系統(tǒng)安全方面,比較常用的安全框架有 SpringSecurity 和 Apache Shiro,相對于Shiro, Spring Security功能強(qiáng)大、擴(kuò)展性強(qiáng),同時學(xué)習(xí)難度也稍大一些。Spring Security是專門針對 Spring 項目的安全框架,關(guān)于 Spring Security 的應(yīng)用,我們可以通過專業(yè)書籍來學(xué)習(xí),在本章我們重點放在Spring Boot對Spring Security的集成以及相關(guān)組件的介紹。
在具體項目實踐中,認(rèn)證和權(quán)限是必不可少的,特別是 Web 項目注冊、登錄、訪問權(quán)限等場景。Spring Security 致力于為 Java 應(yīng)用提供認(rèn)證和授權(quán)管理,同時,它也是 Spring Boot官方推薦的權(quán)限管理框架。
Spring Security 基于 Spring,是一個能夠為企業(yè)應(yīng)用系統(tǒng)提供聲明式安全訪問控制解決方案的安全框架。Spring Security充分利用了 Spring IoC (控制反轉(zhuǎn) Inversion of Control)、DI ( 依賴注入 Dependency Injection)和 AOP(面向切面編程)等特性來實現(xiàn)相關(guān)的功能,并提供了--組可以在 Spring 應(yīng)用上下文中配置的 Bean。

Security 自動配置
在 Spring Boot 中,使用 Spring Security 只 需簡單地引入
spring-boot-starter-security 即可。
在 Spring Boot 中 對 oauth2 、 reactive 和 servlet 進(jìn) 行 了 支 持 相 關(guān) 類 均 位 于spring-boot-autoconfigure 項目下的 org.springframework.
boot.autoconfigure.security 包中。
Spring Boot 項目引入
spring-boot-starter-security 之后,再次訪問之前的 URL,會發(fā)現(xiàn)頁面跳轉(zhuǎn)到--個 login 的登錄界面。這便是 Security 在 SpringBoot 中的默認(rèn)配置生效了。此時,可以在 application 配 置文件中指定以下內(nèi)容,重啟項目輸入用戶名和密碼登錄即可正常訪問。
spring . security. user . name=user
spring. security . user. password=password
spring. security. user . roles=USER下面,我們就逐步分析 Spring Boot 中這一自動配置機(jī)制是如何實現(xiàn)的。

同其他自動配置一樣,在 spring-boot-autoconfigure 項目的 META-INF/spring.factories 中注冊了 Security 相關(guān)的自動配置類。
# Auto Configure
org. springframework . boot . autoconfigure . EnableAutoConfiguration=\
org. springframework. boot . autoconfigure. security . servlet . SecurityAutoConfigu
ration,\
org. springframework . boot . autoconfigure . security. servlet .UserDetailsserviceA
uto-
Configuration, \org. springframework . boot . autoconfigure . security . servlet . SecurityFilterAutoC
onfi-
guration, \
org. springframework . boot . autoconfigure . security . reactive. ReactiveSecurityAu
to-
Configuration, \
org. springframework . boot . autoconfigure . security . reactive. ReactiveUserDetailS-
ServiceAutoConfiguration, \
org. springframework . boot . autoconfigure . security . oauth2. client . servlet . 0Auth
2Client-
AutoConfiguration,\
org. springframework. boot . autoconfigure . security . oauth2. client . reactive . Reac
tive0-
Auth2ClientAutoConfiguration, \
org. springframework . boot . autoconfigure . security . oauth2. resource . servlet .0Au
th2-
ResourceServerAutoConfiguration, \
org. springframework. boot . autoconfigure . security . oauth2. resource . reactive. Re
active-
OAuth2ResourceServerAutoConfiguration, \通過 spring.factories 中的注冊可以看出,針對 Security 有大量的自動配置類,涉及 servlet、reactive 、 oauth2 等 自 動 配 置 。我 們 這 里 重 點 介 紹 SecurityAutoConfiguration 和
Se-curityFilterAutoConfiguration。

本文給大家講解的內(nèi)容是SpringBootSecurity支持:Security自動配置
下篇文章給大家講解的是SpringBootSecurity支持:SecurityAutoConfiguration 詳解;
覺得文章不錯的朋友可以轉(zhuǎn)發(fā)此文關(guān)注小編;
感謝大家的支持!
本文就是愿天堂沒有BUG給大家分享的內(nèi)容,大家有收獲的話可以分享下,想學(xué)習(xí)更多的話可以到微信公眾號里找我,我等你哦。
