構建 oauth2 授權服務器
從表現形式上看,oauth2 授權服務器也是一個獨立的微服務,因此構建授權服務器的方法也是創建一個 springboot 應用程序,我們需要引入對應的 maven 依賴,如下所示:
1
2
3
4
|
<dependency> <groupid>org.springframework.security.oauth</groupid> <artifactid>spring-security-oauth2</artifactid> </dependency> |
這里的 spring-security-oauth2 就是來自 spring security 中的 oauth2 庫。現在 maven 依賴已經添加完畢,下一步就是構建 bootstrap 類作為訪問的入口:
1
2
3
4
5
6
7
|
@springbootapplication @enableauthorizationserver public class authserver { public static void main(string[] args) { springapplication.run(authserver. class , args); } } |
請注意,這里出現了一個新的注解 @enableauthorizationserver,這個注解的作用在于為微服務運行環境提供一個基于 oauth2 協議的授權服務,該授權服務會暴露一系列基于 restful 風格的端點(例如 /oauth/authorize 和 /oauth/token)供 oauth2 授權流程使用。
構建 oauth2 授權服務只是集成 oauth2 協議的第一步,授權服務器是一種集中式系統,管理著所有與安全性流程相關的客戶端和用戶信息。因此,接下來我們需要在授權服務器中對這些基礎信息進行初始化,而 spring security 為我們提供了各種配置類來實現這一目標。
設置客戶端和用戶認證信息
oauth2.0 有幾個授權模式:授權碼模式、簡化模式、密碼模式、客戶端憑證模式。其中,密碼模式以其簡單性得到了廣泛的應用。在接下來的內容中,我們就以密碼模式為例展開講解。
在密碼模式下,用戶向客戶端提供用戶名和密碼,并將用戶名和密碼發給授權服務器從而請求 token。授權服務器首先會對密碼憑證信息進行認證,確認無誤后,向客戶端發放 token。整個流程如下圖所示:
請注意,授權服務器在這里執行認證操作的目的是驗證傳入的用戶名和密碼是否正確。在密碼模式下,這一步是必需的,如果采用其他授權模式,不一定會有用戶認證這一環節。
確定采用密碼模式后,我們來看為了實現這一授權模式,需要對授權服務器做哪些開發工作。首先我們需要設置一些基礎數據,包括客戶端信息和用戶信息。
設置客戶端信息
我們先來看如何設置客戶端信息。設置客戶端時,用到的配置類是 clientdetailsserviceconfigurer,該配置類用來配置客戶端詳情服務 clientdetailsservice。用于描述客戶端詳情的 clientdetails 接口則包含了與安全性控制相關的多個重要方法,該接口中的部分方法定義如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
public interface clientdetails extends serializable { //客戶端唯一性 id string getclientid(); //客戶端安全碼 string getclientsecret(); //客戶端的訪問范圍 set<string> getscope(); //客戶端可以使用的授權模式 set<string> getauthorizedgranttypes(); … } |
首先 clientid 是一個必備屬性,用來唯一標識客戶的 id,而 clientsecret 代表客戶端安全碼。這里的 scope 用來限制客戶端的訪問范圍,如果這個屬性為空,客戶端就擁有全部的訪問范圍。常見的設置方式可以是 webclient 或 mobileclient,分別代表 web 端和移動端。
最后,authorizedgranttypes 代表客戶端可以使用的授權模式,可選的范圍包括代表授權碼模式的 authorization_code、代表隱式授權模式 implicit、代表密碼模式的 password 以及代表客戶端憑據模式的 client_credentials。這個屬性在設置上也可以添加 refresh_token,通過刷新操作獲取以上授權模式下產生的新 token。
和實現認證過程類似,spring security 也提供了 authorizationserverconfigureradapter 這個配置適配器類來簡化配置類的使用方式。我們可以通過繼承該類并覆寫其中的 configure(clientdetailsserviceconfigurer clients) 方法進行配置。使用 authorizationserverconfigureradapter 進行客戶端信息配置的基本代碼結構如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
@configuration public class springauthorizationserverconfigurer extends authorizationserverconfigureradapter { @override public void configure(clientdetailsserviceconfigurer clients) throws exception { clients.inmemory() .withclient( "spring" ) .secret( "{noop}spring_secret" ) .authorizedgranttypes( "refresh_token" , "password" , "client_credentials" ) .scopes( "webclient" , "mobileclient" ); } } |
可以看到,我們創建了一個 springauthorizationserverconfigurer 類來繼承 authorizationserverconfigureradapter,并通過 clientdetailsserviceconfigurer 配置類設置了授權模式為密碼模式。在授權服務器中存儲客戶端信息有兩種方式,一種就是如上述代碼所示的基于內存級別的存儲,另一種則是通過 jdbc 在數據庫中存儲詳情信息。為了簡單起見,這里使用了內存級別的存儲方式。
同時我們注意到,在設置客戶端安全碼時使用了"{noop}spring_secret"這種格式。這是因為在 spring security 5 中統一使用 passwordencoder 對密碼進行編碼,在設置密碼時要求格式為“{id}password”。而這里的前綴“{noop}”就是代表具體 passwordencoder 的 id,表示我們使用的是 nooppasswordencoder。
@enableauthorizationserver 注解會暴露一系列的端點,而授權過程是使用 authorizationendpoint 這個端點進行控制的。要想對該端點的行為進行配置,你可以使用 authorizationserverendpointsconfigurer 這個配置類。和 clientdetailsserviceconfigurer 配置類一樣,我們也通過使用 authorizationserverconfigureradapter 配置適配器類進行配置。
因為我們指定了授權模式為密碼模式,而密碼模式包含認證環節。所以針對 authorizationserverendpointsconfigurer 配置類需要指定一個認證管理器 authenticationmanager,用于對用戶名和密碼進行認證。同樣因為我們指定了基于密碼的授權模式,所以需要指定一個自定義的 userdetailsservice 來替換全局的實現,可以通過如下代碼來配置 authorizationserverendpointsconfigurer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@configuration public class springauthorizationserverconfigurer extends authorizationserverconfigureradapter { @autowired private authenticationmanager authenticationmanager; @autowired private userdetailsservice userdetailsservice; @override public void configure(authorizationserverendpointsconfigurer endpoints) throws exception { endpoints.authenticationmanager(authenticationmanager).userdetailsservice(userdetailsservice); } } |
至此,客戶端設置工作全部完成,我們所做的事情就是實現了一個自定義的 springauthorizationserverconfigurer 配置類并覆寫了對應的配置方法。
設置用戶認證信息
設置用戶認證信息所依賴的配置類是 websecurityconfigurer 類,spring security 同樣提供了 websecurityconfigureradapter 類來簡化該配置類的使用方式,我們可以繼承 websecurityconfigureradapter 類并且覆寫其中的 configure() 的方法來完成配置工作。
關于 websecurityconfigurer 配置類,我們首先需要明確配置的內容。實際上,設置用戶信息非常簡單,只需要指定用戶名(user)、密碼(password)和角色(role)這三項數據即可,如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
@configuration public class springwebsecurityconfigurer extends websecurityconfigureradapter { @override @bean public authenticationmanager authenticationmanagerbean() throws exception { return super .authenticationmanagerbean(); } @override @bean public userdetailsservice userdetailsservicebean() throws exception { return super .userdetailsservicebean(); } @override protected void configure(authenticationmanagerbuilder builder) throws exception { builder .inmemoryauthentication() .withuser( "user1" ) .password( "{noop}password1" ) .roles( "user" ) .and() .withuser( "admin" ) .password( "{noop}password2" ) .roles( "user" , "admin" ); } } |
結合上面的代碼,我們看到構建了具有不同角色和密碼的兩個用戶,請注意"user1"代表的角色是一個普通用戶,"admin"則具有管理員角色。我們在設置密碼時,同樣需要添加前綴“{noop}”。同時,我們還看到 authenticationmanagerbean()和 userdetailsservicebean() 方法分別返回了父類的默認實現,而這里返回的 userdetailsservice 和 authenticationmanager 在前面設置客戶端時會用到。
生成 token
現在,oauth2 授權服務器已經構建完畢,啟動這個授權服務器,我們就可以獲取 token。我們在構建 oauth2 服務器時已經提到授權服務器中會暴露一批端點供 http 請求進行訪問,而獲取 token 的端點就是。在使用該端點時,我們需要提供前面配置的客戶端信息和用戶信息。
這里使用 postman 來模擬 http 請求,客戶端信息設置方式如下圖所示:
我們在“authorization”請求頭中指定認證類型為“basic auth”,然后設置客戶端名稱和客戶端安全碼分別為“spring”和“spring_secret”。
接下來我們指定針對授權模式的專用配置信息。首先是用于指定授權模式的 grant_type 屬性,以及用于指定客戶端訪問范圍的 scope 屬性,這里分別設置為 “password”和“webclient”。既然設置了密碼模式,所以也需要指定用戶名和密碼用于識別用戶身份,這里,我們以“spring_user”這個用戶為例進行設置,如下圖所示:
在 postman 中執行這個請求,會得到如下所示的返回結果:
1
2
3
4
5
6
7
|
{ "access_token" : "d2066f68-665b-4038-9dbe-5dd1035e75a0" , "token_type" : "bearer" , "refresh_token" : "44009836-731c-4e6a-9cc3-274ce3af8c6b" , "expires_in" : 3599 , "scope" : "all" } |
可以看到,除了作為請求參數的 scope,這個返回結果中還包含了 access_token、token_type、refresh_token 和 expires_in 等屬性。這些屬性都很重要。當然,因為每次請求生成的 token 都是唯一的,所以你在嘗試時獲取的結果可能與我的不同。
到此這篇關于java構建oauth2授權服務器的文章就介紹到這了,更多相關java構建oauth2授權服務器內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://juejin.cn/post/6982802237860872199