swagger 配置Token

?后臺有權限認證是springboot+jwt+shiro+swagger的升熊,swagger配置token?

1 首先在pom文件引入 依賴:

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger2</artifactId>

<version>2.2.2</version>

</dependency>

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger-ui</artifactId>

<version>2.2.2</version>

</dependency>

接下來: 下面代碼中?


.globalOperationParameters(pars); 是添加前面四行的進去的,前四行是swagger配置token的 代碼中有注釋


package com.example.dscjwtshrio.config;

import com.google.common.collect.Lists;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;

import springfox.documentation.builders.ParameterBuilder;

import springfox.documentation.builders.PathSelectors;

import springfox.documentation.builders.RequestHandlerSelectors;

import springfox.documentation.schema.ModelRef;

import springfox.documentation.service.ApiInfo;

import springfox.documentation.service.ApiKey;

import springfox.documentation.service.Parameter;

import springfox.documentation.spi.DocumentationType;

import springfox.documentation.spring.web.plugins.Docket;

import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

import java.util.List;

@Configuration

@EnableSwagger2

public class Swagger2 {

? ? @Bean

? ? public Docket createRestApi() {

//? ? ? ? ParameterBuilder tokenPar = new ParameterBuilder();

//

//? ? ? ? List<Parameter> pars = new ArrayList<>();

//? ? ? ? tokenPar.name("Authorization").description("token!").modelRef(new ModelRef("string")).parameterType("header").required(false).defaultValue("").build();

//? ? ? ? pars.add(tokenPar.build());

? ? ? ? ParameterBuilder ticketPar = new ParameterBuilder();

? ? ? ? List<Parameter> pars = new ArrayList<Parameter>();

? ? ? ? ticketPar.name("Token").description("user ticket")//Token 以及Authorization 為自定義的參數(shù)蹦肴,session保存的名字是哪個就可以寫成那個

? ? ? ? ? ? ? ? .modelRef(new ModelRef("string")).parameterType("header")

? ? ? ? ? ? ? ? .required(false).build(); //header中的ticket參數(shù)非必填僚碎,傳空也可以

? ? ? ? pars.add(ticketPar.build());? ? //根據(jù)每個方法名也知道當前方法在設置什么參數(shù)

? ? ? ? return new Docket(DocumentationType.SWAGGER_2)

? ? ? ? ? ? ? ? .apiInfo(apiInfo())

? ? ? ? ? ? ? ? .select()

? ? ? ? ? ? ? ? .apis(RequestHandlerSelectors.basePackage("com.example.dscjwtshrio"))

? ? ? ? ? ? ? ? .paths(PathSelectors.any())

? ? ? ? ? ? ? ? .build()

//? ? ? ? ? ? ? ? .securitySchemes(Lists.newArrayList(apiKey()))

? ? ? ? ? ? ? ? .globalOperationParameters(pars);

? ? }

? ? private ApiInfo apiInfo() {

? ? ? ? return new ApiInfoBuilder()

? ? ? ? ? ? ? ? .title("數(shù)據(jù)平臺接口中心")

? ? ? ? ? ? ? ? .description("數(shù)據(jù)平臺接口中心")

? ? ? ? ? ? ? ? .termsOfServiceUrl("http:www.baidu.com/")

? ? ? ? ? ? ? ? .contact("www")

? ? ? ? ? ? ? ? .version("1.0.1")

? ? ? ? ? ? ? ? .build();

? ? }

//? ? private ApiKey apiKey() {

//? ? ? ? return new ApiKey("apikey", "Authorization", "header");

//? ? }

}

3? ?接下來訪問:http://localhost:8080/swagger-ui.html#!/

4? 此時會出現(xiàn)下面這個,然后將token穿進去即可



?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末阴幌,一起剝皮案震驚了整個濱河市勺阐,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌矛双,老刑警劉巖渊抽,帶你破解...
    沈念sama閱讀 222,946評論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異议忽,居然都是意外死亡懒闷,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,336評論 3 399
  • 文/潘曉璐 我一進店門栈幸,熙熙樓的掌柜王于貴愁眉苦臉地迎上來愤估,“玉大人,你說我怎么就攤上這事速址⊥嫜妫” “怎么了?”我有些...
    開封第一講書人閱讀 169,716評論 0 364
  • 文/不壞的土叔 我叫張陵芍锚,是天一觀的道長昔园。 經(jīng)常有香客問我蔓榄,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,222評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮嘱丢,結果婚禮上,老公的妹妹穿的比我還像新娘澜搅。我一直安慰自己,他們只是感情好邪锌,可當我...
    茶點故事閱讀 69,223評論 6 398
  • 文/花漫 我一把揭開白布店展。 她就那樣靜靜地躺著,像睡著了一般秃流。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上柳弄,一...
    開封第一講書人閱讀 52,807評論 1 314
  • 那天舶胀,我揣著相機與錄音,去河邊找鬼碧注。 笑死嚣伐,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的萍丐。 我是一名探鬼主播轩端,決...
    沈念sama閱讀 41,235評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼逝变!你這毒婦竟也來了基茵?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 40,189評論 0 277
  • 序言:老撾萬榮一對情侶失蹤壳影,失蹤者是張志新(化名)和其女友劉穎拱层,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體宴咧,經(jīng)...
    沈念sama閱讀 46,712評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡根灯,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,775評論 3 343
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了掺栅。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片烙肺。...
    茶點故事閱讀 40,926評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖氧卧,靈堂內(nèi)的尸體忽然破棺而出桃笙,到底是詐尸還是另有隱情,我是刑警寧澤假抄,帶...
    沈念sama閱讀 36,580評論 5 351
  • 正文 年R本政府宣布怎栽,位于F島的核電站丽猬,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏熏瞄。R本人自食惡果不足惜脚祟,卻給世界環(huán)境...
    茶點故事閱讀 42,259評論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望强饮。 院中可真熱鬧由桌,春花似錦、人聲如沸邮丰。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,750評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽剪廉。三九已至娃循,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間斗蒋,已是汗流浹背捌斧。 一陣腳步聲響...
    開封第一講書人閱讀 33,867評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留泉沾,地道東北人捞蚂。 一個月前我還...
    沈念sama閱讀 49,368評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像跷究,于是被迫代替她去往敵國和親姓迅。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,930評論 2 361

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