Please take the time to search the repository, if your question has already been asked or answered.
I tried 2.6.1 and 2.7.0-snapshot
What kind of issue is this?
[ ] Question. Is this a question about how to do a certain thing?
[x] Bug report. If you’ve found a bug, spend the time to write a failing test. Bugs with tests or
steps to reproduce get fixed faster. Here’s an example: https://gist.github.com/swankjesse/6608b4713ad80988cdc9
I build a simple spring-boot application and use springfox-swagger2 and springfox-swagger2-ui, everything works except securityDefinitions not generate in /v2/api-docs, I have tried with the following code snippets
@SwaggerDefinition(securityDefinition = @SecurityDefinition(
apiKeyAuthDefintions = {
@ApiKeyAuthDefinition(key = "token", name = "token", in = ApiKeyLocation.QUERY)
}
)
)
@RestController
@RequestMapping("/api/admin/")
public class AdminUserController {
@Autowired
private AdminUserService adminUserService;
@ApiOperation(value = "List all admin", notes = "List all admin in json response", authorizations={@Authorization(value = "token")})
@RequestMapping(value = "/", method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<List<AdminUser>> get()
......
or
@SwaggerDefinition(securityDefinition = @SecurityDefinition(
apiKeyAuthDefintions = {
@ApiKeyAuthDefinition(key = "token", name = "token", in = ApiKeyLocation.QUERY)
}
)
)
public interface SwaggerSecurityDefinition {
}
@RestController
@RequestMapping("/api/admin/")
public class AdminUserController implements SwaggerSecurityDefinition {
@Autowired
private AdminUserService adminUserService;
@ApiOperation(value = "List all admin", notes = "List all admin in json response", authorizations={@Authorization(value = "token")})
@RequestMapping(value = "/", method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<List<AdminUser>> get()
......
The missing securityDefinitions is
"securityDefinitions": {
"token": {
"type": "apiKey",
"name": "token",
"in": "query"
}
}
Now I can use the following code to fix this.
see also https://github.com/springfox/springfox/issues/1569
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.useDefaultResponseMessages(false)
.select()
.apis(RequestHandlerSelectors.any())
.paths(Predicates.not(PathSelectors.regex("/error.*")))
.build()
.apiInfo(apiInfo())
.protocols(protocols())
.securitySchemes(securitySchemes())
.securityContexts(securityContexts());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("RedPacketServer-API Swagger Documentation")
.description("RedPacketServer-API Swagger Documentation")
.termsOfServiceUrl("http://liudonghua.com")
.contact(new Contact("liudonghua", "liudonghua.com", "[email protected]"))
.license("Apache License Version 2.0")
.licenseUrl("https://www.apache.org/licenses/LICENSE-2.0")
.version("2.0")
.build();
}
private Set<String> protocols() {
Set<String> protocols = new HashSet<>();
protocols.add("http");
return protocols;
}
private List<? extends SecurityScheme> securitySchemes() {
List<SecurityScheme> authorizationTypes = Arrays.asList(new ApiKey("token", "token", "header"));
return authorizationTypes;
}
private List<SecurityContext> securityContexts() {
List<SecurityContext> securityContexts = Arrays.asList(SecurityContext.builder().forPaths(PathSelectors.any()).securityReferences(securityReferences()).build());
return securityContexts;
}
private List<SecurityReference> securityReferences() {
List<SecurityReference> securityReferences = Arrays.asList(SecurityReference.builder().reference("token").scopes(new AuthorizationScope[0]).build());
return securityReferences;
}
}
在SwaggerConfig.java里åŠ
private ApiKey apiKey() {
return new ApiKey("mykey", "key", "header");
}
就有了securityDefinitions 了
Thank you @helxiong and @liudonghua123
Most helpful comment
Now I can use the following code to fix this.
see also https://github.com/springfox/springfox/issues/1569