標籤:spring 攔截
現在由這麼一個需求,就是修改幾個功能模組的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
修改springmvc返回ajax方式的json資料