-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathOpenApiConfiguration.java
82 lines (73 loc) · 2.79 KB
/
OpenApiConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package gov.va.vro.config;
import gov.va.vro.config.propmodel.OpenApiProperties;
import gov.va.vro.openapi.spi.CustomSecuritySchemeProvider;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.servers.Server;
import io.swagger.v3.oas.models.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@OpenAPIDefinition
@Configuration
public class OpenApiConfiguration {
@Autowired(required = false)
private List<CustomSecuritySchemeProvider> securitySchemeProviders;
@Autowired private final OpenApiProperties openApiProperties = new OpenApiProperties();
/**
* Configure OpenAPI processor.
*
* @return OpenAPI configuration bean
*/
@Bean
public OpenAPI customOpenApi() {
List<Server> servers =
openApiProperties.getServers().stream()
.map(server -> new Server().description(server.getDescription()).url(server.getUrl()))
.collect(Collectors.toList());
// Adding to this list will have it appear under `tags` at http://localhost:8080/v3/api-docs
// Not sure why this list isn't automatically populated when @Tag is used.
final List<Tag> tags = Arrays.asList(new Tag().name("Xample Domain"));
OpenAPI config =
new OpenAPI()
.info(
new io.swagger.v3.oas.models.info.Info()
.title("VRO App")
.description("VRO Java-based application"))
.servers(servers)
.addSecurityItem(
new SecurityRequirement()
.addList("bearer-jwt", Arrays.asList("read", "write"))
.addList("oauth2", Arrays.asList("read", "write")))
.tags(tags);
config = configureSecuritySchemes(config);
return config;
}
/**
* Configure OpenAPI Security schemes.
*
* @param config current OpenAPI config object
* @return OpenAPI config object
*/
protected OpenAPI configureSecuritySchemes(OpenAPI config) {
if (null != securitySchemeProviders && securitySchemeProviders.size() > 0) {
Components securitySchemes = new Components();
securitySchemeProviders.forEach(
p -> {
log.info("Adding SecurityScheme [{}]", p.getName());
securitySchemes.addSecuritySchemes(p.getName(), p.create());
});
config.components(securitySchemes);
} else {
log.warn("No SecuritySchemeProviders defined.");
}
return config;
}
}