java中统一方法进行切面管理

aop切面管理统一接口类:

(1)spring配置:


被切面监控的类需要放在pointcut的配置的路径下;

    <!-- AOP配置 -->
    <aop:config>
        <!-- 声明一个切面,并注入切面Bean,相当于@Aspect -->
        <aop:aspect id="httpInterceptor" ref="httpInterceptor">
            <!-- 配置通知,相当于@Before、@After、@AfterReturn、@Around、@AfterThrowing -->
            <aop:around pointcut="execution(public * com.jd.worldwide.http.service.c2c..*.*(..))" method="aroundMethod" />
            <aop:around pointcut="execution(public * com.jd.worldwide.http.service.interAuth.inner..*.*(..))"
                method="aroundMethod" />
            <aop:around pointcut="execution(public * com.jd.worldwide.http.service.faq..*.*(..))" method="aroundMethod" />
            <aop:around pointcut="execution(public * com.jd.worldwide.http.service.advert..*.*(..))" method="aroundMethod" />
        </aop:aspect>
        <aop:aspect id="jsfInterceptor" ref="jsfInterceptor">
            <!-- 配置通知,相当于@Before、@After、@AfterReturn、@Around、@AfterThrowing -->
            <aop:around pointcut="execution(public * com.jd.worldwide.jsf.service.c2c..*.*(..))" method="aroundMethod" />
            <aop:around pointcut="execution(public * com.jd.worldwide.jsf.service.clearance..*.*(..))"
                method="aroundMethod" />
            <aop:around pointcut="execution(public * com.jd.worldwide.jsf.service.detailPage..*.*(..))"
                method="aroundMethod" />
            <aop:around pointcut="execution(public * com.jd.worldwide.jsf.service.sop..*.*(..))"
                method="aroundMethod" />
            <aop:around pointcut="execution(public * com.jd.worldwide.jsf.service.order..*.*(..))"
                method="aroundMethod" />
        </aop:aspect>
    </aop:config>



(2)切面类


在切面中进行统一的类型返回,这个统一的类型作为父类被其他类的方法的返回类继承。在这个切面中可以实现简单的统一处理,包括方法监控,方法异常捕获和返回。真正执行目标方法时pjp.proceed(),pjp作为目标类执行的目标类的实例对象,执行目标方法。

@Component("jsfInterceptor")
public class JSFInterceptor {

    private Log log = LogFactory.getLog(this.getClass());

    @Autowired
    private InterfaceAuthDao interfaceAuthDao;

    private CheckUtil checkUtil;

    @Autowired
    private JdRedisUtils jdRedisUtils;

    // 这个可用来替代以后重复出现的. 直接在后面的Before("myMethod()")就行了.

    public Object aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
        // 获取目标对象class类型
        String targetClassName = pjp.getTarget().getClass().getName();
        // 接口名称
        String interfaceName = pjp.getTarget().getClass().getInterfaces()[0].getName();
        Signature signature = pjp.getSignature();
        // 获取目标方法名称
        String methodName = signature.getName();
        // 组装目标方法监控key
        String methodUMPKey = targetClassName + "." + methodName;
        CallerInfo functionInfo = Profiler.registerInfo((methodUMPKey), UmpProfilerConstant.SYS_NAME, false, true);
        // 对返回类型的顶层类型做强制控制
        Object result = null;
        MethodSignature methodSignature = (MethodSignature) signature;
        methodSignature.getMethod();
        NoAuthRequired noAuthRequired = methodSignature.getMethod().getAnnotation(NoAuthRequired.class);
        Class returnType = methodSignature.getReturnType();
        // 如果不需要校验权限或者权限校验通过,才能执行业务方法
        if (null != noAuthRequired || checkPrivilege(interfaceName)) {
            result = pjp.proceed();
        } else {
            result = returnType.newInstance();
            PropertyUtils.setProperty(result, "success", false);
            PropertyUtils.setProperty(result, "errorMsg", "权限错误");
        }
        Profiler.registerInfoEnd(functionInfo);
        return result;
    }

    protected boolean checkPrivilege(String targetClass) {
        if (!checkUtil.isJsfValid()) {
            return true;
        }
        EnumInterfaceKey enums = EnumInterfaceKey.getInterfaceBykey(targetClass);
        String token = (String) RpcContext.getContext().getAttachment("token");
        String appName = (String) RpcContext.getContext().getAttachment("appName");
        if (null == enums || null == token || null == appName ||
                enums.getInterfaceType() != EnumInterfaceType.JSF.getInterfaceType()) {
            log.error("token校验:appName为null或token为null");
            return false;
        }

        String redisKeyString = RedisKeys.WORLD_WIDE_C2C_PRIVILEGE_VALID + enums.getInterfaceType() + "_" +
                enums.getInterfaceKey() + "_" + appName + "_" + token;
        Boolean retValue = (Boolean) jdRedisUtils.getObject(redisKeyString);
        if (retValue != null) {
            return retValue;
        }
        retValue = false;
        try {
            InterfaceAuthQuery query = new InterfaceAuthQuery();
            query.setInterfaceType(enums.getInterfaceType());
            query.setInterfaceKey(enums.getInterfaceKey());
            query.setAppName(appName);
            List<InterfaceAuth> info = interfaceAuthDao.getInterfaceAuthByquery(query);
            if (null == info || info.size() <= 0) {
                log.error("获取权限认证列表为空");
            } else if (token.equals(info.get(0).getToken())) {
                retValue = true;
            }
            jdRedisUtils.setObjectByExpire(redisKeyString, retValue, RedisKeys.WORLD_WIDE_C2C_PRIVILEGE_VALID_TIME);
        } catch (Exception e) {
            log.error("获取jsf接口权限认证异常e=", e);
        }
        return retValue;
    }

    public CheckUtil getCheckUtil() {
        return checkUtil;
    }

    public void setCheckUtil(CheckUtil checkUtil) {
        this.checkUtil = checkUtil;
    }
}



在切面中执行公共的权限校验,异常捕获



    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值