Backend
Sa-Token necessary implement StpInterface
YYT-0901 DEV Community
23 views
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") // 必须是这个角色
Read original: https://dev.to/yyt0901/sa-token-necessary-implement-stpinterface-16m4
← Previous
🔥 The Imposter’s Tax: Why Hiding Your Wins Is Secretly Hurting Everyone
Next →
Why CAPTCHAs Are Dead (And What Replaces Them in 2026)
Related
The benchmark everyone cited for "fastest backend" was archived in March, and the reason is the interesting part
Backend
56
Dev.to (EN Zone)
Finding Exoplanets in Noisy Data with Machine Learning
Backend
46
DEV Community
How to automatically find the batch size when using Accelerate with FSDP2? [D]
Backend
43
Reddit r/MachineLearning
Cross-Chain Bridge Risk Assessment: Aave V3
Backend
36
DEV Community
Comments0
No comments yet — be the first