在swagger-ui中,我想在请求头中添加授权令牌。
我几乎看遍了这里和其他网站的所有帖子。仍然不能使它像预期的那样工作。我尝试了不同的解决方案,但没有任何改变。
我现在的实现是根据 今春号:
@ApiOperation(
value = "Yeni Üye Tanımlama Servisi",
notes = "Platformlar tarafından iletilen üye bilgilerinin kaydedilmesini sağlayan servistir.",
response = KfsResponse.class
, authorizations = { @Authorization(value="Authorization") })
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(Predicates.or(RequestHandlerSelectors.basePackage("tr.com.mkk.kfs.kfs.web"), RequestHandlerSelectors.basePackage("tr.com.mkk.kfs.kfs.model.infos")))
.paths(PathSelectors.any()).build()
.apiInfo(apiEndPointsInfo())
.securitySchemes(Arrays.asList(apiKey()))
.securityContexts(Arrays.asList(securityContext));
}
private ApiKey apiKey() {
return new ApiKey("Authorization", "Authorization", "header");
}
SecurityReference securityReference = SecurityReference.builder()
.reference("Authorization")
.scopes(new AuthorizationScope[0])
.build();
SecurityContext securityContext = SecurityContext.builder()
.securityReferences(Arrays.asList(securityReference))
.build();
<springfox.version>2.9.2</springfox.version>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-bean-validators</artifactId>
<version>${springfox.version}</version>
</dependency>
https:/github.comspringfoxspringfoxissues2661。,https:/github.comspringfoxspringfoxissues2194。,https:/stackoverflow.coma587200777831244。,https:/stackoverflow.coma528688537831244。.
你可以设置 Authorization
头部至 docket
使用 ParameterBuilder
如下图所示
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Value("${title:title}")
private String title;
@Value("${description:description")
private String description;
@Value("${version:0.0.1}")
private String version;
ApiInfo apiInfo() {
return new ApiInfoBuilder().title(title).description(description).version(version).build();
}
@Bean
public Docket api() {
Docket docket;
docket = new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors.basePackage("com.controller")).paths(PathSelectors.any()).build()
.apiInfo(apiInfo());
docket.globalOperationParameters(
Arrays.asList(
new ParameterBuilder().name("Authorization")
.description("Authorization details for security (JWT token or BasicAuth)")
.modelRef(new ModelRef("String")).parameterType("header").required(false).build()));
return docket;
}
}