在Spring中如何在传入请求中放置一个’/’。

huangapple go评论103阅读模式
英文:

How to place a '/' in an incoming request in Spring

问题

External, unmodifiable service 使用 '___' 而不是 '/' 用于 URL 中的参数路径变量。所以 'http://localhost:9091/infra/Test/Test' 变成 'http://localhost:9091/infra/Test___Test'。

对于每个传入的请求,我需要确保任何可能的 '___' 在请求被处理之前都会转换成 '/' 而不改变映射。以下是我想出来的解决方案:

@Component
public class SlashFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        final HttpServletRequestWrapper wrapped = new HttpServletRequestWrapper(request) {
            @Override
            public String getServletPath() {
                final String originalUrl = ((HttpServletRequest) getRequest()).getRequestURL().toString();
                return originalUrl.replace("___", "/").replace("%20", " "); // 强调
            }
        };

        filterChain.doFilter(wrapped, response);
    }

    @Override
    protected boolean shouldNotFilter(HttpServletRequest request) {
        return !request.getPathInfo().contains("___");
    }
}

然而,这似乎没有起到作用。如果我收到 http://localhost:9091/infra/Test___Test,它会变成 http://localhost:9091/infra/Test/Test,并且我会收到一个错误:

DELETE /infra/Test___Test http: 500 INTERNAL_SERVER_ERROR {"status":500,"error":"Internal Server Error","message":"Invalid contextPath: 'http://localhost:9091/infra/Test/Test': must start with '/' and not end with '/'","path":"/infra/Test___Test"}

所以,我想让我们在这里对 '/' 进行编码!我将标记的行更改为:

return originalUrl.replace("___", "%2F").replace("%20", " ");

然而,这给我带来了以下错误:

DELETE /infra/Test___Test http: 500 INTERNAL_SERVER_ERROR {"status":500,"error":"Internal Server Error","message":"Invalid contextPath: 'http://localhost:9091/infra/Test%252FTest': must start with '/' and not end with '/'","path":"/infra/Test___Test"}

看起来它也转换了 '%','%2F' 变成了 '%252F'。我有点困惑了,已经整天在尝试这个和 RequestWrapper,但到目前为止什么都没用。有人有想法如何让它工作吗?

英文:

An external, unmodifiable service uses '___' instead of '/' for ~parameters~ path variables in URLs. So 'http://localhost:9091/infra/Test/Test' becomes 'http://localhost:9091/infra/Test___Test'.

For every incoming request, I need to make sure that any possible '___' gets converted into '/' without changing the mapping before the request is processed. This is what I came up with:

@Component
public class SlashFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        final HttpServletRequestWrapper wrapped = new HttpServletRequestWrapper(request) {
            @Override
            public String getServletPath() {
                final String originalUrl = ((HttpServletRequest) getRequest()).getRequestURL().toString();
                return originalUrl.replace("___", "/").replace("%20", " "); // emphasis
            }
        };

        filterChain.doFilter(wrapped, response);
    }

    @Override
    protected boolean shouldNotFilter(HttpServletRequest request) {
        return !request.getPathInfo().contains("___");
    }
}

However, this seems to effectively do nothing. If I receive http://localhost:9091/infra/Test___Test, it becomes http://localhost:9091/infra/Test/Test and I get an error:

DELETE /infra/Test___Test http: 500 INTERNAL_SERVER_ERROR {"status":500,"error":"Internal Server Error","message":"Invalid contextPath: 'http://localhost:9091/infra/Test/Test': must start with '/' and not end with '/'","path":"/infra/Test___Test"}

So, I thought let's encode the / here! I changed the marked line to:

return originalUrl.replace("___", "%2F").replace("%20", " ");

However, this gave me the following error:

DELETE /infra/Test___Test http: 500 INTERNAL_SERVER_ERROR {"status":500,"error":"Internal Server Error","message":"Invalid contextPath: 'http://localhost:9091/infra/Test%252FTest': must start with '/' and not end with '/'","path":"/infra/Test___Test"}

It looks like it converts the % as well, and '%2F' becomes '%252F'. I'm kinda getting lost here, have spent all day fiddling with this and the RequestWrapper, but so far nothing works. Anyone got an idea how to make it work?

答案1

得分: 0

In the end, I realized that there was no real use for using path variables and we changed to request parameters. That was a lot easier than hardcoding some filter.

英文:

In the end, I realized that there was no real use for using path variables and we changed to request parameters. That was a lot easier than hardcoding some filter

huangapple
  • 本文由 发表于 2023年4月17日 23:03:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76036565.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定