gpt4 book ai didi

spring - Servlet 过滤器修改 header 值,而 servlet 请求包装器不起作用

转载 作者:行者123 更新时间:2023-12-04 14:29:19 25 4
gpt4 key购买 nike

我正在尝试更改请求中的 Content-Type header ,并在它到达我的 Spring 休息 Controller 之前将其更改为“application/json”。我创建了一个 servlet 请求包装器来更改值,但是当请求到达 Controller 时,它仍然是“文本/纯文本”。日志显示在点击 doFilter() 之前标题值已更改;

这是我扩展 HttpServletRequestWrapper 的类

class HttpServletRequestWritableWrapper extends HttpServletRequestWrapper {
private final Logger logger = org.slf4j.LoggerFactory.getLogger(HttpServletRequestWritableWrapper.class);
private final ByteArrayInputStream decryptedBody;

HttpServletRequestWritableWrapper(HttpServletRequest request, byte[] decryptedData) {
super(request);
decryptedBody = new ByteArrayInputStream(decryptedData);
}



@Override
public String getHeader(String name) {
String headerValue = super.getHeader(name);
if("Accept".equalsIgnoreCase(name))
{
logger.debug("Accept header changing :");
return headerValue.replaceAll(
MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_JSON_VALUE
);
}
else if ("Content-Type".equalsIgnoreCase(name))
{
logger.debug("Content type change: ");
return headerValue.replaceAll(MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_JSON_VALUE);
}
return headerValue;
}

@Override
public Enumeration<String> getHeaderNames() {
return super.getHeaderNames();
}

@Override
public String getContentType() {
String contentTypeValue = super.getContentType();
if (MediaType.TEXT_PLAIN_VALUE.equalsIgnoreCase(contentTypeValue)) {
logger.debug("Changing on getContentType():");
return MediaType.APPLICATION_JSON_VALUE;
}
return contentTypeValue;
}


@Override
public BufferedReader getReader() throws UnsupportedEncodingException {
return new BufferedReader(new InputStreamReader(decryptedBody, UTF_8));
}

@Override
public ServletInputStream getInputStream() throws IOException {
return new ServletInputStream() {
@Override
public int read() {
return decryptedBody.read();
}
};
}

这是我的过滤器:
@WebFilter(displayName = "EncryptionFilter", urlPatterns = "/*")
public class EncryptionFilter implements Filter {
private final Logger logger = org.slf4j.LoggerFactory.getLogger(EncryptionFilter.class);

@Override
public void init(FilterConfig filterConfig) throws ServletException {

}

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
byte[] data = "{\"currentClientVersion\":{\"majorElement\":\"1\",\"minorElement\":\"2\"}}".getBytes();

logger.debug("data string " + data.toString());

logger.debug("Content-type before: " + servletRequest.getContentType());

HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletRequestWritableWrapper requestWrapper = new HttpServletRequestWritableWrapper(request, data);
//logger.debug("Accept Header: " + requestWrapper.getHeader("Accept"));
//logger.debug("Content-Type: " + requestWrapper.getHeader("Content-Type"));
//logger.debug("Contenttype" + requestWrapper.getContentType());

filterChain.doFilter(requestWrapper, servletResponse);


}

@Override
public void destroy() {

}


}

最佳答案

似乎在我的过滤器之后在其他地方调用了 getHeaders 方法,并且没有返回带有我更新值的标题。

我在我的 HttpServletRequestWrapper 中添加了这个覆盖,它现在正在工作:

@Override
public Enumeration<String> getHeaders(String name) {
List<String> headerVals = Collections.list(super.getHeaders(name));
int index = 0;
for (String value : headerVals) {
if ("Content-Type".equalsIgnoreCase(name)) {
logger.debug("Content type change: ");
headerVals.set(index, MediaType.APPLICATION_JSON_VALUE);
}

index++;
}

return Collections.enumeration(headerVals);
}

关于spring - Servlet 过滤器修改 header 值,而 servlet 请求包装器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38416235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com