使用Redis
來存儲OAuth2
相關的客戶端信息以及生成的AccessToken
是一個不錯的選擇,Redis
與生俱來的的高效率、集羣部署是比較出色的功能,若是用來做爲服務認證中心
的數據存儲,能夠大大的提升響應效率。html
Redis
還支持超時自動刪除功能,OAuth2
所生成的AccessToken
相關的數據在超過配置的有效時間
後就會自動被清除,這樣也隱形的提升了接口的安全性。java
既然Redis
能夠作到這麼好,咱們該怎麼實現代碼邏輯呢?git
ApiBoot OAuth2
是支持使用Redis
來存儲AccessToken
的,只須要修改application.yml
一個配置就能夠實現,相關的使用也能夠經過查看文檔瞭解。web
咱們使用IDEA
開發工具建立一個SpringBoot
項目,在項目的pom.xml
添加咱們須要的ApiBoot
的統一版本
依賴以及安全組件
依賴,以下所示:redis
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.minbox.framework</groupId> <artifactId>api-boot-starter-security-oauth-jwt</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.minbox.framework</groupId> <artifactId>api-boot-dependencies</artifactId> <version>2.2.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
既然咱們本章須要用到Redis
,咱們須要在項目內添加相關的依賴,SpringBoot
已經爲咱們提供了封裝好的依賴,在pom.xml
文件內dependencies
節點下添加,以下所示:spring
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
SpringBoot
對Redis
的鏈接、數據操做都作了封裝,咱們只須要在application.yml
配置文件內添加響應的Redis
鏈接信息便可。json
spring-boot-starter-data-redis
依賴所須要的配置都是由RedisProperties
類提供,該類內有部分配置字段存在默認值,部分源碼以下所示:segmentfault
@ConfigurationProperties(prefix = "spring.redis") public class RedisProperties { /** * Database index used by the connection factory. */ private int database = 0; /** * Connection URL. Overrides host, port, and password. User is ignored. Example: * redis://user:password@example.com:6379 */ private String url; /** * Redis server host. */ private String host = "localhost"; /** * Login password of the redis server. */ private String password; /** * Redis server port. */ private int port = 6379; //... }
默認配置下鏈接Redis
只須要在application.yml
配置spring.redis.password
,以下所示:api
spring: # 配置Redis鏈接信息 redis: password: 123123
password
是鏈接Redis
所須要的密碼,在redis.conf
文件內配置。
相關配置解釋:安全
spring.redis.database
:若是你使用的Redis DataBase
並非默認的0
索引,須要修改該配置spring.redis.host
:默認爲localhost
,若是不是本地使用,須要修改該配置spring.redis.url
:這是一個鏈接字符串,如天配置了會自動覆蓋database
、host
、port
等三個配置信息spring.redis.port
:默認爲Redis
的端口號6379
,如已修改Redis
的監聽端口號,須要修改該配置ApiBoot OAuth
提供了redis
配置選項,在application.yml
文件內經過api.boot.oauth.away
配置參數指定,以下所示:
api: boot: security: # 配置內存安全用戶列表 users: - username: yuqiyu password: 123123 oauth: # 配置使用Redis存儲OAuth2相關數據 away: redis # 配置客戶端列表 clients: - clientId: minbox clientSecret: chapter
爲了方便演示,咱們使用ApiBoot Security
的內存方式配置了一個用戶yuqiyu
,並且還修改了默認client
信息,新加了minbox
客戶端。
若是對ApiBoot Security
用戶配置或者ApiBoot OAuth
的客戶端配置不瞭解,能夠查看官方文檔:
在運行測試以前咱們添加一個名爲ApiController
的控制器用來測試,代碼以下所示:
/** * 測試Api控制器 * * @author 恆宇少年 */ @RestController @RequestMapping(value = "/api") public class ApiController { /** * 測試請求,需攜帶令牌訪問 * * @return */ @GetMapping(value = "/index") public String index() { return "this is index"; } }
預計效果是當咱們發送獲取AccessToken
的請求時,會自動將生成的AccessToken
存儲到Redis
。
下面咱們使用CURL
命令來嘗試獲取下AccessToken
,以下所示:
➜ ~ curl minbox:chapter@localhost:9090/oauth/token -d 'grant_type=password&username=yuqiyu&password=123123' {"access_token":"38a7ee20-2fad-43c5-a349-31e6f0ee0f29","token_type":"bearer","refresh_token":"f469b1e8-f63c-4be9-8564-2603f8458024","expires_in":7199,"scope":"api"}
下面咱們使用redis-cli
來看下是否已經將AccessToken
存儲到Redis
,以下所示:
➜ ~ redis-cli 127.0.0.1:6379> auth 123123 OK 127.0.0.1:6379> keys * 1) "uname_to_access:minbox:yuqiyu" 2) "refresh_to_access:f469b1e8-f63c-4be9-8564-2603f8458024" 3) "access_to_refresh:1ea8e5cd-ea63-4a73-969f-9e7767f25f30" 4) "auth:38a7ee20-2fad-43c5-a349-31e6f0ee0f29" 5) "refresh_auth:6898bef4-f4a7-4fa9-858b-a4c62a1567d8" 6) "refresh:6898bef4-f4a7-4fa9-858b-a4c62a1567d8" 7) "refresh_auth:f469b1e8-f63c-4be9-8564-2603f8458024" 8) "access:38a7ee20-2fad-43c5-a349-31e6f0ee0f29" 9) "refresh_to_access:6898bef4-f4a7-4fa9-858b-a4c62a1567d8" 10) "auth_to_access:f02ceb5faa4577222082842b82a57067" 11) "refresh:f469b1e8-f63c-4be9-8564-2603f8458024" 12) "access_to_refresh:38a7ee20-2fad-43c5-a349-31e6f0ee0f29" 13) "client_id_to_access:minbox"
結果每每讓人感受驚喜,看到這裏咱們已經成功的把OAuth2
生成的AccessToken
存儲到了Redis
,若是AccessToken
對應的數據超過了expires_in
時間,就會自動被清除。
咱們能夠拿着生成的AccessToken
來訪問在上面添加的測試ApiController
內的接口,以下所示:
➜ ~ curl -H 'Authorization: Bearer 38a7ee20-2fad-43c5-a349-31e6f0ee0f29' http://localhost:9090/api/index this is index
咱們能夠拿到接口的返回的接口,這也證實了一點,AccessToken
的驗證是沒有問題的,OAuth2
拿着請求攜帶的AccessToken
去Redis
驗證經過。
ApiBoot OAuth
所支持的3種
存儲方式都已經經過文章的方式告知你們,每一種方式都作到了精簡,簡單的配置,添加相關的依賴,就可以實如今以前讓不少人頭疼的集成。
若是在生產環境中數據量較大,建議使用Redis
集羣來解決存儲AccessToken
的問題。
若是你對ApiBoot OAuth其它兩種存儲方式不瞭解,能夠查看我編寫的ApiBoot
系列的文章:ApiBoot開源框架各個組件的系列使用文章彙總
若是您喜歡本篇文章請爲源碼倉庫點個Star
,謝謝!!!
本篇文章示例源碼能夠經過如下途徑獲取,目錄爲apiboot-oauth-use-redis-storage
:
做者我的 博客
使用開源框架 ApiBoot 助你成爲Api接口服務架構師