本文介绍了不区分大小写的查询字符串请求参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是以下所有URI都可以使用

My goal is that all below URI's should work

https:// rest / xyz? sort = name

https://rest/xyz?sort=name

https:// rest / xyz?排序 =姓名

https://rest/xyz?Sort=name

https:// rest / xyz ?过滤器 =名称=值

https://rest/xyz?filter=name=value

https:// rest / xyz?过滤器 =名称=值

https://rest/xyz?Filter=name=value

为实现这一点,我创建了自定义过滤器来覆盖传递给FilterChain的HttpServletRequest。以下是此方法的链接:

To achieve this, I have created custom filter that overrides the HttpServletRequest that is passed to the FilterChain. Below is the link for this approach:

我的代码:

import java.io.IOException;
import java.util.Map;

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 javax.servlet.http.HttpServletRequestWrapper; 

public class HttpCustomParamFilter implements Filter
{
    private static class HttpServletRequestCustomeWrapper extends HttpServletRequestWrapper
    {
        private String[] parameterValues;

        @Override
        public String[] getParameterValues(String name)
        {
            Map<String, String[]> localParameterMap = super.getParameterMap();

            // Handle case insensitivity of http request paramters like start, count, query, sort, filter etc.
            if (localParameterMap != null && !localParameterMap.isEmpty())
            {
                parameterValues = new String[localParameterMap.size()];
                for (String key : localParameterMap.keySet())
                {
                    if (name.equalsIgnoreCase(key))
                        parameterValues = localParameterMap.get(key);
                    else
                        parameterValues = null;
                }
            }
            return parameterValues;
        }

        public HttpServletRequestCustomWrapper(final ServletRequest request)
        {
            super((HttpServletRequest) request);
        }


    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain) throws IOException, ServletException {
        // override the request passed to the FilterChain
        chain.doFilter(new HttpServletRequestCustomWrapper(request), response);
        }

    @Override
    public void init(FilterConfig filterConfig)
            throws ServletException
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void destroy()
    {
        // TODO Auto-generated method stub

    }

}

在此代码中,我重写了getParameterValues(字符串名称)方法和请求参数的大小写不敏感,但不确定我是否需要覆盖任何其他方法。

In this code, i have overriden getParameterValues(String name) method and achieved case-insensitivity of request paramters, but not sure if i need to override any other methods.

我的疑惑:


  • 我是否需要覆盖其他方法,例如getParameter()和getParameterNames()?

  • do i need to override other methods also like getParameter() and getParameterNames()?

哪个内部实现受此影响?

what internal implementation is impacted with this?

哪个类可以看到getParameter(),getParameterNames()和getParameterValues()的代码实现?

which class i can see the code implementation of getParameter(), getParameterNames() and getParameterValues()?

推荐答案

首先,让我说我的平安:我不认为修改HttpServletRequestWrapper是要走的路。我甚至不确定你将如何使用它,因为我的理解是它是特定于App Server的。作为旁注,有详细介绍如何使用HttpServletRequest来获取不区分大小写的查询参数,而无需自己滚动。

First, let me say my peace: I don't think modifying the HttpServletRequestWrapper is the way to go. I am not even sure how you would go about using it, as my understanding is it's App Server specific. As a side note, this article has specifics on how to use the HttpServletRequest to get a case-insensitive query param without rolling your own.

但是,本着回答问题的精神:

But, in the spirit of answering your questions:


  1. 您是否需要覆盖getParameter()和getParameterNames()?你可以,因为它会让你有能力操纵案件。事实上,我认为使查询参数不区分大小写的最安全的方法是仅覆盖那些方法。使getParameter()调用对字符串名称执行不区分大小写的等号。不确定你会用getParameterNames()做什么,也许会返回所有可能的情况,但这似乎是多余的。

  2. 这会影响哪些内部实现?我不确定。 HttpServletRequest几乎是所有内容的核心,如果你的代码不是100%可靠,那么你无法说明你可以引入什么。例如,Spring有一个SecurityContextHolderAwareRequestWrapper,这是否意味着你刚刚打破了Spring Security?没有经过大量测试就没问题。

  3. 我可以在哪个类中看到getParameter(),getParameterNames()和getParameterValues()的代码实现?根据JavaDocs,HttpServletRequestWrapper是HttpServletRequest接口的唯一实现。此类的实际实现取决于您的应用程序容器。例如,在我的应用程序中它的weblogic.servlet.internal.ServletRequestImpl,因为我使用Web Logic。希望您使用的是具有现成代码的开源应用服务器。我发现这个的方法是在我的一个Controller处理程序方法中放置一个中断,该方法定义了HttpServletRequest并在调试器中查看它的getClass()响应。

这篇关于不区分大小写的查询字符串请求参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 03:07