过滤器替换域名
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package com.accompany.business.interceptor;
|
||||
|
||||
import com.accompany.common.constant.AppEnum;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.util.ContentCachingResponseWrapper;
|
||||
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
public class CustomResponseFilter implements Filter {
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
||||
throws IOException, ServletException {
|
||||
ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper((HttpServletResponse) response);
|
||||
chain.doFilter(request, responseWrapper);
|
||||
|
||||
byte[] responseArray = responseWrapper.getContentAsByteArray();
|
||||
String responseStr = new String(responseArray, responseWrapper.getCharacterEncoding());
|
||||
|
||||
// 在这里处理响应内容
|
||||
String modifiedResponse = modifyResponse(responseStr);
|
||||
|
||||
response.getOutputStream().write(modifiedResponse.getBytes());
|
||||
}
|
||||
|
||||
private String modifyResponse(String original) {
|
||||
AppEnum curApp = AppEnum.getCurApp();
|
||||
String apiDomain = curApp.getApiDomain();
|
||||
String resourceDomain = curApp.getResourceDomain();
|
||||
|
||||
List<AppEnum> otherTwoAppEnums = AppEnum.getOtherAppEnums(curApp);
|
||||
for (AppEnum otherApp : otherTwoAppEnums) {
|
||||
String diffResourceDomain = otherApp.getResourceDomain();
|
||||
String diffApiDomain = otherApp.getApiDomain();
|
||||
if (original.contains(diffResourceDomain)) {
|
||||
original = original.replaceAll(diffResourceDomain, resourceDomain);
|
||||
}
|
||||
if (original.contains(diffApiDomain)) {
|
||||
original = original.replaceAll(diffApiDomain, apiDomain);
|
||||
}
|
||||
}
|
||||
return original;
|
||||
}
|
||||
}
|
@@ -1,110 +0,0 @@
|
||||
package com.accompany.business.interceptor;
|
||||
|
||||
import com.accompany.common.constant.AppEnum;
|
||||
import com.accompany.common.result.BusiResult;
|
||||
import com.accompany.common.utils.EnvComponent;
|
||||
import com.accompany.core.vo.BaseResponseVO;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.aop.framework.Advised;
|
||||
import org.springframework.aop.support.AopUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.server.ServerHttpRequest;
|
||||
import org.springframework.http.server.ServerHttpResponse;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@RestControllerAdvice
|
||||
@Slf4j
|
||||
public class ResponseDataHandler implements ResponseBodyAdvice {
|
||||
|
||||
@Autowired
|
||||
private EnvComponent envComponent;
|
||||
|
||||
@Override
|
||||
public boolean supports(MethodParameter methodParameter, Class aClass) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object beforeBodyWrite(Object body, MethodParameter methodParameter, MediaType mediaType, Class selectedConverterType,
|
||||
ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
|
||||
if (envComponent.getDevOrNativeEnv() || body == null) {
|
||||
return body;
|
||||
}
|
||||
try {
|
||||
// 2. 安全获取真实类(处理代理和null)
|
||||
Class<?> targetClass = getTargetClass(body);
|
||||
|
||||
String uri = serverHttpRequest.getURI().getPath();
|
||||
if (BusiResult.class.isAssignableFrom(targetClass)) {
|
||||
BusiResult result = (BusiResult) body;
|
||||
Object data = result.getData();
|
||||
if (null != data) {
|
||||
String json = JSONObject.toJSONString(data);
|
||||
json = handlerDomain(json);
|
||||
|
||||
log.info("app:{}, uri:{} ResponseDataHandler 实际返回内容:{}" , AppEnum.getCurApp().getValue(), uri, json);
|
||||
|
||||
result.setData(JSONObject.parseObject(json, Object.class));
|
||||
return result;
|
||||
}
|
||||
} else if (BaseResponseVO.class.isAssignableFrom(targetClass)) {
|
||||
BaseResponseVO result = (BaseResponseVO) body;
|
||||
Object data = result.getData();
|
||||
if (null != data) {
|
||||
String json = JSONObject.toJSONString(data);
|
||||
json = handlerDomain(json);
|
||||
log.info("app:{}, uri:{} ResponseDataHandler 实际返回内容:{}" , AppEnum.getCurApp().getValue(), uri, json);
|
||||
result.setData(JSONObject.parseObject(json, Object.class));
|
||||
return result;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("ResponseDataHandler error, e:{}" , e.getMessage(), e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return body;
|
||||
}
|
||||
|
||||
private String handlerDomain(String json) {
|
||||
AppEnum curApp = AppEnum.getCurApp();
|
||||
String apiDomain = curApp.getApiDomain();
|
||||
String resourceDomain = curApp.getResourceDomain();
|
||||
|
||||
List<AppEnum> otherTwoAppEnums = AppEnum.getOtherAppEnums(curApp);
|
||||
for (AppEnum otherApp : otherTwoAppEnums) {
|
||||
String diffResourceDomain = otherApp.getResourceDomain();
|
||||
String diffApiDomain = otherApp.getApiDomain();
|
||||
if (json.contains(diffResourceDomain)) {
|
||||
json = json.replaceAll(diffResourceDomain, resourceDomain);
|
||||
}
|
||||
if (json.contains(diffApiDomain)) {
|
||||
json = json.replaceAll(diffApiDomain, apiDomain);
|
||||
}
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
|
||||
// 辅助方法:获取真实类(处理代理)
|
||||
private Class<?> getTargetClass(Object obj) {
|
||||
if (obj == null) {
|
||||
return null;
|
||||
}
|
||||
if (AopUtils.isAopProxy(obj)) { // Spring AOP 代理
|
||||
return AopUtils.getTargetClass(obj);
|
||||
}
|
||||
if (obj instanceof Advised) { // JDK 动态代理
|
||||
return ((Advised) obj).getTargetSource().getTargetClass();
|
||||
}
|
||||
return obj.getClass();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user