1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| package cn.gloduck.onlinetest.security.xss;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.util.HtmlUtils;
import javax.servlet.ReadListener; import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import java.io.*; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map;
public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper { private static ObjectMapper objectMapper = new ObjectMapper();
public XssHttpServletRequestWrapper(HttpServletRequest request) { super(request); }
@Override public String getHeader(String name) { String target = super.getHeader(name); return target == null ? null : HtmlUtils.htmlEscape(target); }
@Override public String getQueryString() { String target = super.getQueryString(); return target == null ? null : HtmlUtils.htmlEscape(target); }
@Override public String getParameter(String name) { String target = super.getParameter(name); return target == null ? null : HtmlUtils.htmlEscape(target); }
@Override public String[] getParameterValues(String name) { String[] values = super.getParameterValues(name); if(values != null) { int length = values.length; String[] escapseValues = new String[length]; for(int i = 0; i < length; i++){ escapseValues[i] = HtmlUtils.htmlEscape(values[i]); } return escapseValues; } return values; }
@Override public ServletInputStream getInputStream() throws IOException { String str=getRequestBody(super.getInputStream()); Map<String,Object> map= objectMapper.readValue(str, Map.class); Map<String,Object> resultMap=new HashMap<>(map.size()); for(String key:map.keySet()){ Object val=map.get(key); if(map.get(key) instanceof String){ resultMap.put(key,HtmlUtils.htmlEscape(val.toString())); }else{ resultMap.put(key,val); } }
str=objectMapper.writeValueAsString(resultMap); final ByteArrayInputStream bais = new ByteArrayInputStream(str.getBytes()); return new ServletInputStream() { @Override public int read() throws IOException { return bais.read(); } @Override public boolean isFinished() { return false; } @Override public boolean isReady() { return false; } @Override public void setReadListener(ReadListener listener) { } }; }
private String getRequestBody(InputStream stream) { String line = ""; StringBuilder body = new StringBuilder(); int counter = 0;
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)); try { while ((line = reader.readLine()) != null) { body.append(line); counter++; } } catch (IOException e) { e.printStackTrace(); } return body.toString(); } }
|