用资源和操作绑定角色,角色绑定用户和操作
对应
两两绑定需要中间表来绑定
@RestController
public class UserAuthApi {
@Autowired
private UserSupport userSupport;
@Autowired
private UserAuthService userAuthService;
@GetMapping("/user-authorities")
public JsonResponse<UserAuthorities> getUserAuthorities(){
Long userId = userSupport.getCurrentUserId();
UserAuthorities userAuthorities = userAuthService.getUserAuthorities(userId);
return new JsonResponse<>(userAuthorities);
}
}
用于查用户的权限
用户权限服务,需要用到 用户-角色服务 和权限服务
@Service
public class UserAuthService {
@Autowired
private UserRoleService userRoleService;
@Autowired
private AuthRoleService authRoleService;
public UserAuthorities getUserAuthorities(Long userId) {
List<UserRole> userRoleList = userRoleService.getUserRoleByUserId(userId);
Set<Long> roleIdSet = userRoleList.stream().map(UserRole :: getRoleId).collect(Collectors.toSet());
List<AuthRoleElementOperation> roleElementOperationList = authRoleService.getRoleElementOperationsByRoleIds(roleIdSet);
List<AuthRoleMenu> authRoleMenuList = authRoleService.getAuthRoleMenusByRoleIds(roleIdSet);
UserAuthorities userAuthorities = new UserAuthorities();
userAuthorities.setRoleElementOperationList(roleElementOperationList);
userAuthorities.setRoleMenuList(authRoleMenuList);
return userAuthorities;
}
public void addUserDefaultRole(Long id) {
UserRole userRole = new UserRole();
AuthRole role = authRoleService.getRoleByCode(AuthRoleConstant.ROLE_LV0);
userRole.setUserId(id);
userRole.setRoleId(role.getId());
userRoleService.addUserRole(userRole);
}
}
@Service
public class UserRoleService {
@Autowired
private UserRoleDao userRoleDao;
public List<UserRole> getUserRoleByUserId(Long userId) {
return userRoleDao.getUserRoleByUserId(userId);
}
public void addUserRole(UserRole userRole) {
userRole.setCreateTime(new Date());
userRoleDao.addUserRole(userRole);
}
}
@Service
public class AuthRoleService {
@Autowired
private AuthRoleDao authRoleDao;
@Autowired
private AuthRoleElementOperationService authRoleElementOperationService;
@Autowired
private AuthRoleMenuService authRoleMenuService;
public List<AuthRoleElementOperation> getRoleElementOperationsByRoleIds(Set<Long> roleIdSet) {
return authRoleElementOperationService.getRoleElementOperationsByRoleIds(roleIdSet);
}
public List<AuthRoleMenu> getAuthRoleMenusByRoleIds(Set<Long> roleIdSet) {
return authRoleMenuService.getAuthRoleMenusByRoleIds(roleIdSet);
}
public AuthRole getRoleByCode(String code) {
return authRoleDao.getRoleByCode(code);
}
}
AuthRoleService中同样也需要两个服务 ,一个是页面元素服务一个是操作按钮服务来查当前用户是不是有点击按钮或者获取元素的权限