HttpSecurity.antMatcher()将HttpSecurity实例的默认请求匹配器从AnyRequestMatcher
更改为AntPathRequestMatcher。expressionUrlAuthorizationConfigurer.expressionInterceptUrlRegistry.antMatchers()用于将授权规则应用于与当前HttpSecurity实例关联的端点的子集。
示例代码:
http .antMatcher("/api/**") .httpBasic() .disable() .authorizeRequests() .antMatchers("/api/user/**", "/api/ticket/**", "/index") .hasRole("ROLE_USER");
在上面的示例中,所有与 / api / 匹配的端点都禁用了基本授权。此外,与 / api / user / 或 / api /
ticket / 匹配的端点将要求请求的身份验证包含ROLE_USER。但是,当用户尝试访问 / index时_
,将出现基本身份验证提示。输入凭据后,无论请求的身份验证是否包含ROLE_USER,都将向用户授予对端点的访问权限。这是因为 _.antMatcher(“
/ api / ”) 将整个HttpSecurity实例的范围限制为该特定的AntMatcher。
下面的示例将确保HttpSecurity的范围包括之前的三个AntMatchers,并且仅包含其他内容:
http .requestMatchers() .antMatchers("/api/user/**", "/api/ticket/**", "/index") .and() .httpBasic() .disable() .authorizeRequests() .any() .hasRole("ROLE_USER");
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)