spring Security 的requestMatchers方法

requestMatchers() 取得RequestMatcherConfigurer对象并配置允许过滤的路由;如requestMatchers().anyRequest()等同于http.authorizeRequests().anyRequest().access(“permitAll”);如下面两个例子:

	@Override
	public void configure(HttpSecurity http) throws Exception {
		
//只允许路由由test开头的需要进行权限认证,其他的接口不需要权限认证;requestMatchers().anyRequest()即所有接口可以不进行认证;	
	http.requestMatchers().anyRequest().and().authorizeRequests().antMatchers("/test/*").authenticated();
 
	}
	@Override
	public void configure(HttpSecurity http) throws Exception {
		//只有以/test 开头的路由需要进行权限认证;其他路由不需要权限认证
		http.requestMatchers().antMatchers("/test/**").and().authorizeRequests().antMatchers("/**").authenticated();
 
	}

上面两个例子中可以看出:http.requestMatchers().anyRequest().and().authorizeRequests().antMatchers("/test/").authenticated();配置和http.requestMatchers().antMatchers("/test/").and().authorizeRequests().antMatchers("/**").authenticated();配置后的效果是完全一样的。

authorizeRequests()
授权管理控制的方法,这个方法返回一个ExpressionUrlAuthorizationConfigurer.ExpressionInterceptUrlRegistry对象。Security所有的权限控制都基于这个类进行控制。如:http.authorizeRequests().anyRequest().authenticated();要求所有接口都需要进行权限认证,这个类中的anyRequest()即所有接口,等同于http.authorizeRequests().antMatchers("/").authenticated(); 而其中的authenticated()则要求必须进行权限认证。而http.authorizeRequests().antMatchers("/").permitAll();这个配置中permitAll方法则要求所有接口都不需要进行权限认证。另外这两个代码中antMatchers方法则是配置匹配规则。即哪些接口需要进行权限认证或不需要进行权限认证。

		//所有接口都不需要权限认证
		http.authorizeRequests().antMatchers("/**").permitAll();
		//所有接口都要进行权限认证
		http.authorizeRequests().antMatchers("/**").authenticated();
		//只有以test开头的接口需要进行权限认证
		http.authorizeRequests().antMatchers("/test/**").authenticated();
		http.authorizeRequests().antMatchers("/test/**").hasRole("user").antMatchers("/**").authenticated();

在这个代码中要求以/test开头的路由需要进行角色认证(这里要求user角色),而其他接口只要登录即可访问。当我们需要配置多个角色时可以通过hasAnyRole方法配置多个角色的访问权限,如

http.authorizeRequests().antMatchers("/test/**").hasAnyRole("user","admin").antMatchers("/**").authenticated();
 
### 如何在Spring Boot集成Spring Security时禁用OPTIONS HTTP方法 为了实现这一目标,在配置类中可以通过重写`configure(HttpSecurity http)`方法来调整HTTP安全设置。具体来说,可以利用`HttpMethodOptionsMatchProcessor`或者直接通过`.httpBasic().and().authorizeRequests()`链式调用来指定哪些请求应该被授权以及如何处理特定类型的HTTP请求。 下面展示了一个具体的例子: ```java @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.cors().and() .csrf().disable() // 如果不需要CSRF保护可关闭它 .authorizeRequests() .antMatchers(HttpMethod.OPTIONS, "/**").permitAll() // 允许所有OPTIONS请求访问任何路径 .anyRequest().authenticated(); // 所有其他请求都需要认证 // 或者完全移除对OPTIONS的支持如下所示: /*http.cors().and() .csrf().disable() .authorizeRequests() .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll() .mvcMatchers("/api/**").hasRole("USER") // 对API端点应用角色检查 .and() .httpBasic() .and() .exceptionHandling().defaultAuthenticationEntryPointFor( new HttpStatusEntryPoint(HttpStatus.METHOD_NOT_ALLOWED), new AntPathRequestMatcher("**", HttpMethod.OPTIONS.toString()));*/ } } ``` 上述代码片段展示了两种方式:一种是允许所有的`OPTIONS`预检请求;另一种则是当接收到`OPTIONS`请求时返回`METHOD_NOT_ALLOWED (405)`状态码[^1]。 对于更复杂的场景,比如与前端框架Vue.js交互时可能遇到跨域资源共享(CORS)问题,确保适当配置CORS过滤器也很重要。由于Vue专注于视图层开发[^3],通常后端需要负责提供必要的响应头以便于前后端分离架构下的正常通信。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值