Spring Security

@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.authorizeRequests().antMatchers("/**").permitAll();
//所有接口都要进行权限认证(authenticated()表示需要权限认证)
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();

posted @ 2021-11-02 17:12  DawnHouse  阅读(82)  评论(0)    收藏  举报