现在由这么一个需求,就是修改几个功能模块的springmvc的ajax请求返回值(对返回值加密),
因为controller很多,而且以前抱着开闭原则,这里就选择使用拦截器的方式(是Filter不是spring的handlerFilter)。废话不多少,上代码。(原始controller里返回的都是json数据)。
修改web.xml 添加filter
<filter>
<filter-name>jsonFilter</filter-name>
<filter-class>com.spr.util.UpdateJsonFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>jsonFilter</filter-name>
<url-pattern>/android/*</url-pattern><!-- 只拦截这个请求路径下的action-->
</filter-mapping>
2.处理类
package com.spr.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
public class UpdateJsonFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
ByteResponseWrapper brw = new ByteResponseWrapper(
(HttpServletResponse) response);
chain.doFilter(request, brw);
String out = new String(brw.getBytes());
if (!StringUtils.isBlank(out)) {
//处理加密的业务(略过)
out = out.toString();
} else {
out = "";
}
response.setContentType("application/json; charset=utf-8");
response.setContentLength(out.getBytes().length);
response.getOutputStream().write(out.getBytes());
}
public void destroy() {
}
public void init(FilterConfig filterConfig) {
}
static class ByteResponseWrapper extends HttpServletResponseWrapper {
private PrintWriter writer;
private ByteOutputStream output;
private int code = 200;
public byte[] getBytes() {
writer.flush();
return output.getBytes();
}
public ByteResponseWrapper(HttpServletResponse response) {
super(response);
output = new ByteOutputStream();
writer = new PrintWriter(output);
}
@Override
public PrintWriter getWriter() {
return writer;
}
@Override
public ServletOutputStream getOutputStream() throws IOException {
return output;
}
public int getStatus() {
return code;
}
public void sendRedirect(String location) throws IOException {
code = HttpStatus.MOVED_TEMPORARILY.value();
super.sendRedirect(location);
}
@Override
public void setStatus(int sc) {
this.code = sc;
}
@Override
public void setStatus(int sc, String message) {
this.code = sc;
}
@Override
public void sendError(int sc) throws IOException {
this.code = sc;
}
@Override
public void sendError(int sc, String msg) throws IOException {
this.code = sc;
}
}
static class ByteOutputStream extends ServletOutputStream {
private ByteArrayOutputStream bos = new ByteArrayOutputStream();
@Override
public void write(int b) throws IOException {
bos.write(b);
}
public byte[] getBytes() {
return bos.toByteArray();
}
}
}
本文出自 “重新来学JAVA” 博客,请务必保留此出处http://3131854.blog.51cto.com/3121854/1637831
原文地址:http://3131854.blog.51cto.com/3121854/1637831