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

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

同其他自動(dòng)配置一樣,在 spring-boot-autoconfigure 項(xiàng)目的 META-INF/spring.factories 中注冊(cè)了 Security 相關(guān)的自動(dòng)配置類。
# 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, \通過(guò) spring.factories 中的注冊(cè)可以看出,針對(duì) Security 有大量的自動(dòng)配置類,涉及 servlet、reactive 、 oauth2 等 自 動(dòng) 配 置 。我 們 這 里 重 點(diǎn) 介 紹 SecurityAutoConfiguration 和
Se-curityFilterAutoConfiguration。

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