|
|
@ -13,6 +13,7 @@
|
|
|
|
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
* 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
|
|
|
|
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations under the License.
|
|
|
|
* specific language governing permissions and limitations under the License.
|
|
|
|
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
package com.tencent.cloud.metadata.config;
|
|
|
|
package com.tencent.cloud.metadata.config;
|
|
|
@ -21,14 +22,19 @@ import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
import com.netflix.zuul.ZuulFilter;
|
|
|
|
import com.netflix.zuul.ZuulFilter;
|
|
|
|
import com.tencent.cloud.metadata.core.filter.gateway.Metadata2HeaderScgFilter;
|
|
|
|
import com.tencent.cloud.common.constant.MetadataConstant;
|
|
|
|
import com.tencent.cloud.metadata.core.filter.gateway.Metadata2HeaderZuulFilter;
|
|
|
|
import com.tencent.cloud.metadata.core.DecodeTransferMetadataReactiveFilter;
|
|
|
|
import com.tencent.cloud.metadata.core.interceptor.Metadata2HeaderFeignInterceptor;
|
|
|
|
import com.tencent.cloud.metadata.core.DecodeTransferMetadataServletFilter;
|
|
|
|
import com.tencent.cloud.metadata.core.interceptor.Metadata2HeaderRestTemplateInterceptor;
|
|
|
|
import com.tencent.cloud.metadata.core.EncodeTransferMedataFeignInterceptor;
|
|
|
|
|
|
|
|
import com.tencent.cloud.metadata.core.EncodeTransferMedataRestTemplateInterceptor;
|
|
|
|
|
|
|
|
import com.tencent.cloud.metadata.core.EncodeTransferMedataScgFilter;
|
|
|
|
|
|
|
|
import com.tencent.cloud.metadata.core.EncodeTransferMetadataZuulFilter;
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.BeansException;
|
|
|
|
import org.springframework.beans.BeansException;
|
|
|
|
import org.springframework.beans.factory.config.BeanPostProcessor;
|
|
|
|
import org.springframework.beans.factory.config.BeanPostProcessor;
|
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
|
|
|
|
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
|
|
|
|
|
|
|
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
|
|
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
|
|
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
|
import org.springframework.context.ApplicationContextAware;
|
|
|
|
import org.springframework.context.ApplicationContextAware;
|
|
|
@ -38,6 +44,12 @@ import org.springframework.http.client.ClientHttpRequestInterceptor;
|
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static javax.servlet.DispatcherType.ASYNC;
|
|
|
|
|
|
|
|
import static javax.servlet.DispatcherType.ERROR;
|
|
|
|
|
|
|
|
import static javax.servlet.DispatcherType.FORWARD;
|
|
|
|
|
|
|
|
import static javax.servlet.DispatcherType.INCLUDE;
|
|
|
|
|
|
|
|
import static javax.servlet.DispatcherType.REQUEST;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Metadata transfer auto configuration.
|
|
|
|
* Metadata transfer auto configuration.
|
|
|
|
*
|
|
|
|
*
|
|
|
@ -46,6 +58,46 @@ import org.springframework.web.client.RestTemplate;
|
|
|
|
@Configuration
|
|
|
|
@Configuration
|
|
|
|
public class MetadataTransferAutoConfiguration {
|
|
|
|
public class MetadataTransferAutoConfiguration {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Create when web application type is SERVLET.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Configuration
|
|
|
|
|
|
|
|
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
|
|
|
|
|
|
|
|
static class MetadataServletFilterConfig {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
|
|
|
public FilterRegistrationBean<DecodeTransferMetadataServletFilter> metadataServletFilterRegistrationBean(
|
|
|
|
|
|
|
|
DecodeTransferMetadataServletFilter decodeTransferMetadataServletFilter) {
|
|
|
|
|
|
|
|
FilterRegistrationBean<DecodeTransferMetadataServletFilter> filterRegistrationBean = new FilterRegistrationBean<>(
|
|
|
|
|
|
|
|
decodeTransferMetadataServletFilter);
|
|
|
|
|
|
|
|
filterRegistrationBean.setDispatcherTypes(ASYNC, ERROR, FORWARD, INCLUDE,
|
|
|
|
|
|
|
|
REQUEST);
|
|
|
|
|
|
|
|
filterRegistrationBean
|
|
|
|
|
|
|
|
.setOrder(MetadataConstant.OrderConstant.WEB_FILTER_ORDER);
|
|
|
|
|
|
|
|
return filterRegistrationBean;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
|
|
|
public DecodeTransferMetadataServletFilter metadataServletFilter() {
|
|
|
|
|
|
|
|
return new DecodeTransferMetadataServletFilter();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Create when web application type is REACTIVE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Configuration
|
|
|
|
|
|
|
|
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
|
|
|
|
|
|
|
|
static class MetadataReactiveFilterConfig {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
|
|
|
public DecodeTransferMetadataReactiveFilter metadataReactiveFilter() {
|
|
|
|
|
|
|
|
return new DecodeTransferMetadataReactiveFilter();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Create when gateway application is Zuul.
|
|
|
|
* Create when gateway application is Zuul.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
@ -54,8 +106,8 @@ public class MetadataTransferAutoConfiguration {
|
|
|
|
static class MetadataTransferZuulFilterConfig {
|
|
|
|
static class MetadataTransferZuulFilterConfig {
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
|
public ZuulFilter metadata2HeaderZuulFilter() {
|
|
|
|
public ZuulFilter encodeTransferMetadataZuulFilter() {
|
|
|
|
return new Metadata2HeaderZuulFilter();
|
|
|
|
return new EncodeTransferMetadataZuulFilter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -68,8 +120,8 @@ public class MetadataTransferAutoConfiguration {
|
|
|
|
static class MetadataTransferScgFilterConfig {
|
|
|
|
static class MetadataTransferScgFilterConfig {
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
|
public GlobalFilter metadata2HeaderScgFilter() {
|
|
|
|
public GlobalFilter encodeTransferMedataScgFilter() {
|
|
|
|
return new Metadata2HeaderScgFilter();
|
|
|
|
return new EncodeTransferMedataScgFilter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -82,8 +134,8 @@ public class MetadataTransferAutoConfiguration {
|
|
|
|
static class MetadataTransferFeignInterceptorConfig {
|
|
|
|
static class MetadataTransferFeignInterceptorConfig {
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
|
public Metadata2HeaderFeignInterceptor metadata2HeaderFeignInterceptor() {
|
|
|
|
public EncodeTransferMedataFeignInterceptor encodeTransferMedataFeignInterceptor() {
|
|
|
|
return new Metadata2HeaderFeignInterceptor();
|
|
|
|
return new EncodeTransferMedataFeignInterceptor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -98,13 +150,13 @@ public class MetadataTransferAutoConfiguration {
|
|
|
|
private ApplicationContext context;
|
|
|
|
private ApplicationContext context;
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
|
public Metadata2HeaderRestTemplateInterceptor metadata2HeaderRestTemplateInterceptor() {
|
|
|
|
public EncodeTransferMedataRestTemplateInterceptor encodeTransferMedataRestTemplateInterceptor() {
|
|
|
|
return new Metadata2HeaderRestTemplateInterceptor();
|
|
|
|
return new EncodeTransferMedataRestTemplateInterceptor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
|
BeanPostProcessor metadata2HeaderRestTemplatePostProcessor(
|
|
|
|
BeanPostProcessor encodeTransferMetadataRestTemplatePostProcessor(
|
|
|
|
Metadata2HeaderRestTemplateInterceptor metadata2HeaderRestTemplateInterceptor) {
|
|
|
|
EncodeTransferMedataRestTemplateInterceptor encodeTransferMedataRestTemplateInterceptor) {
|
|
|
|
// Coping with multiple bean injection scenarios
|
|
|
|
// Coping with multiple bean injection scenarios
|
|
|
|
Map<String, RestTemplate> beans = this.context
|
|
|
|
Map<String, RestTemplate> beans = this.context
|
|
|
|
.getBeansOfType(RestTemplate.class);
|
|
|
|
.getBeansOfType(RestTemplate.class);
|
|
|
@ -117,14 +169,14 @@ public class MetadataTransferAutoConfiguration {
|
|
|
|
.getInterceptors();
|
|
|
|
.getInterceptors();
|
|
|
|
// Avoid setting interceptor repeatedly.
|
|
|
|
// Avoid setting interceptor repeatedly.
|
|
|
|
if (null != interceptors && !interceptors
|
|
|
|
if (null != interceptors && !interceptors
|
|
|
|
.contains(metadata2HeaderRestTemplateInterceptor)) {
|
|
|
|
.contains(encodeTransferMedataRestTemplateInterceptor)) {
|
|
|
|
interceptors.add(metadata2HeaderRestTemplateInterceptor);
|
|
|
|
interceptors.add(encodeTransferMedataRestTemplateInterceptor);
|
|
|
|
restTemplate.setInterceptors(interceptors);
|
|
|
|
restTemplate.setInterceptors(interceptors);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new Metadata2HeaderRestTemplatePostProcessor(
|
|
|
|
return new EncodeTransferMetadataRestTemplatePostProcessor(
|
|
|
|
metadata2HeaderRestTemplateInterceptor);
|
|
|
|
encodeTransferMedataRestTemplateInterceptor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -133,14 +185,14 @@ public class MetadataTransferAutoConfiguration {
|
|
|
|
this.context = applicationContext;
|
|
|
|
this.context = applicationContext;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static class Metadata2HeaderRestTemplatePostProcessor
|
|
|
|
public static class EncodeTransferMetadataRestTemplatePostProcessor
|
|
|
|
implements BeanPostProcessor {
|
|
|
|
implements BeanPostProcessor {
|
|
|
|
|
|
|
|
|
|
|
|
private Metadata2HeaderRestTemplateInterceptor metadata2HeaderRestTemplateInterceptor;
|
|
|
|
private EncodeTransferMedataRestTemplateInterceptor encodeTransferMedataRestTemplateInterceptor;
|
|
|
|
|
|
|
|
|
|
|
|
Metadata2HeaderRestTemplatePostProcessor(
|
|
|
|
EncodeTransferMetadataRestTemplatePostProcessor(
|
|
|
|
Metadata2HeaderRestTemplateInterceptor metadata2HeaderRestTemplateInterceptor) {
|
|
|
|
EncodeTransferMedataRestTemplateInterceptor encodeTransferMedataRestTemplateInterceptor) {
|
|
|
|
this.metadata2HeaderRestTemplateInterceptor = metadata2HeaderRestTemplateInterceptor;
|
|
|
|
this.encodeTransferMedataRestTemplateInterceptor = encodeTransferMedataRestTemplateInterceptor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -156,8 +208,8 @@ public class MetadataTransferAutoConfiguration {
|
|
|
|
.getInterceptors();
|
|
|
|
.getInterceptors();
|
|
|
|
// Avoid setting interceptor repeatedly.
|
|
|
|
// Avoid setting interceptor repeatedly.
|
|
|
|
if (null != interceptors && !interceptors
|
|
|
|
if (null != interceptors && !interceptors
|
|
|
|
.contains(metadata2HeaderRestTemplateInterceptor)) {
|
|
|
|
.contains(encodeTransferMedataRestTemplateInterceptor)) {
|
|
|
|
interceptors.add(this.metadata2HeaderRestTemplateInterceptor);
|
|
|
|
interceptors.add(this.encodeTransferMedataRestTemplateInterceptor);
|
|
|
|
restTemplate.setInterceptors(interceptors);
|
|
|
|
restTemplate.setInterceptors(interceptors);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|