publish 2.0.0.rc18
This commit is contained in:
@@ -1,76 +0,0 @@
|
||||
package com.tiesheng.role.config;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.tiesheng.annotation.role.RoleAuthority;
|
||||
import com.tiesheng.util.ServletKit;
|
||||
import com.tiesheng.util.config.TsTokenConfig;
|
||||
import com.tiesheng.util.exception.ApiException;
|
||||
import com.tiesheng.util.pojos.TokenBean;
|
||||
import com.tiesheng.util.service.TsCacheService;
|
||||
import com.tiesheng.util.service.role.TsAuthorityHandler;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Aspect
|
||||
@Component
|
||||
public class RoleAuthorityAspect {
|
||||
|
||||
public static final String CACHE_HAS_AUTHORITY = "CACHE:HAS_AUTHORITY:{}:{}";
|
||||
|
||||
@Resource
|
||||
TsTokenConfig tsTokenConfig;
|
||||
@Resource
|
||||
TsAuthorityHandler tsAuthorityHandler;
|
||||
|
||||
|
||||
/**
|
||||
* 获取
|
||||
*/
|
||||
@Before("execution(* com..controller..*.*(..))")
|
||||
public void before(JoinPoint joinPoint) {
|
||||
|
||||
RoleAuthority classAnnotation = joinPoint.getTarget().getClass().getAnnotation(RoleAuthority.class);
|
||||
if (classAnnotation == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
HttpServletRequest request = ServletKit.getRequest();
|
||||
TokenBean tokenBean = tsTokenConfig.validToken(request, true);
|
||||
String cacheKey = StrUtil.format(CACHE_HAS_AUTHORITY, tokenBean.getRoleId(), tokenBean.getId());
|
||||
List<String> authorityList = StrUtil.split(TsCacheService.of().get(cacheKey), ";")
|
||||
.stream().filter(StrUtil::isNotEmpty).collect(Collectors.toList());
|
||||
if (CollUtil.isEmpty(authorityList)) {
|
||||
authorityList = tsAuthorityHandler.getAuthorities(tokenBean);
|
||||
if (CollUtil.isNotEmpty(authorityList)) {
|
||||
TsCacheService.of().put(cacheKey, StrUtil.join(";", authorityList));
|
||||
}
|
||||
}
|
||||
|
||||
String authority = StrUtil.join("_", classAnnotation.group(), classAnnotation.value());
|
||||
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||
RoleAuthority annotation = signature.getMethod().getAnnotation(RoleAuthority.class);
|
||||
if (annotation != null) {
|
||||
// 检查是否是功能点的权限
|
||||
if (CollUtil.contains(authorityList, StrUtil.join("_", authority, annotation.value()))) {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
// 检查是否是菜单级别的权限
|
||||
if (CollUtil.contains(authorityList, authority)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
throw new ApiException(403, "您无权访问");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,7 +7,6 @@ import cn.hutool.core.lang.Validator;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.tiesheng.annotation.role.RoleAuthority;
|
||||
import com.tiesheng.role.config.RoleAuthorityAspect;
|
||||
import com.tiesheng.role.mapper.*;
|
||||
import com.tiesheng.role.pojos.dao.CoreRoleAuthority;
|
||||
import com.tiesheng.role.pojos.dao.CoreRoleGroup;
|
||||
@@ -162,7 +161,7 @@ public class CoreRoleService extends TsServiceBase<CoreRoleGroupMapper, CoreRole
|
||||
* 当授权发生变化时
|
||||
*/
|
||||
public void onRoleChange(String roleId, String userId) {
|
||||
TsCacheService.of().keys(StrUtil.format(RoleAuthorityAspect.CACHE_HAS_AUTHORITY,
|
||||
TsCacheService.of().keys(StrUtil.format(TsAuthorityHandler.CACHE_HAS_AUTHORITY,
|
||||
roleId, userId)).forEach(key -> TsCacheService.of().remove(key));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user