-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e7c55a
commit caa9680
Showing
12 changed files
with
253 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...contract/src/main/java/com/tencent/cloud/polaris/contract/filter/ApiDocServletFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.tencent.cloud.polaris.contract.filter; | ||
|
||
import java.io.IOException; | ||
|
||
import com.tencent.cloud.polaris.contract.config.PolarisContractProperties; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.annotation.WebFilter; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
import org.springframework.lang.NonNull; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import static com.tencent.cloud.polaris.contract.filter.FilterConstant.SWAGGER_RESOURCE_PREFIX; | ||
import static com.tencent.cloud.polaris.contract.filter.FilterConstant.SWAGGER_V2_API_DOC_URL; | ||
import static com.tencent.cloud.polaris.contract.filter.FilterConstant.SWAGGER_V3_API_DOC_URL; | ||
import static com.tencent.cloud.polaris.contract.filter.FilterConstant.SWAGGER_WEBJARS_V2_PREFIX; | ||
import static com.tencent.cloud.polaris.contract.filter.FilterConstant.SWAGGER_WEBJARS_V3_PREFIX; | ||
import static org.springdoc.core.utils.Constants.SWAGGER_UI_URL; | ||
|
||
/** | ||
* Filter to disable api doc controller. | ||
* | ||
* @author Haotian Zhang | ||
*/ | ||
@WebFilter | ||
public class ApiDocServletFilter extends OncePerRequestFilter { | ||
|
||
private final PolarisContractProperties polarisContractProperties; | ||
|
||
public ApiDocServletFilter(PolarisContractProperties polarisContractProperties) { | ||
this.polarisContractProperties = polarisContractProperties; | ||
} | ||
|
||
@Override | ||
public void doFilterInternal(@NonNull HttpServletRequest httpServletRequest, | ||
@NonNull HttpServletResponse httpServletResponse, @NonNull FilterChain filterChain) | ||
throws ServletException, IOException { | ||
if (!polarisContractProperties.isExposure()) { | ||
String path = httpServletRequest.getServletPath(); | ||
if (path.equals(SWAGGER_V2_API_DOC_URL) || | ||
path.startsWith(SWAGGER_V3_API_DOC_URL) || | ||
path.equals(SWAGGER_UI_URL) || | ||
path.startsWith(SWAGGER_RESOURCE_PREFIX) || | ||
path.startsWith(SWAGGER_WEBJARS_V2_PREFIX) || | ||
path.startsWith(SWAGGER_WEBJARS_V3_PREFIX)) { | ||
httpServletResponse.setStatus(HttpServletResponse.SC_FORBIDDEN); | ||
return; | ||
} | ||
} | ||
filterChain.doFilter(httpServletRequest, httpServletResponse); | ||
} | ||
} | ||
|
53 changes: 53 additions & 0 deletions
53
...contract/src/main/java/com/tencent/cloud/polaris/contract/filter/ApiDocWebFluxFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.tencent.cloud.polaris.contract.filter; | ||
|
||
import com.tencent.cloud.polaris.contract.config.PolarisContractProperties; | ||
import reactor.core.publisher.Mono; | ||
|
||
import org.springframework.core.io.buffer.DataBuffer; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.server.reactive.ServerHttpResponse; | ||
import org.springframework.lang.NonNull; | ||
import org.springframework.web.server.ServerWebExchange; | ||
import org.springframework.web.server.WebFilter; | ||
import org.springframework.web.server.WebFilterChain; | ||
|
||
import static com.tencent.cloud.polaris.contract.filter.FilterConstant.SWAGGER_RESOURCE_PREFIX; | ||
import static com.tencent.cloud.polaris.contract.filter.FilterConstant.SWAGGER_UI_URL; | ||
import static com.tencent.cloud.polaris.contract.filter.FilterConstant.SWAGGER_V2_API_DOC_URL; | ||
import static com.tencent.cloud.polaris.contract.filter.FilterConstant.SWAGGER_V3_API_DOC_URL; | ||
import static com.tencent.cloud.polaris.contract.filter.FilterConstant.SWAGGER_WEBJARS_V2_PREFIX; | ||
import static com.tencent.cloud.polaris.contract.filter.FilterConstant.SWAGGER_WEBJARS_V3_PREFIX; | ||
|
||
/** | ||
* Filter to disable api doc controller. | ||
* | ||
* @author Haotian Zhang | ||
*/ | ||
public class ApiDocWebFluxFilter implements WebFilter { | ||
|
||
private final PolarisContractProperties polarisContractProperties; | ||
|
||
public ApiDocWebFluxFilter(PolarisContractProperties polarisContractProperties) { | ||
this.polarisContractProperties = polarisContractProperties; | ||
} | ||
|
||
@Override | ||
public Mono<Void> filter(ServerWebExchange serverWebExchange, @NonNull WebFilterChain webFilterChain) { | ||
if (!polarisContractProperties.isExposure()) { | ||
String path = serverWebExchange.getRequest().getURI().getPath(); | ||
if (path.equals(SWAGGER_V2_API_DOC_URL) || | ||
path.startsWith(SWAGGER_V3_API_DOC_URL) || | ||
path.equals(SWAGGER_UI_URL) || | ||
path.startsWith(SWAGGER_RESOURCE_PREFIX) || | ||
path.startsWith(SWAGGER_WEBJARS_V2_PREFIX) || | ||
path.startsWith(SWAGGER_WEBJARS_V3_PREFIX)) { | ||
ServerHttpResponse response = serverWebExchange.getResponse(); | ||
response.setRawStatusCode(HttpStatus.FORBIDDEN.value()); | ||
DataBuffer dataBuffer = response.bufferFactory().allocateBuffer(); | ||
return response.writeWith(Mono.just(dataBuffer)); | ||
} | ||
} | ||
return webFilterChain.filter(serverWebExchange); | ||
} | ||
} | ||
|
59 changes: 59 additions & 0 deletions
59
...aris-contract/src/main/java/com/tencent/cloud/polaris/contract/filter/FilterConstant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available. | ||
* | ||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. | ||
* | ||
* Licensed under the BSD 3-Clause License (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://opensource.org/licenses/BSD-3-Clause | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed | ||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | ||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package com.tencent.cloud.polaris.contract.filter; | ||
|
||
/** | ||
* Constant for filter. | ||
* | ||
* @author Haotian Zhang | ||
*/ | ||
public final class FilterConstant { | ||
|
||
/** | ||
* Swagger api doc V2 url. | ||
*/ | ||
public static final String SWAGGER_V2_API_DOC_URL = "/v2/api-docs"; | ||
|
||
/** | ||
* Swagger api doc V3 url. | ||
*/ | ||
public static final String SWAGGER_V3_API_DOC_URL = "/v3/api-docs"; | ||
|
||
/** | ||
* Swagger UI url. | ||
*/ | ||
public static final String SWAGGER_UI_URL = "/swagger-ui.html"; | ||
|
||
/** | ||
* Swagger resource url prefix. | ||
*/ | ||
public static final String SWAGGER_RESOURCE_PREFIX = "/swagger-resource/"; | ||
|
||
/** | ||
* Swagger webjars V2 url prefix. | ||
*/ | ||
public static final String SWAGGER_WEBJARS_V2_PREFIX = "/webjars/springfox-swagger-ui/"; | ||
|
||
/** | ||
* Swagger webjars V3 url prefix. | ||
*/ | ||
public static final String SWAGGER_WEBJARS_V3_PREFIX = "/webjars/swagger-ui/"; | ||
|
||
private FilterConstant() { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...olaris-contract/src/main/resources/META-INF/additional-spring-configuration-metadata.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"properties": [ | ||
{ | ||
"name": "spring.cloud.polaris.contract.enabled", | ||
"type": "java.lang.Boolean", | ||
"defaultValue": true, | ||
"description": "Enable polaris record contract or not." | ||
}, | ||
{ | ||
"name": "spring.cloud.polaris.contract.basePackage", | ||
"type": "java.lang.String", | ||
"defaultValue": "", | ||
"description": "Packages to be scanned. Split by \",\"." | ||
}, | ||
{ | ||
"name": "spring.cloud.polaris.contract.excludePath", | ||
"type": "java.lang.String", | ||
"defaultValue": "", | ||
"description": "Paths to be excluded. Split by \",\"." | ||
}, | ||
{ | ||
"name": "spring.cloud.polaris.contract.group", | ||
"type": "java.lang.String", | ||
"defaultValue": "default", | ||
"description": "Group to create swagger docket." | ||
}, | ||
{ | ||
"name": "spring.cloud.polaris.contract.basePath", | ||
"type": "java.lang.String", | ||
"defaultValue": "/**", | ||
"description": "Base paths to be scanned. Split by \",\"." | ||
}, | ||
{ | ||
"name": "spring.cloud.polaris.contract.exposure", | ||
"type": "java.lang.Boolean", | ||
"defaultValue": "true", | ||
"description": "Enable polaris contract exposure or not." | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters