Swagger 커스터마이징

Swagger 에 Authentication Header 추가

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

@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.ant("/api/**"))
.build()
.securityContexts(Collections.singletonList(securityContext()))
.securitySchemes(Collections.singletonList(apiKey()));
}

private ApiKey apiKey() {
return new ApiKey("JWT", "x-auth-token", "header");
}

private SecurityContext securityContext() {
return springfox
.documentation
.spi.service
.contexts
.SecurityContext
.builder()
.securityReferences(defaultAuth()).forPaths(PathSelectors.any()).build();
}

List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
return Collections.singletonList(new SecurityReference("JWT", authorizationScopes));
}
}

Swagger Model Naming 정책 변경하기.

기본적으로 Swagger 는 리플렉션시 getSimpleName() 함수를 사용. 이렇게되면 Nested Class 가 만약 이름이 동일하다면 Swagger-ui 에서 모델을 구분하지 못한다. 이럴때 네이밍 정책을 바꿔준다.

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

@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.ant("/api/**"))
.build()
.securityContexts(Collections.singletonList(securityContext()))
.securitySchemes(Collections.singletonList(apiKey()));
}

private ApiKey apiKey() {
return new ApiKey("JWT", "x-auth-token", "header");
}

private SecurityContext securityContext() {
return springfox
.documentation
.spi.service
.contexts
.SecurityContext
.builder()
.securityReferences(defaultAuth()).forPaths(PathSelectors.any()).build();
}

List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
return Collections.singletonList(new SecurityReference("JWT", authorizationScopes));
}

@Component
@Order(SwaggerPluginSupport.SWAGGER_PLUGIN_ORDER)
public static class CustomTypeNameProvider implements TypeNameProviderPlugin {
@Override
public String nameFor(Class<?> type) {
String fullName = type.getName();
return fullName.substring(fullName.lastIndexOf(".") + 1);
}

@Override
public boolean supports(DocumentationType documentationType) {
return true;
}
}
}

Share