parent
baea48be68
commit
ab992f4848
@ -0,0 +1,48 @@ |
||||
package com.ruoyi.common.filter; |
||||
|
||||
import java.io.IOException; |
||||
import javax.servlet.Filter; |
||||
import javax.servlet.FilterChain; |
||||
import javax.servlet.FilterConfig; |
||||
import javax.servlet.ServletException; |
||||
import javax.servlet.ServletRequest; |
||||
import javax.servlet.ServletResponse; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
|
||||
import com.ruoyi.common.enums.HttpMethod; |
||||
|
||||
/** |
||||
* Repeatable 过滤器 |
||||
* |
||||
* @author ruoyi |
||||
*/ |
||||
public class RepeatableFilter implements Filter |
||||
{ |
||||
@Override |
||||
public void init(FilterConfig filterConfig) throws ServletException |
||||
{ |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) |
||||
throws IOException, ServletException |
||||
{ |
||||
HttpServletRequest req = (HttpServletRequest) request; |
||||
if (HttpMethod.PUT.name().equals(req.getMethod()) || HttpMethod.POST.name().equals(req.getMethod())) |
||||
{ |
||||
RepeatedlyRequestWrapper repeatedlyRequest = new RepeatedlyRequestWrapper((HttpServletRequest) request); |
||||
chain.doFilter(repeatedlyRequest, response); |
||||
} |
||||
else |
||||
{ |
||||
chain.doFilter(request, response); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void destroy() |
||||
{ |
||||
|
||||
} |
||||
} |
@ -0,0 +1,84 @@ |
||||
package com.ruoyi.common.filter; |
||||
|
||||
import java.io.BufferedReader; |
||||
import java.io.ByteArrayInputStream; |
||||
import java.io.IOException; |
||||
import java.io.InputStreamReader; |
||||
import java.nio.charset.Charset; |
||||
import javax.servlet.ReadListener; |
||||
import javax.servlet.ServletInputStream; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletRequestWrapper; |
||||
import com.ruoyi.common.utils.StringUtils; |
||||
|
||||
/** |
||||
* 构建可重复读取inputStream的request |
||||
* |
||||
* @author ruoyi |
||||
*/ |
||||
public class RepeatedlyRequestWrapper extends HttpServletRequestWrapper |
||||
{ |
||||
private final byte[] body; |
||||
|
||||
public RepeatedlyRequestWrapper(HttpServletRequest request) throws IOException |
||||
{ |
||||
super(request); |
||||
body = readBytes(request.getReader(), "utf-8"); |
||||
} |
||||
|
||||
@Override |
||||
public BufferedReader getReader() throws IOException |
||||
{ |
||||
return new BufferedReader(new InputStreamReader(getInputStream())); |
||||
} |
||||
|
||||
@Override |
||||
public ServletInputStream getInputStream() throws IOException |
||||
{ |
||||
final ByteArrayInputStream bais = new ByteArrayInputStream(body); |
||||
return new ServletInputStream() |
||||
{ |
||||
|
||||
@Override |
||||
public boolean isFinished() |
||||
{ |
||||
return false; |
||||
} |
||||
|
||||
@Override |
||||
public boolean isReady() |
||||
{ |
||||
return false; |
||||
} |
||||
|
||||
@Override |
||||
public void setReadListener(ReadListener listener) |
||||
{ |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public int read() throws IOException |
||||
{ |
||||
return bais.read(); |
||||
} |
||||
}; |
||||
} |
||||
|
||||
/** |
||||
* 通过BufferedReader和字符编码集转换成byte数组 |
||||
*/ |
||||
private byte[] readBytes(BufferedReader br, String encoding) throws IOException |
||||
{ |
||||
String str = null, retStr = ""; |
||||
while ((str = br.readLine()) != null) |
||||
{ |
||||
retStr += str; |
||||
} |
||||
if (StringUtils.isNotBlank(retStr)) |
||||
{ |
||||
return retStr.getBytes(Charset.forName(encoding)); |
||||
} |
||||
return null; |
||||
} |
||||
} |
@ -1,4 +1,4 @@ |
||||
package com.ruoyi.common.xss; |
||||
package com.ruoyi.common.filter; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.ArrayList; |
@ -1,4 +1,4 @@ |
||||
package com.ruoyi.common.xss; |
||||
package com.ruoyi.common.filter; |
||||
|
||||
import java.io.ByteArrayInputStream; |
||||
import java.io.IOException; |
Loading…
Reference in new issue