自定义filter ,通过HttpServletResponseWrapper 加密返回值。无法获取content做处理,又或者取到但是乱码。该如何解决?
开放对外接口,要对返回数据做加密。我通过filter来拦截请求做处理。通过自定义wapper来对返回数据操作。
HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse; EncryptResponseWrapper encryptResponseWrapper = new EncryptResponseWrapper(httpServletResponse);
encryptResponseWrapper.setCharacterEncoding("UTF-8");
filterChain.doFilter(servletRequest,encryptResponseWrapper);
newBIng提供了一种实现方式,但是实际测试并没有什么用,flushbuffer完全没有执行:
public class EncryptResponseWrapper extends HttpServletResponseWrapper { private CharArrayWriter charArrayWriter = new CharArrayWriter();
public EncryptResponseWrapper(HttpServletResponse response) {
super(response);
}
@Override
public PrintWriter getWriter() throws IOException {
return new PrintWriter(charArrayWriter);
}
@Override
public void flushBuffer() throws IOException {
// 获取原始响应对象
HttpServletResponse response = (HttpServletResponse) getResponse();
// 获取响应内容
String content = charArrayWriter.toString();
// 对响应内容进行加密
String encryptedContent = DesUtils.getEncryptString(content);
// 将加密后的内容写入原始响应对象
response.getWriter().write(encryptedContent);
// 刷新缓冲区
super.flushBuffer();
}
}
然后上网百度了别人的实现,都是先获取content,在做加密。
我用他们的方法无法获取到content,dofilter后的代码不会执行。
但是发现使用newbing提供的CharArrayWriter 能获取content。
但是获取content中文乱码。即使设置了UTF-8也没用。
EncryptResponseWrapper encryptResponseWrapper = new EncryptResponseWrapper(httpServletResponse); encryptResponseWrapper.setCharacterEncoding("UTF-8");
filterChain.doFilter(servletRequest,encryptResponseWrapper);
String content = encryptResponseWrapper.getCharArrayWriter().toString();
// 对响应内容进行加密
String encryptedContent = DesUtils.getEncryptString(content);
// 将加密后的内容写入原始响应对象
servletResponse.getWriter().write(encryptedContent);
package com.wondersgroup.wdls.js.wapper;import com.wondersgroup.wdls.js.web.utils.DesUtils;
import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.*;
/**
* @author xuxuehui
* @create 2023 - 08 - 18 9:39
*/
public class EncryptResponseWrapper extends HttpServletResponseWrapper {
private CharArrayWriter charArrayWriter = null;
private PrintWriter writer = null;
private ServletOutputStream out = null;
public EncryptResponseWrapper(HttpServletResponse response) throws IOException{
super(response);
charArrayWriter = new CharArrayWriter();
out = new WapperOutputStream(charArrayWriter);
writer = new PrintWriter(new OutputStreamWriter(new WapperOutputStream(charArrayWriter),this.getCharacterEncoding()));
}
@Override
public PrintWriter getWriter() throws IOException {
return writer;
}
@Override
public ServletOutputStream getOutputStream() throws IOException {
return out;
}
public CharArrayWriter getCharArrayWriter() {
return charArrayWriter;
}
@Override
public void flushBuffer() throws IOException {
if (out!=null){
out.flush();
}
if (writer !=null){
writer.flush();
}
}
@Override
public void reset() {
charArrayWriter.reset();
}
public String getResponseData() throws IOException{
flushBuffer();
return charArrayWriter.toString();
}
private class WapperOutputStream extends ServletOutputStream{
private CharArrayWriter charArrayWriter = null;
public WapperOutputStream(CharArrayWriter charArrayWriter){
this.charArrayWriter = charArrayWriter;
}
@Override
public void write(int b) throws IOException{
charArrayWriter.write(b);
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setWriteListener(WriteListener listener) {
}
}
}
又做了一些更改,但都是无用功。代码改动版本太多了,就不贴了;
如何解决编码问题,又或者new bing给的玩意能用,但是需要加东西。搞了太久,头都炸了。
回答:
writer = new PrintWriter(new OutputStreamWriter(new WapperOutputStream(charArrayWriter), "UTF-8"));
以上是 自定义filter ,通过HttpServletResponseWrapper 加密返回值。无法获取content做处理,又或者取到但是乱码。该如何解决? 的全部内容, 来源链接: utcz.com/p/945309.html