角色授权管理可通过框架配置:
// 1.可以指定请求方法、路径匹配、所需角色
.antMatchers(HttpMethod.POST,"/user/*").hasRole("ADMIN")
// 如果配置hasRole,对应userDetailService授权时入参为"ROLE_ADMIN",因为在验证时框架会拼接"ROLE_"前缀,完成匹配
// 2.多个权限判断联合使用时可用权限表达式,如下,同时使用角色和IP地址授权:
.access("hasRole('ADMIN') and hasIpAddress('xxx')")
通用授权和项目授权拆分管理:
1.新建授权配置接口AuthorizeConfigProvider:
/**
* 授权配置接口
*
* @author zhaohaibin
*/
public interface AuthorizeConfigProvider {
/**
* authorizeRequests
*
* 后续方法调用基于authorizeRequests方法返回的对象,所以入参为authorizeRequests方法返回的对象
*
* @param config
*/
void config(ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry config);
}
2.新建授权配置接口实现——通用授权实现:
/**
* 授权配置接口实现
*
* @author zhaohaibin
*/
@Component
@Order(Integer.MIN_VALUE)
public class DemoAuthorizeConfigProvider implements AuthorizeConfigProvider {