Tomcat 8.5.86版本启动web项目报错Caused by: java.lang.IllegalArgumentException: Invalid <url-pattern> [sso.action] in filter mapping
查看项目的web.xml文件相关片段
<filter-mapping>
<filter-name>SSOFilter</filter-name>
<url-pattern>sso.action</url-pattern>
</filter-mapping>
这里的url-pattern取值可能不符合标准。首先查看servlet 3.1规范
Java Servlet Specifications
Servlet 4.0 specification is JSR 369.
Servlet 3.1 specification is JSR 340.
Servlet 3.0 specification is JSR 315.
Servlet 2.5 is a maintenance release of Servlet 2.4. Both are JSR 154.
相关的内容如下图所示
再查看Tomcat的源码org.apache.catalina.core.StandardContext#validateURLPattern
/**
* Validate the syntax of a proposed <code><url-pattern></code> for conformance with specification
* requirements.
*
* @param urlPattern URL pattern to be validated
*
* @return <code>true</code> if the URL pattern is conformant
*/
private boolean validateURLPattern(String urlPattern) {
// 不能为空
if (urlPattern == null) {
return false;
}
// 不能包含换行 制表符
if (urlPattern.indexOf('\n') >= 0 || urlPattern.indexOf('\r') >= 0) {
return false;
}
// 空字符串代表匹配所有
if (urlPattern.equals("")) {
return true;
}
// 以 * 开头
if (urlPattern.startsWith("*.")) {
if (urlPattern.indexOf('/') < 0) {
checkUnusualURLPattern(urlPattern);
return true;
} else {
return false;
}
}
// 以/开头不包含*
if (urlPattern.startsWith("/") && !urlPattern.contains("*.")) {
checkUnusualURLPattern(urlPattern);
return true;
} else {
return false;
}
}
可以看到urlPattern可以为空字符串、至少以*.
开头不包含/
或者以/
开头不不包含*.
。
checkUnusualURLPattern仅仅是打印日志,不会导致报错
/**
* Check for unusual but valid <code><url-pattern></code>s. See Bugzilla 34805, 43079 & 43080
*/
private void checkUnusualURLPattern(String urlPattern) {
if (log.isInfoEnabled()) {
// First group checks for '*' or '/foo*' style patterns
// Second group checks for *.foo.bar style patterns
if ((urlPattern.endsWith("*") &&
(urlPattern.length() < 2 || urlPattern.charAt(urlPattern.length() - 2) != '/')) ||
urlPattern.startsWith("*.") && urlPattern.length() > 2 && urlPattern.lastIndexOf('.') > 1) {
log.info("Suspicious url pattern: \"" + urlPattern + "\"" + " in context [" + getName() + "] - see" +
" sections 12.1 and 12.2 of the Servlet specification");
}
}
}
以上实现在Tomcat 9.0.55中没有变化
综合分析,修改web.xml中配置如下(在url-pattern前面添加一个/)
<filter-mapping>
<filter-name>SSOFilter</filter-name>
<url-pattern>/sso.action</url-pattern>
</filter-mapping>