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

来源:互联网 发布:c语言单引号字符 编辑:程序博客网 时间:2024/05/17 02:37

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