使用Servlet过滤器覆盖查询字符串

时间:2015-10-27 15:21:36

标签: java servlets servlet-filters

我有一个要求,我需要解密查询字符串。我计划通过Servlet Filter和扩展HttpServletRequestWrapper来实现,如下所示。

@WebFilter(filterName = "urlDecryptionFilter", urlPatterns = {"/*"})
public class UrlDecryptionFilter implements Filter {
    static class FilteredRequest extends HttpServletRequestWrapper {
        public FilteredRequest(ServletRequest request) {
            super((HttpServletRequest)request);
        }

        public String getQueryString() {
            //here i put the logic to transform the existing string
            return "quertStr=modified";
        }
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        chain.doFilter(new FilteredRequest(request), response);
    }

    @Override
    public void destroy() {

    }
}

但这似乎不起作用。如果有人能指出正确的方向,我将不胜感激。

0 个答案:

没有答案