简体   繁体   English

使用 Spring @RequestParam 无法在 Controller 中获取“#”符号

[英]Cannot get '#' symbol in Controller using Spring @RequestParam

I have the following request Url /search?我有以下请求 Url /search? charset =UTF-8&q=C%23C%2B%2B.字符集=UTF-8&q=C%23C%2B%2B。 My controller looks like我的 controller 看起来像

@RequestMapping(method = RequestMethod.GET, params = "q")
public String refineSearch(@RequestParam("q") final String searchQuery,....

and here i have searchQuery = 'CC++'.在这里我有 searchQuery = 'CC++'。 '#' is encoded in '%23' and '+' is '%2B'. “#”编码为“%23”,“+”编码为“%2B”。 Why searchQuery does not contain '#'?为什么 searchQuery 不包含“#”?

searchQuery in debug调试中的搜索查询

The main cause is known as the "fragment identifier".主要原因被称为“片段标识符”。 You find more detail for Fragment Identifier right here .您可以在此处找到有关片段标识符的更多详细信息。 It says:它说:

The fragment identifier introduced by a hash mark # is the optional last part of a URL for a document.由井号 # 引入的片段标识符是文档 URL 的可选最后一部分。 It is typically used to identify a portion of that document.它通常用于标识该文档的一部分。

When you write # sign, it contains info for clientbase.当你写 # 号时,它包含客户端的信息。 Put everything only the browser needs here.把浏览器需要的所有东西都放在这里。 You can get this problem for all types of URI characters you can look Percent Encoding for this.对于所有类型的 URI 字符,您都可以遇到此问题,您可以为此查看百分比编码 In my opinion The simple solution is character replacing, you could try replace in serverbase.在我看来,简单的解决方案是字符替换,您可以尝试在 serverbase 中替换。

Finally i found a problem.In filters chain ServletRequest is wrapped in XSSRequestWrapper with DefaultXSSValueTranslator and here is the method String stripXSS(String value) which iterates through pattern list,in case if value matches with pattern, method will delete it.最后我发现了一个问题。在过滤器链中,ServletRequest 用 DefaultXSSValueTranslator 包裹在 XSSRequestWrapper 中,这里是方法 String stripXSS(String value) 迭代模式列表,如果值与模式匹配,方法将删除它。 Pattern list contains "\#" pattern and '#' will be replaced with ""模式列表包含 "\#" 模式,'#' 将被替换为 ""

DefaultXSSValueTranslator.
    private String stripXSS(String value) {
        Pattern scriptPattern;
        if (value != null && value.length() > 0) {
            for(Iterator var3 = this.patterns.iterator(); var3.hasNext(); value = scriptPattern.matcher(value).replaceAll("")) {
                scriptPattern = (Pattern)var3.next();
            }
        }

        return value;
    }

I resolved a similar problem by URL encoding the hash part.我通过对 hash 部分进行编码的 URL 解决了类似的问题。 We have Spring web server and mix of JS and VueJS client.我们有 Spring web 服务器以及 JS 和 VueJS 客户端的混合。 This fixed my problem:这解决了我的问题:

 const location = window.location; const redirect = location.pathname + encodeURIComponent(location.hash);

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

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