From 00181aaa104c7c3bacee6c735c51b6ee2707f39c Mon Sep 17 00:00:00 2001 From: Raod <1130305001@qq.com> Date: Wed, 26 Jan 2022 11:22:10 +0800 Subject: [PATCH] =?UTF-8?q?url=E4=B8=AD=E5=8D=88=E7=BC=96=E7=A0=81filter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gaea/business/filter/UrlDecodeFilter.java | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 report-core/src/main/java/com/anjiplus/template/gaea/business/filter/UrlDecodeFilter.java diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/filter/UrlDecodeFilter.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/filter/UrlDecodeFilter.java new file mode 100644 index 00000000..0d79a3a0 --- /dev/null +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/filter/UrlDecodeFilter.java @@ -0,0 +1,77 @@ +package com.anjiplus.template.gaea.business.filter; + +import com.anji.plus.gaea.constant.GaeaConstant; +import org.apache.catalina.util.ParameterMap; +import org.apache.commons.lang3.StringUtils; +import org.apache.ibatis.ognl.IteratorEnumeration; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; + +import javax.servlet.*; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequestWrapper; +import java.io.IOException; +import java.net.URLDecoder; +import java.util.Enumeration; +import java.util.Map; +import java.util.Set; + +/** + * @author: Raod + * @since: 2022-01-26 + */ +@Component +@Order(Ordered.HIGHEST_PRECEDENCE + 1) +public class UrlDecodeFilter implements Filter { + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { + HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; + ParameterMap parameterMap = (ParameterMap) httpServletRequest.getParameterMap(); + ParamHttpServletRequestWrapper wrapper = new ParamHttpServletRequestWrapper(httpServletRequest, parameterMap); + + Enumeration parameterNames = wrapper.getParameterNames(); + while (parameterNames.hasMoreElements()) { + String paramName = parameterNames.nextElement(); + String parameter = httpServletRequest.getParameter(paramName); + if (StringUtils.isNotBlank(parameter)) { + String decode = URLDecoder.decode(parameter, GaeaConstant.CHARSET_UTF8); + parameterMap.setLocked(false); + parameterMap.put(paramName, new String[]{decode}); + } + } + filterChain.doFilter(wrapper, servletResponse); + } + + /** + * 参数 + */ + class ParamHttpServletRequestWrapper extends HttpServletRequestWrapper { + + private ParameterMap parameterMap; + + public ParamHttpServletRequestWrapper(HttpServletRequest request, ParameterMap parameterMap) { + super(request); + this.parameterMap = parameterMap; + } + + @Override + public Map getParameterMap() { + return parameterMap; + } + + @Override + public Enumeration getParameterNames() { + + Set keySet = parameterMap.keySet(); + IteratorEnumeration iteratorEnumeration = new IteratorEnumeration(keySet.iterator()); + return iteratorEnumeration; + } + + @Override + public String[] getParameterValues(String name) { + return parameterMap.get(name); + } + } +}