优化feign拦截器代码

This commit is contained in:
ajaxfan
2021-04-27 15:48:18 +08:00
parent 09d783a58d
commit 0074fe5061
2 changed files with 17 additions and 17 deletions

View File

@ -41,7 +41,7 @@ public class FeignConfig implements RequestInterceptor {
public void apply(RequestTemplate template) { public void apply(RequestTemplate template) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (null != attributes) { if (Objects.nonNull(attributes)) {
if (isNonExistsWhiteList(template.url())) { if (isNonExistsWhiteList(template.url())) {
injectToken(template, attributes); injectToken(template, attributes);
} }
@ -50,6 +50,7 @@ public class FeignConfig implements RequestInterceptor {
// 检查请求头是否包含 currentRoleCode // 检查请求头是否包含 currentRoleCode
if (StringUtils.isNotEmpty(currentRoleCode)) { if (StringUtils.isNotEmpty(currentRoleCode)) {
template.removeHeader(CURRENT_ROLE_CODE);
template.header(CURRENT_ROLE_CODE, currentRoleCode); template.header(CURRENT_ROLE_CODE, currentRoleCode);
} }
} }

View File

@ -1,26 +1,25 @@
package com.chinaunicom.mall.ebtp.core.config; package com.chinaunicom.mall.ebtp.core.config;
import com.chinaunicom.mall.ebtp.common.config.FeignConfig;
import feign.RequestTemplate;
import io.seata.core.context.RootContext;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import com.chinaunicom.mall.ebtp.common.config.FeignConfig;
import feign.RequestTemplate;
import io.seata.core.context.RootContext;
@Configuration @Configuration
public class FeignSeataConfig extends FeignConfig { public class FeignSeataConfig extends FeignConfig {
@Override @Override
public void apply(RequestTemplate template) { public void apply(RequestTemplate template) {
super.apply(template); super.apply(template);
String xid = RootContext.getXID();
if (StringUtils.isNotEmpty(xid)) {
template.header(RootContext.KEY_XID, xid);
}
}
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (null != attributes) {
String xid = RootContext.getXID();
if (StringUtils.isNotEmpty(xid)) {
template.header(RootContext.KEY_XID, xid);
}
}
}
} }