简体   繁体   English

不区分大小写的查询字符串请求参数

[英]Case-insensitive query string request parameters

My goal is that all below URI's should work 我的目标是以下所有URI都应该有效

https://rest/xyz? https://开头休息/ XYZ? sort =name sort = name

https://rest/xyz? https://开头休息/ XYZ? Sort =name 排序 =名称

https://rest/xyz? https://开头休息/ XYZ? filter =name=value filter = name = value

https://rest/xyz? https://开头休息/ XYZ? Filter =name=value Filter = name = value

To achieve this, I have created custom filter that overrides the HttpServletRequest that is passed to the FilterChain. 为此,我创建了自定义过滤器来覆盖传递给FilterChain的HttpServletRequest。 Below is the link for this approach: 以下是此方法的链接:

http://forum.springsource.org/archive/index.php/t-87433.html http://forum.springsource.org/archive/index.php/t-87433.html

My code: 我的代码:

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

    }

}

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. 在这段代码中,我重写了getParameterValues(String name)方法并实现了请求参数的大小写不敏感,但不确定我是否需要覆盖任何其他方法。

my doubts: 我的疑惑:

  • do i need to override other methods also like getParameter() and getParameterNames()? 我是否需要覆盖其他方法,如getParameter()和getParameterNames()?

  • what internal implementation is impacted with this? 内部实施受此影响的是什么?

  • which class i can see the code implementation of getParameter(), getParameterNames() and getParameterValues()? 哪个类我可以看到getParameter(),getParameterNames()和getParameterValues()的代码实现?

First, let me say my peace: I don't think modifying the HttpServletRequestWrapper is the way to go. 首先,让我说出我的平安:我不认为修改HttpServletRequestWrapper是要走的路。 I am not even sure how you would go about using it, as my understanding is it's App Server specific. 我甚至不确定你将如何使用它,因为我的理解是它是特定于App Server的。 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. 作为旁注, 本文详细介绍了如何使用HttpServletRequest获取不区分大小写的查询参数,而无需自行编译。

But, in the spirit of answering your questions: 但是,本着回答你的问题的精神:

  1. Do you need to override getParameter() and getParameterNames()? 你需要覆盖getParameter()和getParameterNames()吗? You could, as it would give you the ability to manipulate the case. 你可以,因为它会让你有能力操纵案件。 In fact, I would say the safest way to make the query parameters case-insensitive would be to overwrite ONLY those methods. 事实上,我认为使查询参数不区分大小写的最安全的方法是仅覆盖那些方法。 Make the getParameter() call do a case-insensitive equals on the string names. 使getParameter()调用对字符串名称执行不区分大小写的等号。 Not sure what you would do with getParameterNames(), maybe return every possible case, but this seems redundant. 不确定你会用getParameterNames()做什么,也许会返回所有可能的情况,但这似乎是多余的。
  2. What internal implementation is impacted by this? 这会影响哪些内部实施? I am not certain. 我不确定。 HttpServletRequest is so core to pretty much everything, there is no telling what you could introduce if your code is not 100% solid. HttpServletRequest几乎是所有内容的核心,如果您的代码不是100%可靠,那么无法告诉您可以引入什么。 For instance, Spring has a SecurityContextHolderAwareRequestWrapper, so does that mean you just broke Spring Security? 例如,Spring有一个SecurityContextHolderAwareRequestWrapper,这是否意味着你刚刚打破了Spring Security? No telling without a lot of testing. 没有很多测试就没有告诉。
  3. Which class can I see the code implementation of getParameter(), getParameterNames(), and getParameterValues()? 我可以在哪个类中看到getParameter(),getParameterNames()和getParameterValues()的代码实现? HttpServletRequestWrapper is the only implementation of HttpServletRequest interface, according to the JavaDocs. 根据JavaDocs,HttpServletRequestWrapper是HttpServletRequest接口的唯一实现。 The actual implementation of this class is dependent on your application container. 此类的实际实现取决于您的应用程序容器。 For instance, in my app its weblogic.servlet.internal.ServletRequestImpl, since I use Web Logic. 例如,在我的应用程序中它的weblogic.servlet.internal.ServletRequestImpl,因为我使用Web Logic。 Hopefully you are using an open-source app server that has the code readily available. 希望您使用的是具有现成代码的开源应用服务器。 The way I found this was to put a break in one of my Controller handler methods that has HttpServletRequest defined and viewing it's getClass() response in the debugger. 我发现这个的方法是在我的一个Controller处理程序方法中放置一个中断,该方法定义了HttpServletRequest并在调试器中查看它的getClass()响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM