Spring Security整合Oauth2實現(xiàn)流程詳解
一、創(chuàng)建項目并導入依賴
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.security.oauth</groupId><artifactId>spring-security-oauth2</artifactId><version>2.3.6.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
注:這里的oauth2不是springboot那個,這是springsecurity。
Oauth2一共有四種認證模式是
本篇是password的認證模式,用于前后端分離登陸
第三方登陸一般是授權碼模式
二、相關配置和代碼
注:授權服務器和資源服務器一般是分開來的,我這里就不分開了
2.1)application.properties
spring.redis.host=192.168.21.135
spring.redis.port=6379
spring.redis.database=0
spring.redis.password=520hufei520
2.2)創(chuàng)建授權服務
2.2.1)實現(xiàn)AuthorizationServiceConfigurerAdapter

@Configuration表示這個一個配置類
@EnableAuthorizationServer表示開啟授權服務
2.2.2)注入AuthenticationManager、RedisConnectionFactory、UserDetailsService

AuthenticationManager表示支持password認證模式
RedisConnectionFactory登陸成功后的token需要存在redis里面,因為redis里面有過期機制
UserDetailsService里面存放著用戶信息
2.2.3)重寫方法


2.3)創(chuàng)建資源服務
2.3.1)實現(xiàn)ResourceServerConfigurerAdapter

@configuration表示這是一個配置類
@enbaleResourceServer表示開啟資源服務
2.3.2)重寫方法

2.4)創(chuàng)建Security配置類
2.4.1)實現(xiàn)WebSecurityConfigurerAdapter

2.4.2)將授權服務需要的兩個bean,提供給它


@Bean表示告訴方法,產(chǎn)生一個Bean對象,然后這個Bean對象交給Spring管理。產(chǎn)生這個Bean對象的方法Spring只會調(diào)用一次,隨后這個Spring將會將這個Bean對象放在自己的IOC容器中。
@Bean和@Component作用一樣都是將bean注冊到spring容器中去
2.4.3)重寫方法

2.5)創(chuàng)建Controller

三、測試&效果
3.1)獲取訪問資源服務的token

3.2)訪問資源服務

3.3)刷新token

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關文章:
1. php使用正則驗證密碼字段的復雜強度原理詳細講解 原創(chuàng)2. ASP中實現(xiàn)字符部位類似.NET里String對象的PadLeft和PadRight函數(shù)3. 基于jsp+mysql實現(xiàn)在線水果銷售商城系統(tǒng)4. 基于javaweb+jsp實現(xiàn)企業(yè)財務記賬管理系統(tǒng)5. JSP+Servlet實現(xiàn)文件上傳到服務器功能6. vue前端RSA加密java后端解密的方法實現(xiàn)7. 利用CSS3新特性創(chuàng)建透明邊框三角8. 淺談由position屬性引申的css進階討論9. ASP動態(tài)網(wǎng)頁制作技術經(jīng)驗分享10. CSS可以做的幾個令你嘆為觀止的實例分享

網(wǎng)公網(wǎng)安備