问题描述
HttpSecurity.antMatcher()将HttpSecurity实例的默认请求匹配器从AnyRequestMatcher更改为AntPathRequestMatcher。ExpressionUrlAuthorizationConfigurer.ExpressionInterceptUrlRegistry.antMatchers()用于将授权规则应用于与当前HttpSecurity实例关联的端点的子集。
示例代码:
http .antMatcher("/api/**") .httpBasic() .disable() .authorizeRequests() .antMatchers("/api/user/**", "/api/ticket/**", "/index") .hasRole("ROLE_USER");
@H_502_7@在上面的示例中,所有与 / api / 或 / api / ticket / ”) 将整个HttpSecurity实例的范围限制为该特定的AntMatcher。
下面的示例将确保HttpSecurity的范围包括之前的三个AntMatchers,并且仅包含其他内容:
http .requestMatchers() .antMatchers("/api/user/**", "/api/ticket/**", "/index") .and() .httpBasic() .disable() .authorizeRequests() .any() .hasRole("ROLE_USER");
@H_502_7@解决方法
如果我们只需要保护一条这样的路径:
http.antMatcher("/api/**").authorizeRequests()....
然后使用
antMatcher()
。如果我们需要像这样保护多个URL路径:
http .authorizeRequests() .antMatchers("/high_level_url_A/sub_level_1").hasRole('USER') .antMatchers("/high_level_url_A/sub_level_2").hasRole('USER2') ...
然后使用
antMatchers()
。