考虑以下示例:
@RequestMapping("myPath")
public Mono<MyDto> myMethod(@RequestParam(value = "amount") Long amount, @AuthenticationPrincipal MyUser user) {
}
为了从参数列表中跳过MyUser
,我将@AuthenticationPrincipal
添加到exclusions和springfox生成的swagger文档中,如下所示:
"/myPath": {
"get": {
"tags": [
"my-controller"
],
"summary": "myMethod",
"operationId": "myMethodUsingGET",
"produces": [
"*/*"
],
"parameters": [
{
"name": "amount",
"in": "query",
"description": "amount",
"required": true,
"type": "integer",
"format": "int64"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/Mono«MyDto»"
}
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden"
},
"404": {
"description": "Not Found"
}
},
"deprecated": false
}
}
下面是spring的配置:
@Configuration
@EnableSwagger2WebFlux
public class SwaggerConfig {
@Bean
public Docket api() {
Class[] clazz = {AuthenticationPrincipal.class};
return new Docket(DocumentationType.SWAGGER_2)
.select()
.paths(PathSelectors.any())
.build()
.ignoredParameterTypes(clazz);
}
}
有没有办法为指定了@AuthenticationPrincipal
的每个路径在springfox项目生成的swagger文档中添加一个头文件?例如,忽略MyUser
类作为参数(或参数组),并将其替换为所有找到@AuthenticationPrincipal
的@RequestMapping
方法的头(如My-Auth-Header
)。
发布于 2019-01-25 13:40:16
这是一个如何使用名为Auithorization
的header
参数添加JWT令牌的示例
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.globalOperationParameters(
Collections.singletonList(new ParameterBuilder()
.name("Authorization")
.description("JWT Authorization token")
.modelRef(new ModelRef("string"))
.parameterType("header")
.required(false)
.build()));
}
}
https://stackoverflow.com/questions/54366267
复制相似问题