鉴权注解@SaCheckPermission和@SaCheckRole
校验注解的内容
以ruoyi-vue-plus的一个接口为例
/**
* 导出租户列表
*/
// SUPER_ADMIN_ROLE_KEY = "superadmin";
@SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
@SaCheckPermission("system:tenant:export")
@Log(title = "租户", businessType = BusinessType.EXPORT)
@PostMapping("/export")
public void export(SysTenantBo bo, HttpServletResponse response) {
List<SysTenantVo> list = tenantService.queryList(bo);
ExcelUtil.exportExcel(list, "租户", SysTenantVo.class, response);
}
@SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)与@SaCheckPermission(“system:tenant:export”)是如何起作用的?
只需实现StpInterface接口
StpInterface接口
SaToken配置类
/**
* sa-token 配置
*
* @author Lion Li
*/
@AutoConfiguration
// 它的作用是告诉 Spring 去加载指定路径下的属性源文件,并且使用指定的工厂类 YmlPropertySourceFactory 来解析这个属性源文件
@PropertySource(value = "classpath:common-satoken.yml", factory = YmlPropertySourceFactory.class)
public class SaTokenConfig {
@Bean
public StpLogic getStpLogicJwt() {
// Sa-Token 整合 jwt (简单模式)
return new StpLogicJwtForSimple();
}
/**
* 权限接口实现(使用bean注入方便用户替换)
*/
@Bean
public StpInterface stpInterface() {
return new SaPermissionImpl();
}
/**
* 自定义dao层存储
*/
@Bean
public SaTokenDao saTokenDao() {
return new PlusSaTokenDao();
}
}
注入StpInterface
当我们实现StpInterface接口后,SaToken会通过set注入的方式把SaPermissionImpl注入到容器中。这是因为satoken配置类中我们返回了一个SaPermissionImpl的bean。
SaPermissionImpl
/**
* sa-token 权限管理实现类
*
* @author Lion Li
*/
public class SaPermissionImpl implements StpInterface {
/**
* 获取菜单权限列表
* 对应@SaCheckPermission
*/
@Override
public List<String> getPermissionList(Object loginId, String loginType) {
LoginUser loginUser = LoginHelper.getLoginUser();
UserType userType = UserType.getUserType(loginUser.getUserType());
if (userType == UserType.SYS_USER) {
// 返回当前用户的所有权限(对应@SaCheckPermission)
return new ArrayList<>(loginUser.getMenuPermission());
} else if (userType == UserType.APP_USER) {
// 其他端 自行根据业务编写
}
return new ArrayList<>();
}
/**
* 获取角色权限列表
* 对应@SaCheckRole
*/
@Override
public List<String> getRoleList(Object loginId, String loginType) {
LoginUser loginUser = LoginHelper.getLoginUser();
UserType userType = UserType.getUserType(loginUser.getUserType());
if (userType == UserType.SYS_USER) {
// 返回当前用户的所有权限(对应@SaCheckRole)
return new ArrayList<>(loginUser.getRolePermission());
} else if (userType == UserType.APP_USER) {
// 其他端 自行根据业务编写
}
return new ArrayList<>();
}
}
当我们访问这个接口时,会先经过getRoleList()方法和getPermissionList()方法。也就是校验权限
鉴权注解@SaCheckPermission和@SaCheckRole
http://example.com/2024/05/17/鉴权注解@SaCheckPermission和@SaCheckRole/