概述
响应数据拦截
1、ResponseFilter
package com.ws.ldy.config.auth.filter;
import com.alibaba.fastjson.JSON;
import com.ws.ldy.config.auth.service.FilterService;
import com.ws.ldy.config.auth.wrapper.ResponseWrapper;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.Method;
/**
* 返回值输出过滤器,这里用来加密返回值
*
* @Title: ResponseFilter
* @Description:
* @author kokJuis
* @date 上午9:52:42
*/
@Slf4j
public class ResponseFilter implements Filter {
private FilterService filterService;
public ResponseFilter(FilterService filterService) {
this.filterService = filterService;
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
String uri = request.getRequestURI();
// 1、排除
Boolean aBoolean = filterService.excludeUri(uri);
if (!aBoolean) {
filterChain.doFilter(servletRequest, servletResponse);
return;
}
// 转换成代理类
ResponseWrapper wrapperResponse = new ResponseWrapper(response);
// 这里只拦截返回,直接让请求过去,如果在请求前有处理,可以在这里处理
filterChain.doFilter(request, wrapperResponse);
// 返回值字节
byte[] content = wrapperResponse.getContent();
// 判断是否有值
if (content.length <= 0) {
return;
}
// 返回值
String str = new String(content, "UTF-8");
// 调用链
if (!isJson(str)) {
// 如果不是json数据,可能是返回的数据流或下载文件,直接返回原数据
//把返回值输出到客户端
ServletOutputStream out = response.getOutputStream();
out.write(content);
out.flush();
} else {
// 处理返回数据
String cipherText = null;
try {
cipherText = str;
//......根据需要处理返回值
} catch (Exception e) {
e.printStackTrace();
}
//把返回值输出到客户端
ServletOutputStream out = response.getOutputStream();
out.write(cipherText.getBytes());
out.flush();
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
@Override
public void destroy() {
}
/**
* 判断字符串数据是否为json
* @param content
* @return
*/
public static boolean isJson(String content) {
try {
JSON.parseObject(content);
return true;
} catch (Exception e) {
return false;
}
}
}
2、ResponseWrapper
package com.ws.ldy.config.auth.wrapper;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
/**
* 返回值输出代理类
*
* @Title: ResponseWrapper
* @Description:
* @author kokJuis
* @date 上午9:52:11
*/
public class ResponseWrapper extends HttpServletResponseWrapper {
private ByteArrayOutputStream buffer;
private ServletOutputStream out;
public ResponseWrapper(HttpServletResponse httpServletResponse) {
super(httpServletResponse);
buffer = new ByteArrayOutputStream();
out = new WrapperOutputStream(buffer);
}
@Override
public ServletOutputStream getOutputStream()
throws IOException {
return out;
}
@Override
public void flushBuffer()
throws IOException {
if (out != null) {
out.flush();
}
}
public byte[] getContent()
throws IOException {
flushBuffer();
return buffer.toByteArray();
}
class WrapperOutputStream extends ServletOutputStream {
private ByteArrayOutputStream bos;
public WrapperOutputStream(ByteArrayOutputStream bos) {
this.bos = bos;
}
@Override
public void write(int b) throws IOException {
bos.write(b);
}
@Override
public boolean isReady() {
// TODO Auto-generated method stub
return false;
}
@Override
public void setWriteListener(WriteListener arg0) {
// TODO Auto-generated method stub
}
}
}
最后
以上就是英勇乌龟为你收集整理的java Filter过滤器获取响应数据的全部内容,希望文章能够帮你解决java Filter过滤器获取响应数据所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复