Spring Aop+自定义注解对部分接口结果处理

需求:用户隐私配置,打开则把相关界面展示的手机号码中间四位用*替换,关闭则不隐藏

如针对A、B、C接口后置处理

1.自定义注解

/**
 * 号码隐藏注解
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MaskPhone {
}

2.aop后置处理

@Aspect
@Component
@Slf4j
public class CallMaskPhoneAspect {



    @AfterReturning(pointcut = "@annotation(maskPhone)", returning = "result")
    public Object afterReturningAdvice(MaskPhone maskPhone, Object result) {

//        GlobalConfig globalConfig = globalConfigService.getGlobalConfig(UserInfoContext.getSid());

//        if (globalConfig != null && globalConfig.getHide()==1) {
//        }

            // 对返回结果进行加工处理
            if (result instanceof PageInfoDTO) {

                PageInfoDTO pageInfoDTO = (PageInfoDTO) result;
                List<Object> data = pageInfoDTO.getData();
                for (Object o : data) {
                    // 假设需要处理 phone 字段

                    if (o instanceof CallOutRecord) {
                        CallOutRecord callOutRecord = (CallOutRecord) o;
                        String calleeNumber = callOutRecord.getCalleeNumber();
                        if (StringUtils.isNotBlank(calleeNumber)) {
                            String maskedPhone = maskPhone(callOutRecord.getCalleeNumber());
                            callOutRecord.setCalleeNumber(maskedPhone);
                        }
                        String callerNumber = callOutRecord.getCallerNumber();
                        if (StringUtils.isNotBlank(callerNumber)) {
                            String maskedPhone = maskPhone(callOutRecord.getCallerNumber());
                            callOutRecord.setCallerNumber(maskedPhone);
                        }
                    }else if (o instanceof CallPlan) {
                        CallPlan callPlan = (CallPlan) o;
                        String customerPhone = callPlan.getCustomerPhone();
                        if (StringUtils.isNotBlank(customerPhone)) {
                            String maskedPhone = maskPhone(customerPhone);
                            callPlan.setCustomerPhone(maskedPhone);
                        }

                    }else if (o instanceof CallTaskCustomer) {
                        CallTaskCustomer callTaskCustomer = (CallTaskCustomer) o;
                        String customerPhone = callTaskCustomer.getCustomerPhone();
                        if (StringUtils.isNotBlank(customerPhone)) {
                            String maskedPhone = maskPhone(customerPhone);
                            callTaskCustomer.setCustomerPhone(maskedPhone);
                        }


                    }

                }
                return pageInfoDTO; // 返回处理后的对象
            }else if (result instanceof Result) {
                Result ans = (Result) result;
                Object data = ans.getData();
                if (data instanceof CallOutRecord) {
                    CallOutRecord callOutRecord = (CallOutRecord)ans.getData();
                    String calleeNumber = callOutRecord.getCalleeNumber();
                    if (StringUtils.isNotBlank(calleeNumber)) {
                        String maskedPhone = maskPhone(callOutRecord.getCalleeNumber());
                        callOutRecord.setCalleeNumber(maskedPhone);
                    }
                    String callerNumber = callOutRecord.getCallerNumber();
                    if (StringUtils.isNotBlank(callerNumber)) {
                        String maskedPhone = maskPhone(callOutRecord.getCallerNumber());
                        callOutRecord.setCallerNumber(maskedPhone);
                    }
                }else if (data instanceof List){

                    List<MyCallTask> list = (List<MyCallTask>) data;
                    for (MyCallTask myCallTask : list) {
                        List<CallTaskCustomer> customerList = myCallTask.getCustomerList();
                        if (CollectionUtil.isNotEmpty(customerList)){
                            customerList.forEach(c->{
                                String maskedPhone = maskPhone(c.getCustomerPhone());
                                c.setCustomerPhone(maskedPhone);
                            });
                        }
                    }

                }

                return ans;
            }


        return result; // 返回原始结果
    }


    private String maskPhone(String phone) {
        if (phone.length() < 7) {
            return phone; // 如果长度小于7,返回原始号码
        }
        return phone.substring(0, 3) + "****" + phone.substring(7);

    }
}

3.在需要校验的接口加上注解

首先,我们需要定义一个自定义注解 `@RequiresPermissions`,用于标识需要授权访问的方法,例如: ```java @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface RequiresPermissions { String[] value(); // 权限值 } ``` 然后,我们需要实现一个切面,用于拦截被 `@RequiresPermissions` 标识的方法,并进行权限校验,例如: ```java @Component @Aspect public class PermissionCheckAspect { @Autowired private AuthService authService; @Around("@annotation(requiresPermissions)") public Object checkPermission(ProceedingJoinPoint joinPoint, RequiresPermissions requiresPermissions) throws Throwable { // 获取当前用户 User user = authService.getCurrentUser(); if (user == null) { throw new UnauthorizedException("用户未登录"); } // 获取当前用户的权限列表 List<String> permissions = authService.getUserPermissions(user); // 校验权限 for (String permission : requiresPermissions.value()) { if (!permissions.contains(permission)) { throw new ForbiddenException("没有访问权限:" + permission); } } // 执行目标方法 return joinPoint.proceed(); } } ``` 在切面中,我们首先通过 `AuthService` 获取当前用户及其权限列表,然后校验当前用户是否拥有被 `@RequiresPermissions` 标识的方法所需的所有权限,如果没有则抛出 `ForbiddenException` 异常,如果有则继续执行目标方法。 最后,我们需要在 Spring 配置文件中启用 AOP 自动代理,并扫描切面所在的包,例如: ```xml <aop:aspectj-autoproxy /> <context:component-scan base-package="com.example.aspect" /> ``` 这样,我们就通过 Spring AOP自定义注解模拟实现了类似 Shiro 权限校验的功能。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值