· 5 years ago · Jan 09, 2020, 08:42 PM
1@Configuration
2@EnableSwagger2
3public class SwaggerConfig {
4
5 @Value("${custom.my-secret-token}") String mySecretToken;
6 @Value("${spring.application.name}") String appName;
7 @Value("${custom.version}") String version;
8 @Value("${custom.base-url}") String baseUrl;
9 String web = "http://example/";
10 String email = "admin@carmelitanos.com";
11 String name = "Mercurio";
12 private final static String REST_PACKAGE = SwaggerConfig.class.getPackage().getName().substring(0, SwaggerConfig.class.getPackage().getName().length() - 6) + "web.rest";
13
14 @Bean
15 public Docket api() {
16// String restPackage = packageName + ".web.rest";
17 return new Docket(DocumentationType.SWAGGER_2)
18 .select()
19 .apis(
20 Predicates.or(
21// RequestHandlerSelectors.withClassAnnotation(RestController.class),
22// RequestHandlerSelectors.basePackage(restPackage),
23 RequestHandlerSelectors.basePackage(REST_PACKAGE)
24 )
25 )
26 .paths(PathSelectors.any())
27 .build()
28 .pathMapping("/")
29 .genericModelSubstitutes(ResponseEntity.class)
30 .useDefaultResponseMessages(false)
31 .securitySchemes(Arrays.asList(securitySchema()))
32 .securityContexts(Collections.singletonList(securityContext()))
33 .apiInfo(apiInfo())
34 .enableUrlTemplating(false);
35 }
36
37 private ApiInfo apiInfo() {
38 return new ApiInfoBuilder()
39 .title(appName)
40 .description("RESTful Api Documentation")
41 .contact(new Contact(name, web, email))
42 .version(version)
43 .build();
44 }
45
46 private OAuth securitySchema() {
47 List<AuthorizationScope> authorizationScopeList = new ArrayList<>();
48 authorizationScopeList.add(new AuthorizationScope("read", "read all"));
49 authorizationScopeList.add(new AuthorizationScope("write", "access all"));
50 List<GrantType> grantTypes = new ArrayList<>();
51 GrantType resourceOwnerPasswordCredentialsGrant = new ResourceOwnerPasswordCredentialsGrant("http://" + baseUrl + "/oauth/token");
52 grantTypes.add(resourceOwnerPasswordCredentialsGrant);
53 return new OAuth(appName, authorizationScopeList, grantTypes);
54 }
55
56 private SecurityContext securityContext() {
57 return SecurityContext.builder()
58 .securityReferences(defaultAuth())
59 .forPaths(PathSelectors.ant("/api/**"))
60 .build();
61 }
62
63 private List<SecurityReference> defaultAuth() {
64 final AuthorizationScope[] authorizationScopes = new AuthorizationScope[2];
65 authorizationScopes[0] = new AuthorizationScope("read", "read all");
66 authorizationScopes[1] = new AuthorizationScope("write", "write all");
67 return Collections.singletonList(new SecurityReference(appName, authorizationScopes));
68 }
69
70 @Bean
71 public SecurityConfiguration security() {
72 return SecurityConfigurationBuilder.builder()
73 .clientId(appName)
74 .clientSecret(mySecretToken)
75 .scopeSeparator(" ")
76 .additionalQueryStringParams(null)
77 .useBasicAuthenticationWithAccessCodeGrant(true)
78 .build();
79 }
80
81 @Bean
82 UiConfiguration uiConfig() {
83 return UiConfigurationBuilder.builder()
84 .deepLinking(true)
85 .displayOperationId(false) // getFacilitiesUsingGET_1
86 .defaultModelsExpandDepth(1)
87 .defaultModelExpandDepth(1)
88 .defaultModelRendering(ModelRendering.EXAMPLE)
89 .displayRequestDuration(true)
90 .docExpansion(DocExpansion.NONE)
91 .filter(true)
92 .maxDisplayedTags(null)
93 .operationsSorter(OperationsSorter.ALPHA)
94 .showExtensions(false)
95 .tagsSorter(TagsSorter.ALPHA)
96 .supportedSubmitMethods(UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS)
97 .validatorUrl(null)
98 .build();
99 }
100
101
102}