有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring AntRequestMatcher不匹配

我有一个关于蚂蚁匹配者的问题。我想创建一个不应用于特定URL的spring安全过滤器

要忽略此url,我正在使用AntPathRequestMatcher

AntPathRequestMatcher ignored = new AntPathRequestMatcher("**/MyURL/**");

实际上,我认为它会匹配所有包含“MyURL”的URL。但我错了

if(ignored.matches(httpReq)) {
    log.info(ignored.toString()+" matches "+httpReq.getRequestURI());            
} else {
    log.info(ignored.toString()+" does not match "+httpReq.getRequestURI());             
}

上面这一节给我带来了

Ant [pattern='**/myurl/**'] does not match /resource/MyURL/

当我提出请求时在日志中。模式should be case unsensitive


共 (1) 个答案

  1. # 1 楼答案

    正如@M. Deinum所写,您的模式必须以/开头,请参见AntPathMatcher

    Note: a pattern and a path must both be absolute or must both be relative in order for the two to match. Therefore it is recommended that users of this implementation to sanitize patterns in order to prefix them with "/" as it makes sense in the context in which they're used.

    AntPathMatcher#doMatch

    protected boolean More ...doMatch(String pattern, String path, boolean fullMatch, Map<String, String> uriTemplateVariables) {
        if (path.startsWith(this.pathSeparator) != pattern.startsWith(this.pathSeparator)) {
            return false;
        }