Swagger2怎麼整合OAuth2來在線調試接口?

  • 2019 年 12 月 26 日
  • 筆記

知識改變命運,擼碼使我快樂,2019年你的發跡線還好嗎? 點贊再看,養成習慣 本篇文章對應源碼碼雲(Gitee)倉庫 https://gitee.com/minbox-projects/api-boot-chapter,您的Star是給我最大動力

前言

Swagger2作為侵入式文檔中比較出色的一員,支持接口認證的在線調試肯定是不在話下的,當我們在調用OAuth2所保護的接口時,需要將有效的AccessToken作為請求HeaderAuthorization的值時,我們才擁有了訪問權限,那麼我們在使用Swagger在線調試時該設置AccessToken的值呢? 本文所需ApiBoot相關鏈接:

創建示例項目

在之前文章「使用Swagger2作為文檔來描述你的接口信息」我們已經講到了使用Swagger2來簡單的描述接口,提供可視化在線的接口文檔,我們本章的主要目的是來集成使用OAuth2實現在線調試接口,我們把之前章節測試的接口UserController複製到本篇文章中以便於測試,本章項目pom.xml依賴如下所示:

<dependencies>    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-web</artifactId>    </dependency>    <dependency>      <groupId>org.minbox.framework</groupId>      <artifactId>api-boot-starter-swagger</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.1.RELEASE</version>        <type>pom</type>        <scope>import</scope>      </dependency>    </dependencies>  </dependencyManagement>

如果你看過ApiBoot SecurityApiBoot OAuth你應該知道,通過application.yml文件簡單的幾行配置就可以集成Spring Security整合OAuth2,本章來使用內存方式配置用戶列表以及客戶端列表

ApiBoot Security & ApiBoot OAuth組件使用系列文章:https://blog.yuqiyu.com/apiboot-all-articles.html 如果你想深入的了解這個神奇的ApiBoot安全組件,可以通過依賴文章匯總鏈接學習。

啟用ApiBoot Swagger

通過@EnableApiBootSwagger註解來啟用ApiBoot Swagger相關功能,在XxxApplication入口類配置如下所示:

@SpringBootApplication  @EnableApiBootSwagger  public class ApibootSwaggerIntegratedOauthApplication {        public static void main(String[] args) {          SpringApplication.run(ApibootSwaggerIntegratedOauthApplication.class, args);      }    }

配置ApiBoot Security

使用grant_type=password獲取AccessToken時,需要我們傳遞用戶的usernamepassword,使用默認的內存方式配置我們只需要在application.yml文件內添加如下配置:

api:    boot:      security:        # 配置安全用戶列表        users:          - username: yuqiyu            password: 123123        # 資源保護路徑前綴,默認為/api/**        auth-prefix: /**

配置ApiBoot OAuth

我們來添加OAuth2所需要的客戶端列表配置信息,使用默認的內存方式配置客戶端的client-idclient-secret,只需要修改application.yml文件內容如下所示:

api:    boot:      oauth:        # 配置OAuth客戶端列表        clients:          - clientId: minbox            clientSecret: chapter

了解資源文件排除攔截

Swagger2可視化界面有很多靜態資源組成,比如:js/css/images等,而集成Spring Security後這些資源需要排除權限攔截才可以訪問到,如果是使用傳統的方式整合Spring Security,需要使用WebSecurity來進行忽略路徑才可以,而通過ApiBoot Security則是不用考慮這一點,在內部已經對Swagger的靜態資源文件做出了排除。

運行測試

通過Application方式啟動本章項目,Swagger可視化界面訪問:http://localhost:8080/swagger-ui.html

獲取AccessToken

通過CURL方式獲取用戶:yuqiyu的請求令牌,如下所示:

➜ ~ curl -X POST minbox:chapter@localhost:8080/oauth/token -d 'grant_type=password&username=yuqiyu&password=123123'  {"access_token":"304676a4-b9a6-4c4d-af40-e439b934aba8","token_type":"bearer","refresh_token":"ee2b5744-6947-4677-862e-fcf9517afca5","expires_in":7199,"scope":"api"}

Swagger在線調試

我們把獲取的AccessToken與類型進行組合成:Bearer 304676a4-b9a6-4c4d-af40-e439b934aba8,將該令牌字符串配置到Swagger界面上,如下圖所示:

輸入後點擊Authorize按鈕即可。

敲黑板,劃重點

Swagger的在線調試其實內部是模擬發送請求,將界面上輸入的參數進行組合裝配,發送到需要測試的接口路徑,而上圖設置AccessToken,也是一個臨時保存,刷新頁面就會丟失,發送請求時會自動追加到Request Header列表內。

代碼示例

如果您喜歡本篇文章請為源碼倉庫點個Star,謝謝!!! 本篇文章示例源碼可以通過以下途徑獲取,目錄為apiboot-swagger-integrated-oauth

作者個人 博客 使用開源框架 ApiBoot 助你成為Api接口服務架構師