? ? ? 开发的项目中,可能遇到不同的角色,不同的角色有不通的权限定义。AOP切面是个很好的解决方案。
1.?定义MerchRoles?
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MerchRoles {
}
2.?定义切点
public class MerchAop {
@Autowired
private DtoMerchGroupMapper merchGroupMapper;
@Pointcut("@annotation(com.ruoyi.shop.api.aop.MerchRoles)")
private void permissionCheck() {}
@Around("permissionCheck()")
public Object around(ProceedingJoinPoint p) throws Throwable{
WxLoginUser user=(WxLoginUser) getAuthentication().getPrincipal();
//建议采用redis缓存方案,更好
if(BooleanUtil.isFalse(getUserPermissions(user))){
return AjaxResult.warn("无权访问");
}
Map<String, Object> response = (Map<String, Object>) p.proceed();
return response;
}
private Boolean getUserPermissions(WxLoginUser user) {
//建议采用redis缓存方案,更好
MPJLambdaWrapper<MerchGroupDto> wrapper = new MPJLambdaWrapper<MerchGroupDto>()
.selectAll(MerchGroupDto.class)
.eq(MerchGroupDto::getMemberId, user.getUserId());
List<MerchGroupDto> list1=merchGroupMapper.selectJoinList(MerchGroupDto.class, wrapper);
if(CollUtil.size(list1)>0){
return true;
}
return false;
}
}
3. controller控制器,就使用吧。