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


轉:http://blog.csdn.net/u014044812/article/details/71473226

大家使用swagger往往會和JWT一起使用,而一般使用jwt會將token放在head里,這樣我們在使用swagger測試的時候並不方便,因為跨域問題它默認不能自定義head參數。然后自己去網上找,發現國內大多數的都是寫一個Filter接口,然后添加到配置。這樣極大的破壞了程序的完整性。想想這相當於維護兩套代碼。我們只是需要一個簡單的小功能,國外大多是修改Swagger的index頁面:

 

[html]  view plain  copy
 
  1. window.swaggerUi = new SwaggerUi({  
  2.             discoveryUrl: "http://pathtomyservice.com/resources",  
  3.                 headers: { "testheader" : "123" },  
  4.                 apiKey: "123",  
  5.                 apiKeyName: "Api-Key",  
  6.                 dom_id:"swagger-ui-container",  
  7.                 supportHeaderParams: true,  
  8.                 supportedSubmitMethods: ['get', 'post', 'put', 'delete'],  
  9.                 onComplete: function(swaggerApi, swaggerUi){  
  10.                     if(console) {  
  11.                         console.log("Loaded SwaggerUI");  
  12.                         console.log(swaggerApi);  
  13.                         console.log(swaggerUi);  
  14.                     }  
  15.                   $('pre code').each(function(i, e) {hljs.highlightBlock(e)});  
  16.                 },  
  17.                 onFailure: function(data) {  
  18.                     if(console) {  
  19.                         console.log("Unable to Load SwaggerUI");  
  20.                         console.log(data);  
  21.                     }  
  22.                 },  
  23.                 docExpansion: "none"  
  24.             });  

supportHeaderParams默認為false,而我用的是swagger2,不需要配置靜態的那些東西,所以我在SwaggerConfig添加了幾行代碼:

 

 

[java]  view plain  copy
 
  1. @EnableSwagger2  
  2. @EnableWebMvc  
  3. @ComponentScan("com.g.web")  
  4. public class SwaggerConfig {  
  5.     @Bean  
  6.     public Docket api(){  
  7.         ParameterBuilder tokenPar = new ParameterBuilder();  
  8.         List<Parameter> pars = new ArrayList<Parameter>();  
  9.         tokenPar.name("x-access-token").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();  
  10.         pars.add(tokenPar.build());  
  11.         return new Docket(DocumentationType.SWAGGER_2)  
  12.             .select()  
  13.             .apis(RequestHandlerSelectors.any())  
  14.             .paths(PathSelectors.regex("/api/.*"))  
  15.             .build()  
  16.             .globalOperationParameters(pars)  
  17.             .apiInfo(apiInfo());  
  18.     }  
  19.   
  20.     private ApiInfo apiInfo() {  
  21.         return new ApiInfoBuilder()  
  22.             .title("后台接口文檔與測試")  
  23.             .description("這是一個給app端人員調用server端接口的測試文檔與平台")  
  24.             .version("1.0.0")  
  25.             .termsOfServiceUrl("http://terms-of-services.url")  
  26.             //.license("LICENSE")  
  27.             //.licenseUrl("http://url-to-license.com")  
  28.             .build();  
  29.     }  
  30. }  


前四行代碼是添加head參數的,前台效果是這樣的:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM