| | |
| | | package com.ruoyi.common.filter; |
| | | |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.IOException; |
| | | import cn.hutool.core.io.IoUtil; |
| | | import cn.hutool.core.lang.Validator; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.hutool.http.HtmlUtil; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.MediaType; |
| | | |
| | | import javax.servlet.ReadListener; |
| | | import javax.servlet.ServletInputStream; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletRequestWrapper; |
| | | import org.apache.commons.io.IOUtils; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.MediaType; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.html.EscapeUtil; |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.IOException; |
| | | import java.nio.charset.StandardCharsets; |
| | | |
| | | /** |
| | | * XSS过滤处理 |
| | | * |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper |
| | |
| | | for (int i = 0; i < length; i++) |
| | | { |
| | | // 防xss攻击和过滤前后空格 |
| | | escapseValues[i] = EscapeUtil.clean(values[i]).trim(); |
| | | escapseValues[i] = HtmlUtil.cleanHtmlTag(values[i]).trim(); |
| | | } |
| | | return escapseValues; |
| | | } |
| | |
| | | } |
| | | |
| | | // 为空,直接返回 |
| | | String json = IOUtils.toString(super.getInputStream(), "utf-8"); |
| | | if (StringUtils.isEmpty(json)) |
| | | String json = IoUtil.read(super.getInputStream(), StandardCharsets.UTF_8); |
| | | if (Validator.isEmpty(json)) |
| | | { |
| | | return super.getInputStream(); |
| | | } |
| | | |
| | | // xss过滤 |
| | | json = EscapeUtil.clean(json).trim(); |
| | | final ByteArrayInputStream bis = new ByteArrayInputStream(json.getBytes("utf-8")); |
| | | json = HtmlUtil.cleanHtmlTag(json).trim(); |
| | | |
| | | final ByteArrayInputStream bis = IoUtil.toStream(json, StandardCharsets.UTF_8); |
| | | return new ServletInputStream() |
| | | { |
| | | @Override |
| | |
| | | |
| | | /** |
| | | * 是否是Json请求 |
| | | * |
| | | * |
| | | * @param request |
| | | */ |
| | | public boolean isJsonRequest() |
| | | { |
| | | String header = super.getHeader(HttpHeaders.CONTENT_TYPE); |
| | | return MediaType.APPLICATION_JSON_VALUE.equalsIgnoreCase(header) |
| | | || MediaType.APPLICATION_JSON_UTF8_VALUE.equalsIgnoreCase(header); |
| | | return StrUtil.startWithIgnoreCase(header, MediaType.APPLICATION_JSON_VALUE); |
| | | } |
| | | } |
| | | } |