I have simple REST services implemented with Spring MVC. I decided to describe them with Springfox and Swagger 2.0. Everything seemed to be OK until I started adding security schemas and contexts. I use HTTP Basic authentication for certain endpoints and token-based authentication for others. Whatever I do, I cannot see any option to set HTTP Basic authentication credentials or to specify a token in Swagger UI. Below is my configuration. For simplicity's sake I apply both schemas to all endpoints here.
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket apiV1() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.pathMapping("/api/v1")
.securitySchemes(newArrayList(new BasicAuth("xBasic"),
new ApiKey("X-Auth-Token", "xAuthToken", "header")))
.securityContexts(newArrayList(xBasicSecurityContext(), xAuthTokenSecurityContext()))
}
private SecurityContext xBasicSecurityContext() {
SecurityContext.builder()
.securityReferences(newArrayList(new SecurityReference("xBasic",
new AuthorizationScope[0])))
.build()
}
private SecurityContext xAuthTokenSecurityContext() {
SecurityContext.builder()
.securityReferences(newArrayList(new SecurityReference("xAuthToken",
new AuthorizationScope[0])))
.build()
}