Springboot应用中过滤器如何修改response的header和body内容

  package one.util;

  import org.apache.catalina.connector.Request;

  import org.apache.catalina.connector.RequestFacade;

  import org.apache.tomcat.util.http.MimeHeaders;

  import org.springframework.util.ObjectUtils;

  import javax.servlet.ServletRequestWrapper;

  import javax.servlet.http.HttpServletRequest;

  import java.lang.reflect.Field;

  import java.util.HashMap;

  import java.util.Map;

  public class HeadersEdit {

  private final HttpServletRequest request;

  private Map modifyHeaders;

  private MimeHeaders itsHeaders;

  private boolean isDeleteKnown=true;

  public HeadersEdit(HttpServletRequest request){

  this.request=request;

  modifyHeaders= new HashMap<>();

  itsHeaders=getMimeHeaders(getRequestFacade());

  }

  private HttpServletRequest getRequestFacade(){

  try{

  Field field= ServletRequestWrapper.class.getDeclaredField("request");

  HttpServletRequest result;

  field.setAccessible(true);

  result=(HttpServletRequest)field.get((request));

  while(!(result instanceof RequestFacade)){

  result=(HttpServletRequest)field.get((result));

  }

  return result;

  }catch (Exception e){

  e.printStackTrace();

  throw new RuntimeException("HeadersEdit Error!");

  }

  }

  private MimeHeaders getMimeHeaders(HttpServletRequest requestFacade){

  try{

  Field itsRequest= RequestFacade.class.getDeclaredField("request");

  itsRequest.setAccessible(true);

  HttpServletRequest o5=(HttpServletRequest) itsRequest.get(requestFacade);

  Field coyoteField= Request.class.getDeclaredField("coyoteRequest");

  coyoteField.setAccessible(true);

  org.apache.coyote.Request coyoteRequest=(org.apache.coyote.Request) coyoteField.get(o5);

  Field headers=org.apache.coyote.Request.class.getDeclaredField("headers");

  headers.setAccessible(true);

  return (MimeHeaders)headers.get(coyoteRequest);

  }catch (Exception e){

  e.printStackTrace();

  throw new RuntimeException("HeadersEdit Error!");

  }

  }

  private boolean setValueExecutor(MapnewHeaders){

  String key;

  for(Map.Entry entry : newHeaders.entrySet()){

  key=entry.getKey();

  if(itsHeaders.getHeader(key)!=null){

  if(isDeleteKnown){

  itsHeaders.removeHeader(key);

  }else{

  return false;

  }

  }

  itsHeaders.addValue(key).setString(entry.getValue());

  }

  return true;

  }

  public boolean setValue(String key,String value){

  if(ObjectUtils.isEmpty(key)||ObjectUtils.isEmpty(value)){

  return false;

  }

  Mapmap=new HashMap<>();

  map.put(key,value);

  return setValueExecutor(map);

  }

  public boolean setValue(MapnewHeaders){

  if(ObjectUtils.isEmpty(newHeaders)){

  return false;

  }

  return setValueExecutor(newHeaders);

  }

  public void changeCheck(){

  isDeleteKnown=!isDeleteKnown;

  }

  }