Swagger2 非全局、無需重復(fù)輸入的Head參數(shù)(Token)配置

去我的博客查看

網(wǎng)絡(luò)上關(guān)于Swagger2的教程多如牛毛,關(guān)于Swagger加入全局head參數(shù)(如token)的文章也很多。例如:

Swagger2 添加HTTP head參數(shù)

Swagger2 添加HTTP head參數(shù),解決用戶是token信息保留

但上述方案存在2個(gè)不足之處:

  1. 需要在每個(gè)接口下單獨(dú)輸入?yún)?shù)
  2. 全局配置了參數(shù),如果某些接口(如login等)不需要參數(shù),則必須在改接口中通過annotation現(xiàn)實(shí)聲明,較為麻煩

綜上,選擇優(yōu)化方案如下:

  1. 通過Swagger2的securitySchemes配置全局參數(shù):如下列代碼所示,securitySchemes的ApiKey中增加一個(gè)名為“Authorization”,type為“header”的參數(shù)。
private List<ApiKey> securitySchemes() {
        return newArrayList(
                new ApiKey("Authorization", "Authorization", "header"));
 }
  1. 在Swagger2的securityContexts中通過正則表達(dá)式,設(shè)置需要使用參數(shù)的接口(或者說,是去除掉不需要使用參數(shù)的接口),如下列代碼所示,通過PathSelectors.regex("^(?!auth).*$"),所有包含"auth"的接口不需要使用securitySchemes。即不需要使用上文中設(shè)置的名為“Authorization”,type為“header”的參數(shù)。
private List<SecurityContext> securityContexts() {
        return newArrayList(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("^(?!auth).*$"))
                        .build()
        );
    }

設(shè)置完成后進(jìn)入SwaggerUI,右上角出現(xiàn)“Authorization”按鈕,點(diǎn)擊即可輸入我們配置的參數(shù)。
對(duì)于不需要輸入?yún)?shù)的接口(上文所述的包含auth的接口),在未輸入Authorization參數(shù)就可以訪問。
其他接口則將返回401錯(cuò)誤。點(diǎn)擊右上角“Authorization”按鈕,輸入配置的參數(shù)后即可訪問。參數(shù)輸入后全局有效,無需每個(gè)接口單獨(dú)輸入。


image.png

至此,完成Swagger2 非全局、無需重復(fù)輸入的Head參數(shù)配置。
Swagger2的相關(guān)完整代碼如下(工程基于Springboot):

@Configuration
@EnableSwagger2
public class Swagger {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2).
                useDefaultResponseMessages(false)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.regex("^(?!auth).*$"))
                .build()
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts())
                ;
    }

    private List<ApiKey> securitySchemes() {
        return newArrayList(
                new ApiKey("Authorization", "Authorization", "header"));
    }

    private List<SecurityContext> securityContexts() {
        return newArrayList(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("^(?!auth).*$"))
                        .build()
        );
    }

    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return newArrayList(
                new SecurityReference("Authorization", authorizationScopes));
    }
}

去我的博客查看

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,969評(píng)論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,959評(píng)論 6 342
  • iOS網(wǎng)絡(luò)架構(gòu)討論梳理整理中。。。 其實(shí)如果沒有APIManager這一層是沒法使用delegate的,畢竟多個(gè)單...
    yhtang閱讀 5,252評(píng)論 1 23
  • 國(guó)家電網(wǎng)公司企業(yè)標(biāo)準(zhǔn)(Q/GDW)- 面向?qū)ο蟮挠秒娦畔?shù)據(jù)交換協(xié)議 - 報(bào)批稿:20170802 前言: 排版 ...
    庭說閱讀 11,172評(píng)論 6 13
  • 這一年自己很叛逆,自以為是,不把任何人放在眼里,全世界就我最牛逼知道的最多做法最正確,只有我一個(gè)人堅(jiān)守夢(mèng)想,只有我...
    嘻嘻嘻東閱讀 276評(píng)論 0 0