我有一个过滤器
public class CheckFilter implements Filter {
过滤器执行
@Override
public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {
if(req instanceof HttpServletRequest && res instanceof HttpServletResponse){
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequestWrapper requestMod = new ReqWrapper((HttpServletRequest) request);
chain.doFilter(requestMod , response);
}
我的目的是从查询的一部分网址中删除一个特定的值。 http:\ site \ page?a = 1&b = 2&c = 3;
我需要去除b
及其值。
过滤后的网址必须传递给该链中没有该参数的其他过滤器。
public class MultisessionReqWrapper extends HttpServletRequestWrapper {
private String param_to_strip = "b";
public MultisessionReqWrapper(final HttpServletRequest request)
throws IOException {
super(request);
}
根据Modify request parameter with servlet filter,我通过了所有带有调试的方法,但没有成功。
最重要的方法是那些。
@Override
public java.lang.String getParameter(java.lang.String arg0) {
if (StringUtils.equals(arg0, param_to_strip )) {
return null;
} else {
String param = super.getParameter(arg0);
return param;
}
}
@Override
public Map getParameterMap() {
Map<String, String[]> pm = super.getParameterMap();
pm.remove(param_to_strip );
return pm;
}
@Override
public Enumeration getParameterNames() {
Enumeration<String> pn = super.getParameterNames();
if (pn != null) {
Vector<String> v = new Vector<>();
while (pn.hasMoreElements()) {
String string = (String) pn.nextElement();
if (!StringUtils.equals(string, param_to_strip )) {
v.add(string);
}
}
return (Enumeration) v;
} else {
return pn;
}
}
@Override
public java.lang.String[] getParameterValues(java.lang.String arg0) {
if (StringUtils.equals(arg0, param_to_strip )) {
return null;
} else {
String[] pv = super.getParameterValues(arg0);
return pv;
}
}
@Override
public java.lang.String getQueryString() {
StringBuilder requestURL = new StringBuilder(super.getRequestURL()
.toString());
String queryString = super.getQueryString();
if (queryString == null) {
return null;
} else {
try {
String path = requestURL.append('?').append(queryString)
.toString();
URI uri = new URI(path);
UriBuilder uriBuilder = UriBuilder.fromUri(uri);
URI newuri = uriBuilder.replaceQueryParam(param_to_strip ,
(Object[]) null).build();
URL url = newuri.toURL();
return url.getQuery();
// url= new
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
// removeParameterFromURI()
return "";
}
最后,它似乎通过保留原始值的方法。我是否也要覆盖这个?
public ServletRequest getRequest() {
return super.getRequest();
}