在spring mvc中默认 访问url 加任意后缀名都能访问

比如:你想访问 /login ,但是通过 /login.do /login.action /login.json 都能访问

通常来说可能没有影响,但对于权限控制,这就严重了。

权限控制通常有两种思路:

1)弱权限控制

允许所有url通过,仅对个别重要的url做权限控制。此种方式比较简单,不需要对所有url资源进行配置,只配置重要的资源。

2)强权限控制

默认禁止所有url请求通过,仅开放授权的资源。此种方式对所有的url资源进行控制。在系统种需要整理所有的请求,或者某一目录下所有的url资源。这种方式安全控制比较严格,操作麻烦,但相对安全。


如果用第二种方式,则上面spring mvc的访问策略对安全没有影响。

但如果用第一种安全策略,则会有很大的安全风险。

例如:我们控制了/login 的访问,但是我们默认除/login的资源不受权限控制约束,那么攻击者就可以用 /login.do /login.xxx 来访问我们的资源。

在spring 3.1之后,url找对应方法的处理步骤,第一步,直接调用RequestMappingHandlerMapping查找到相应的处理方法,第二步,调用RequestMappingHandlerAdapter进行处理

我们在RequestMappingHandlerMapping中可以看到

/**
 * Whether to use suffix pattern match for registered file extensions only
 * when matching patterns to requests.
 * <p>If enabled, a controller method mapped to "/users" also matches to
 * "/users.json" assuming ".json" is a file extension registered with the
 * provided {@link #setContentNegotiationManager(ContentNegotiationManager)
 * contentNegotiationManager}. This can be useful for allowing only specific
 * URL extensions to be used as well as in cases where a "." in the URL path
 * can lead to ambiguous interpretation of path variable content, (e.g. given
 * "/users/{user}" and incoming URLs such as "/users/john.j.joe" and
 * "/users/john.j.joe.json").
 * <p>If enabled, this flag also enables
 * {@link #setUseSuffixPatternMatch(boolean) useSuffixPatternMatch}. The
 * default value is {@code false}.
 */
public void setUseRegisteredSuffixPatternMatch(boolean useRegisteredSuffixPatternMatch) {
   this.useRegisteredSuffixPatternMatch = useRegisteredSuffixPatternMatch;
   this.useSuffixPatternMatch = (useRegisteredSuffixPatternMatch || this.useSuffixPatternMatch);
}
 
那么如何来配置呢?
  <mvc:annotation-driven>
    <mvc:path-matching suffix-pattern="false" />
  </mvc:annotation-driven>
在匹配模式时是否使用后缀模式匹配,默认值为true。这样你想访问 /login ,通过 /login.* 就不能访问了
转账请注明出处 锋行的blog:http://blog.csdn/ruipheng/article/details/65438703

更多推荐

spring mvc url匹配禁用后缀访问