{"id":687,"date":"2020-06-14T22:57:48","date_gmt":"2020-06-14T14:57:48","guid":{"rendered":"https:\/\/blog.humh.cn\/?p=687"},"modified":"2020-06-14T22:57:49","modified_gmt":"2020-06-14T14:57:49","slug":"swagger%e4%b9%8b%e6%8e%a5%e5%8f%a3header%e6%8e%a7%e5%88%b6","status":"publish","type":"post","link":"https:\/\/blog.humh.cn\/?p=687","title":{"rendered":"Swagger\u4e4b\u63a5\u53e3header\u63a7\u5236"},"content":{"rendered":"\n<p>\u5728\u5927\u90e8\u5206\u9879\u76ee\u4e2d\uff0c\u90fd\u5b58\u5728\u6743\u9650\u63a7\u5236\uff0c\u57fa\u672c\u4e0a\u5927\u90e8\u5206\u7684\u63a5\u53e3\u90fd\u9700\u8981\u7528\u6237\u7684\u767b\u5f55\u4fe1\u606f\u3002\u73b0\u5728\u4e3b\u6d41\u7684\u91c7\u7528\u5982jwt\u6216\u8005\u5176\u4ed6\u65b9\u5f0f\uff0c\u6765\u901a\u8fc7\u8bf7\u6c42\u65f6\u5411header\u52a0\u5165token\uff0c\u7136\u540e\u670d\u52a1\u7aef\u89e3\u6790token\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u5728swagger\u751f\u6210\u7684\u63a5\u53e3\u6587\u6863\u4e0a\u4e5f\u8981\u8fdb\u884cheader\u63a7\u5236\uff0c\u9664\u4e86\u63a5\u53e3\u53c2\u6570\u4ee5\u5916\uff0c\u8fd8\u8981\u8f93\u5165token header\u3002 <\/p>\n\n\n\n<p>\u8fd9\u91cc\u63d0\u4f9bswagger header\u4e09\u79cd\u65b9\u5f0f\uff1a <\/p>\n\n\n\n<h4><strong>\u4e00\u3001\u5728\u6bcf\u4e2acontroller\u63a5\u53e3\u4e0a\uff0c\u6807\u8bb0swagger\u6ce8\u89e3\u5b8c\u6210\uff08@ApiImplicitParam\uff09<\/strong><\/h4>\n\n\n\n<p>@ApiImplicitParam \u6307\u5b9a\u4e00\u4e2a\u8bf7\u6c42\u53c2\u6570\u7684\u914d\u7f6e\u4fe1\u606f\uff0c\u5bf9\u63a5\u53e3\u53c2\u6570\u8fdb\u884c\u914d\u7f6e\u3002\u4ed6\u6709\u51e0\u4e2a\u5c5e\u6027\u5982\u4e0b\uff1a&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name\uff1a\u53c2\u6570\u540d<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value\uff1a\u53c2\u6570\u7684\u8bf4\u660e\u3001\u89e3\u91ca<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;required\uff1a\u53c2\u6570\u662f\u5426\u5fc5\u987b\u4f20\uff0c\u9ed8\u8ba4false<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paramType\uff1a\u53c2\u6570\u653e\u5728\u54ea\u4e2a\u5730\u65b9<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u00b7 header &#8211;&gt; \u8bf7\u6c42\u5934<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u00b7 query &#8211;&gt; \u4e00\u822curlcode\u4e2d\u7684\u201ckey=value\u201d\uff0c\u4e5f\u5c31\u662f\u76f8\u5f53\u4e8e@RequestParam\u6807\u8bb0\u7684\u53c2\u6570<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u00b7 path &#8211;&gt; get\u8bf7\u6c42url\u4e2d\u7684\u201c\/{userId}\u201d&nbsp;,\u4e5f\u5c31\u662f\u76f8\u5f53\u4e8e@PathVariable\u6807\u8bb0\u7684\u53c2\u6570<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u00b7 body\uff08\u4e0d\u5e38\u7528\uff09<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u00b7 form\uff08\u4e0d\u5e38\u7528\uff09 &nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataType\uff1a\u53c2\u6570\u7c7b\u578b\uff0c\u9ed8\u8ba4String\uff0c\u5176\u5b83\u503cdataType=&#8221;Integer&#8221; \u7b49&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;defaultValue\uff1a\u53c2\u6570\u7684\u9ed8\u8ba4\u503c<\/p>\n\n\n\n<p>@ApiImplicitParams\uff1a\u7528\u5728\u8bf7\u6c42\u7684\u65b9\u6cd5\u4e0a\uff0c\u5305\u542b\u4e00\u7ec4\u53c2\u6570\u8bf4\u660e\uff0c\u7528\u4e8e\u591a\u4e2a@ApiImplicitParam\u6ce8\u89e3\u65f6\u3002<\/p>\n\n\n\n<p>1\u3001header\u4e2d\u5305\u542buserId\u503c\uff0cget\u63a5\u53e3\u5165\u53c2 key\uff0c\u90fd\u7528@ApiImplicitParam\u6ce8\u89e3\u8868\u793a\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    @ApiOperation(value = \"\u83b7\u53d6\u897f\u5de5\u5927\u5b66\u6821\u4fe1\u606f\", notes = \"\u8be5\u63a5\u53e3\u8d1f\u8d23\u4ece\u9879\u76ee\u914d\u7f6e\u4e2d\u53d6\u51fa\u897f\u5de5\u5927\u7684\u76f8\u5173\u4fe1\u606f\")\n    @ApiImplicitParams({\n            @ApiImplicitParam(paramType = \"header\", name = \"userId\", value = \"\u767b\u5f55\u7528\u6237id\", dataType = \"Integer\", required = false),\n            @ApiImplicitParam(paramType = \"query\", name = \"key\", value = \"\u5173\u952e\u5b57\", dataType = \"Integer\", required = true)\n    })\n    @GetMapping(\"\/nwpu\")\n    public NwpuVO getNwpuInfo(@RequestParam String key) {\n        return new NwpuVO(schoolService.getNwpuInfo());\n    }<\/pre>\n\n\n\n<p>2\u3001\u5176\u4e2dget\u63a5\u53e3\u5165\u53c2key\u4e5f\u53ef\u4ee5@ApiParam\u8868\u793a\uff08\u5982\u679c\u6ca1\u6709\u63a5\u53e3\u5165\u53c2\uff0c\u53ef\u4ee5\u53bb\u6389\uff09\uff0cheader\u8fd8\u662f@ApiImplicitParam\u6ce8\u89e3\u6807\u8bb0\uff1a <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    @ApiOperation(value = \"\u83b7\u53d6\u897f\u5de5\u5927\u5b66\u6821\u4fe1\u606f\", notes = \"\u8be5\u63a5\u53e3\u8d1f\u8d23\u4ece\u9879\u76ee\u914d\u7f6e\u4e2d\u53d6\u51fa\u897f\u5de5\u5927\u7684\u76f8\u5173\u4fe1\u606f\")\n    @ApiImplicitParam(paramType = \"header\", name = \"userId\", value = \"\u767b\u5f55\u7528\u6237id\", dataType = \"Integer\", required = false)\n    @GetMapping(\"\/nwpu\")\n    public NwpuVO getNwpuInfo(@ApiParam(name = \"key\", value = \"\u5173\u952e\u5b57\", required = true) @RequestParam String key) {\n        return new NwpuVO(schoolService.getNwpuInfo());\n    }<\/pre>\n\n\n\n<p>\u5982\u4e0a\u65b9\u5f0f\uff0c\u6548\u679c\u5747\u5982\u4e0b\u56fe\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a href=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-14.png\"><img width=\"1106\" height=\"872\" class=\"wp-image-689 lazyload\" title=\"Swagger\u4e4b\u63a5\u53e3header\u63a7\u5236\" src=\"data:image\/gif;base64,R0lGODlhAgABAIAAALGxsQAAACH5BAAAAAAALAAAAAACAAEAAAICBAoAOw==\" data-src=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-14.png\" alt=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-14.png\" data-srcset=\"https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-14.png 1106w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-14-300x237.png 300w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-14-1024x807.png 1024w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-14-768x606.png 768w\" sizes=\"(max-width: 1106px) 100vw, 1106px\"><\/a><\/figure><\/div>\n\n\n\n<h4><strong>\u4e8c\u3001\u6dfb\u52a0\u4e3a\u5168\u5c40\u53c2\u6570<\/strong><\/h4>\n\n\n\n<p>\u9488\u5bf9\u91c7\u7528swagger\u914d\u7f6e\u7c7b\u7684\u65b9\u5f0f\u914d\u7f6eswagger\u60c5\u51b5\u4e0b\uff0c\u53ef\u5728swagger\u914d\u7f6e\u7c7b\u4e2d\u8fdb\u884cheader\u63a7\u5236\uff0c\u56e0\u4e3a\u5927\u90e8\u5206\u7684\u63a5\u53e3\u90fd\u9700\u8981token header\uff0c\u6240\u4ee5\u53ef\u4ee5\u5c06\u5176\u4f5c\u4e3a\u5168\u5c40\u53c2\u6570\u914d\u7f6e\u6bcf\u4e00\u4e2a\u5728swagger\u63a5\u53e3\u4e0a\u3002<\/p>\n\n\n\n<p>ParameterBuilder\u4e3aSpringFox\u9879\u76ee\u4e2d\u7684\u53c2\u6570\u6784\u9020\u5668\uff0c\u53ef\u4ee5\u7528\u6765\u6784\u9020\u63a5\u53e3\u53c2\u6570\uff0c\u4e00\u79cd\u5de5\u5382\u6a21\u578b\uff0c\u53ef\u76f4\u63a5\u6784\u9020\u65b9\u6cd5\u521d\u59cb\u5316\u5c5e\u6027\uff0c\u4e5f\u53ef\u5206\u522bset\u3002\u5176\u4e2d\u90e8\u5206api\u8bf4\u660e\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>\u548c@ApiImplicitParam \u7684\u5c5e\u6027\u542b\u4e49\u57fa\u672c\u4e00\u81f4\uff1a<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name\uff1a\u6307\u5b9a\u53c2\u6570\u540d<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;description\uff1a\u53c2\u6570\u7684\u8bf4\u660e\u3001\u63cf\u8ff0\u89e3\u91ca<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;required\uff1a\u53c2\u6570\u662f\u5426\u5fc5\u987b\u4f20\uff0c\u9ed8\u8ba4false<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parameterType\uff1a\u53c2\u6570\u653e\u5728\u54ea\u4e2a\u5730\u65b9<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u00b7 header &#8211;&gt; \u8bf7\u6c42\u5934<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u00b7 query &#8211;&gt; \u4e00\u822curlcode\u4e2d\u7684\u201ckey=value\u201d\uff0c\u4e5f\u5c31\u662f\u76f8\u5f53\u4e8e@RequestParam\u6807\u8bb0\u7684\u53c2\u6570<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u00b7 path &#8211;&gt; get\u8bf7\u6c42url\u4e2d\u7684\u201c\/{userId}\u201d&nbsp;,\u4e5f\u5c31\u662f\u76f8\u5f53\u4e8e@PathVariable\u6807\u8bb0\u7684\u53c2\u6570<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u00b7 body\uff08\u4e0d\u5e38\u7528\uff09<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u00b7 form\uff08\u4e0d\u5e38\u7528\uff09 &nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;modelRef\uff1a\u6307\u5b9a\u53c2\u6570\u5f15\u7528\u7c7b\u578b\uff0cString\uff0cInteger\u7b49<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;defaultValue\uff1a\u53c2\u6570\u7684\u9ed8\u8ba4\u503c<\/p>\n\n\n\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Configuration\n@EnableSwagger2\npublic class SwaggerConfig {\n \n    private String SCAN_BASE_PACKAGE = \"cn.code\";\n    private String VERSION = \"1.0.0\";\n \n     private ApiInfo apiInfo() {\n        return new ApiInfoBuilder()\n                \/\/ \u6807\u9898\n                .title(\"Server API\")\n                \/\/ \u63cf\u8ff0\n                .description(\"API\u6587\u6863\")\n                \/\/ \u6761\u6b3e\u5730\u5740\uff08\u516c\u53f8\u5185\u90e8\u4f7f\u7528\u65e0\u9700\u914d\u7f6e\uff09\n                .termsOfServiceUrl(\"\")\n                \/\/ \u63a5\u53e3\uff08\u6587\u6863\uff09\u7248\u672c\n                .version(VERSION)\n                .build();\n    }\n \n    @Bean\n    public Docket apiDocket() {\n \n        \/\/ \u5168\u5c40\u53c2\u6570\n        ParameterBuilder tokenPar = new ParameterBuilder();\n        List&lt;Parameter&gt; pars = new ArrayList&lt;&gt;();\n        tokenPar.name(\"userId\").description(\"token userId\").modelRef(new ModelRef(\"string\"))\n                .parameterType(\"header\").required(true).build();\n        pars.add(tokenPar.build());\n \n        return new Docket(DocumentationType.SWAGGER_2)\n                .groupName(\"RestfulApi\")\n                .select()\n                \/\/ \u5305\u626b\u63cf\u8303\u56f4\uff08\u5bf9\u6307\u5b9a\u7684\u5305\u4e0b\u8fdb\u884c\u626b\u63cf\uff0c\u5982\u679c\u6807\u6ce8\u6709\u76f8\u5173swagger\u6ce8\u89e3\uff0c\u5219\u751f\u6210\u76f8\u5e94\u6587\u6863\uff09\n                .apis(RequestHandlerSelectors.basePackage(SCAN_BASE_PACKAGE))\n                \/\/ \u8fc7\u6ee4\u6389\u54ea\u4e9bpath\u4e0d\u7528\u751f\u6210swagger\n                .paths(PathSelectors.any())\n                .build()\n                \/\/ \u5ffd\u7565\u8be5\u53c2\u6570\u5728swagger\u4e0a\u7684\u663e\u793a\n                .ignoredParameterTypes()\n                .apiInfo(apiInfo())\n                \/\/ \u6307\u5b9a\u5168\u5c40\u53c2\u6570\n                .globalOperationParameters(pars)\n                \/\/ swagger\u751f\u6548\n                .enable(true);\n    }\n}<\/pre>\n\n\n\n<p>\u6548\u679c\u5982\u4e0b\u56fe\uff0c\u6240\u6709\u63a5\u53e3\u4e0a\u5747\u6709\u8be5\u5168\u5c40\u53c2\u6570\uff1a <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a href=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-15.png\"><img width=\"1068\" height=\"904\" class=\"wp-image-690 lazyload\" title=\"Swagger\u4e4b\u63a5\u53e3header\u63a7\u5236\" src=\"data:image\/gif;base64,R0lGODlhAgABAIAAALGxsQAAACH5BAAAAAAALAAAAAACAAEAAAICBAoAOw==\" data-src=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-15.png\" alt=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-15.png\" data-srcset=\"https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-15.png 1068w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-15-300x254.png 300w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-15-1024x867.png 1024w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-15-768x650.png 768w\" sizes=\"(max-width: 1068px) 100vw, 1068px\"><\/a><\/figure><\/div>\n\n\n\n<h4><strong>\u4e09\u3001\u65e0\u9700\u6bcf\u4e2aswagger\u63a5\u53e3\u4e0a\u663e\u793a\u8be5\u53c2\u6570\uff0c\u907f\u514d\u91cd\u590d\u8f93\u5165\u53c2\u6570<\/strong><\/h4>\n\n\n\n<p>\u4e0a\u9762\u4e24\u79cd\u65b9\u5f0f\u5b58\u5728\u4e00\u4e2a\u5171\u540c\u7684\u95ee\u9898\uff0c\u5c31\u662f\u6bcf\u4e2aswagger\u63a5\u53e3\u4e0a\u5747\u6709\u8be5\u53c2\u6570\uff0c\u6bcf\u4e2a\u63a5\u53e3\u90fd\u9700\u8981\u8f93\u5165\u4e00\u904d\uff0c\u4e00\u79cd\u91cd\u590d\u6027\u7684\u64cd\u4f5c\u3002\u540c\u65f6\u7b2c\u4e8c\u79cd\u65b9\u5f0f\uff0c\u8bbe\u7f6e\u5168\u5c40\u53c2\u6570\uff0c\u8fd8\u5b58\u5728\u4e00\u4e2a\u95ee\u9898\uff0c\u5c31\u662f\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u9020\u6210\u6bcf\u4e2aswagger\u63a5\u53e3\u4e0a\u5747\u6709\u8be5\u53c2\u6570\uff0c\u4f46\u5b9e\u9645\u9879\u76ee\u4e2d\u53ef\u80fd\u6709\u4e9b\u63a5\u53e3\u662f\u4e0d\u9700\u8981\u8be5\u53c2\u6570\u7684\u3002\u62fftoken header\u4e3e\u4f8b\uff0c\u5bf9\u4e8e\u767b\u5f55\u63a5\u53e3\u5c31\u4e0d\u9700\u8981\u3002\u4e0a\u9762\u8fd9\u79cd\u65b9\u5f0f\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u8bdd\uff0c\u9700\u8981\u8fdb\u884c\u4e00\u4e9b\u989d\u5916\u7684\u64cd\u4f5c\uff0c\u5f88\u9ebb\u70e6\u3002\u6240\u4ee5\u6211\u4eec\u5e0c\u671b\uff0c\u53ea\u7528\u5728\u4e00\u4e2a\u5730\u65b9\u8f93\u5165\u6307\u5b9a\u4e86\u8be5\u53c2\u6570\uff0c\u9700\u8981\u8be5\u53c2\u6570\u7684\u63a5\u53e3\u90fd\u4e0d\u9700\u8981\u5728\u989d\u5916\u8f93\u5165\u4e86\uff0c\u8fd9\u6837\u66f4\u65b9\u4fbf\u5feb\u6377\uff0c\u5e76\u4e14\u4e0d\u9700\u8981\u7684\u5730\u65b9\u53ef\u4ee5\u81ea\u52a8\u8fc7\u6ee4\u3002<\/p>\n\n\n\n<p>\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\nimport springfox.documentation.builders.ApiInfoBuilder;\nimport springfox.documentation.builders.PathSelectors;\nimport springfox.documentation.builders.RequestHandlerSelectors;\nimport springfox.documentation.service.*;\nimport springfox.documentation.spi.DocumentationType;\nimport springfox.documentation.spi.service.contexts.SecurityContext;\nimport springfox.documentation.spring.web.plugins.Docket;\nimport springfox.documentation.swagger2.annotations.EnableSwagger2;\n\nimport java.util.ArrayList;\nimport java.util.Arrays;\nimport java.util.List;\n\n\/**\n * @Description:swagger\u914d\u7f6e\u7c7b\n * @Author: \n * @Date: 2019-11-18\n *\/\n\n\n@Configuration\n@EnableSwagger2\npublic class SwaggerConfig {\n\n    private String SCAN_BASE_PACKAGE = \"cn.susoncloud\";\n    private String VERSION = \"1.0.0\";\n\n     private ApiInfo apiInfo() {\n        return new ApiInfoBuilder()\n                \/\/ \u6807\u9898\n                .title(\"Server API\")\n                \/\/ \u63cf\u8ff0\n                .description(\"API\u6587\u6863\")\n                \/\/ \u6761\u6b3e\u5730\u5740\uff08\u516c\u53f8\u5185\u90e8\u4f7f\u7528\u65e0\u9700\u914d\u7f6e\uff09\n                .termsOfServiceUrl(\"\")\n                \/\/ \u63a5\u53e3\uff08\u6587\u6863\uff09\u7248\u672c\n                .version(VERSION)\n                .build();\n    }\n\n    @Bean\n    public Docket apiDocket() {\n        return new Docket(DocumentationType.SWAGGER_2)\n                .groupName(\"RestfulApi\")\n                .select()\n                \/\/ \u5305\u626b\u63cf\u8303\u56f4\uff08\u5bf9\u6307\u5b9a\u7684\u5305\u4e0b\u8fdb\u884c\u626b\u63cf\uff0c\u5982\u679c\u6807\u6ce8\u6709\u76f8\u5173swagger\u6ce8\u89e3\uff0c\u5219\u751f\u6210\u76f8\u5e94\u6587\u6863\uff09\n                .apis(RequestHandlerSelectors.basePackage(SCAN_BASE_PACKAGE))\n                \/\/ \u8fc7\u6ee4\u6389\u54ea\u4e9bpath\u4e0d\u7528\u751f\u6210swagger\n                .paths(PathSelectors.any())\n                .build()\n                \/\/ \u5ffd\u7565\u8be5\u53c2\u6570\u5728swagger\u4e0a\u7684\u663e\u793a\n                .ignoredParameterTypes()\n                \/\/ \u914d\u7f6eswagger\u63a5\u53e3\u5b89\u5168\u6821\u9a8c\u89c4\u5219\n                .securitySchemes(securitySchemes())\n                \/\/ \u914d\u7f6eswagger\u63a5\u53e3\u5b89\u5168\u6821\u9a8c\u4e0a\u4e0b\u6587\u4e2d\u7684\u4fe1\u606f\uff08\u5305\u542b\u5b89\u5168\u6743\u9650\u4e0e\u5b89\u5168\u6821\u9a8c\u751f\u6548\u7684\u63a5\u53e3\u8def\u5f84\uff09\n                .securityContexts(securityContexts())\n                .apiInfo(apiInfo())\n                \/\/ swagger\u751f\u6548\n                .enable(true);\n    }\n\n    private List&lt;ApiKey&gt; securitySchemes() {\n        return new ArrayList&lt;ApiKey&gt;(){{\n            add(new ApiKey(\"userId\", \"userId\", \"header\"));\n        }};\n    }\n\n    private List&lt;SecurityContext&gt; securityContexts() {\n        return new ArrayList&lt;SecurityContext&gt;(){{\n           add(SecurityContext.builder()\n                   .securityReferences(defaultAuth())\n                   .forPaths(PathSelectors.any())\n                   .build());\n        }};\n    }\n\n    private List&lt;SecurityReference&gt; defaultAuth() {\n        AuthorizationScope authorizationScope = new AuthorizationScope(\"global\", \"accessEverything\");\n        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];\n        authorizationScopes[0] = authorizationScope;\n        return Arrays.asList(\n                new SecurityReference(\"userId\", authorizationScopes));\n    }\n}<\/pre>\n\n\n\n<p>\u6548\u679c\u56fe\u5982\u4e0b\uff1a\u5728swagger-ui\u4e0a\u4f1a\u663e\u793a\u4e00\u4e2aAuthorize\u6309\u94ae\uff0c\u70b9\u5f00\uff0c\u53ef\u4ee5\u770b\u5230\u9700\u8981\u6dfb\u52a0\u7684\u63a5\u53e3\u8ba4\u8bc1\u53c2\u6570\uff0c\u4e5f\u5c31\u662f\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u6307\u5b9a\u7684header userId\u3002\u5728\u672a\u6dfb\u52a0\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u770b\u5230\u63a5\u53e3\u4f1a\u6709\u7ea2\u8272\u611f\u53f9\u53f7\uff0c\u6dfb\u52a0\u540e\uff0c\u4f1a\u53d8\u6210\u84dd\u8272\u8fdb\u884c\u63d0\u793a\u3002\u8fd9\u91cc\u6ce8\u610f\u6dfb\u52a0\u4e0e\u4e0d\u6dfb\u52a0\u5747\u53ef\u4ee5\u8bbf\u95ee\u63a5\u53e3\uff0c\u63a5\u53e3\u9274\u6743\u901a\u4e0d\u901a\u8fc7\u5c31\u53d6\u51b3\u4e8e\u5185\u90e8\u63a5\u53e3\u903b\u8f91\u4e86\u3002\u5982\u679c\u4e0d\u586bAuthorize\u7684\u8bdd\uff0c\u63a5\u53e3\u5c31\u4e0d\u4f1a\u4f20\u5165 header userId\u3002 <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a href=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-16.png\"><img width=\"1096\" height=\"841\" class=\"wp-image-691 lazyload\" title=\"Swagger\u4e4b\u63a5\u53e3header\u63a7\u5236\" src=\"data:image\/gif;base64,R0lGODlhAgABAIAAALGxsQAAACH5BAAAAAAALAAAAAACAAEAAAICBAoAOw==\" data-src=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-16.png\" alt=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-16.png\" data-srcset=\"https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-16.png 1096w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-16-300x230.png 300w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-16-1024x786.png 1024w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-16-768x589.png 768w\" sizes=\"(max-width: 1096px) 100vw, 1096px\"><\/a><\/figure><\/div>\n\n\n\n<p> \u586b\u5199Authorize\u540e\u7684\u63a5\u53e3\u8bbf\u95ee <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a href=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-17.png\"><img width=\"984\" height=\"790\" class=\"wp-image-692 lazyload\" title=\"Swagger\u4e4b\u63a5\u53e3header\u63a7\u5236\" src=\"data:image\/gif;base64,R0lGODlhAgABAIAAALGxsQAAACH5BAAAAAAALAAAAAACAAEAAAICBAoAOw==\" data-src=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-17.png\" alt=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-17.png\" data-srcset=\"https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-17.png 984w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-17-300x241.png 300w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-17-768x617.png 768w\" sizes=\"(max-width: 984px) 100vw, 984px\"><\/a><\/figure><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5927\u90e8\u5206\u9879\u76ee\u4e2d\uff0c\u90fd\u5b58\u5728\u6743\u9650\u63a7\u5236\uff0c\u57fa\u672c\u4e0a\u5927\u90e8\u5206\u7684\u63a5\u53e3\u90fd\u9700\u8981\u7528\u6237\u7684\u767b\u5f55\u4fe1\u606f\u3002\u73b0\u5728\u4e3b\u6d41\u7684\u91c7\u7528\u5982jwt\u6216\u8005\u5176\u4ed6\u65b9\u5f0f\uff0c\u6765 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[51],"tags":[35,89],"_links":{"self":[{"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/posts\/687"}],"collection":[{"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=687"}],"version-history":[{"count":4,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/posts\/687\/revisions"}],"predecessor-version":[{"id":696,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/posts\/687\/revisions\/696"}],"wp:attachment":[{"href":"https:\/\/blog.humh.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=687"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}