sql table reference: RBAC SQL table create
There has two function is must be override, which is getPermissionList and getRoleList, because sa-token unknown your structure of SQL. So, you want to tell it how to get your PermissionList and RoleList.
1. StpInterfaceImpl
@Component
public class StpInterfaceImpl implements StpInterface {
@Autowired
private SysPermissionService sysPermissionService;
@Autowired
private SysRoleService sysRoleService;
@Override
public List<String> getPermissionList(Object loginId, String loginType) {
Long userId = Long.valueOf(loginId.toString());
return sysPermissionService.getPermissionCodesByUserId(userId);
}
@Override
public List<String> getRoleList(Object loginId, String loginType) {
Long userId = Long.valueOf(loginId.toString());
return sysRoleService.getRoleCodesByUserId(userId);
}
}
2. SysPermissionServiceImpl & SysRoleServiceImpl
@Override
public List<String> getPermissionCodesByUserId(Long userId) {
return this.baseMapper.selectPermissionCodesByUserId(userId);
}
@Override
public List<String> getRoleCodesByUserId(Long userId) {
return this.baseMapper.selectRoleCodesByUserId(userId);
}
3. SysPermissionMapper.xml & SysRoleMapper.xml
<select id="selectPermissionCodesByUserId" resultType="java.lang.String" parameterType="java.lang.Long">
SELECT DISTINCT p.permission_code
FROM sys_user_role ur
JOIN sys_role r ON r.id = ur.role_id AND r.status = 1 AND r.deleted = 0
JOIN sys_role_permission rp ON rp.role_id = r.id
JOIN sys_permission p ON p.id = rp.permission_id AND p.status = 1 AND p.deleted = 0
WHERE ur.user_id = #{userId}
</select>
<select id="selectRoleCodesByUserId" resultType="java.lang.String" parameterType="java.lang.Long">
SELECT DISTINCT r.role_code
FROM sys_user_role ur
JOIN sys_role r ON r.id = ur.role_id
WHERE ur.user_id = #{userId}
AND r.status = 1
AND r.deleted = 0
</select>
After that, Interface authentication becomes very simple:
@SaCheckLogin // 必须登录
@SaCheckPermission("food:add") // 必须有这个权限码
@SaCheckRole("ROLE_VIP") // 必须是这个角色
Top comments (0)