利用Spring的AOP拦截post方法

  • @Component 依赖注入
  • @Order(1)

注解@Order或者接口Ordered的作用是定义Spring IOC容器中Bean的执行顺序的优先级,而不是定义Bean的加载顺序,Bean的加载顺序不受@Order或Ordered接口的影响;

  • @Asoect

@Aspect:作用是把当前类标识为一个切面供容器读取
@Pointcut:Pointcut是植入Advice的触发条件。每个Pointcut的定义包括2部分,一是表达式,二是方法签名。方法签名必须是 public及void型。可以将Pointcut中的方法看作是一个被Advice引用的助记符,因为表达式不直观,因此我们可以通过方法签名的方式为 此表达式命名。因此Pointcut中的方法只需要方法签名,而不需要在方法体内编写实际代码。
@Around:环绕增强,相当于MethodInterceptor
@AfterReturning:后置增强,相当于AfterReturningAdvice,方法正常退出时执行
@Before:标识一个前置增强方法,相当于BeforeAdvice的功能,相似功能的还有
@AfterThrowing:异常抛出增强,相当于ThrowsAdvice
@After: final增强,不管是抛出异常或者正常退出都会执行

@Component
@Order(1)
@Aspect
public class RequestAop {
    private static final Logger logger = LoggerFactory.getLogger("RequestAop");

    @Around("execution(* com.alibaba.industryadminportal.web..*.*(..)) " +
            "&& !execution(* com.alibaba.industryadminportal.web.JsonpResponseBodyAdvice.*(..))" +
            "&& !execution(* com.alibaba.industryadminportal.web.epc.AdapterAuthManagerController.getOemBrand(..))" +
            "&& !execution(* com.alibaba.industryadminportal.web.epc.AdapterAuthManagerController.getItemSkuAdapter(..))" +
            "&& !execution(* com.alibaba.industryadminportal.web.epc.AdapterAuthManagerController.getItemSkuExt(..))")
    public Object doAroundMethod(ProceedingJoinPoint pig) throws Throwable {
        Object obj = null;
        try {
            if (!judgeIsGet()) {
                logger.info("aop post is not allow");
                return getResult(pig, obj);
            }
            obj = pig.proceed();
        } catch (Throwable e) {
            logger.error("aop is Exception " + ExceptionUtils.getStackTrace(e));
            throw e;
        }
        return obj;
    }

    /**
     * 判断是否为get
     *
     * @return
     */
    private boolean judgeIsGet() {
        if (!SwitchConfig.forbidRequest) {
            return true;
        }
        //request
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        if (request.getMethod().equalsIgnoreCase(RequestMethod.GET.name())) {
            return true;
        } else if (request.getMethod().equalsIgnoreCase(RequestMethod.POST.name())) {
            return false;
        } else {
            return true;
        }
    }

    private Object getResult(ProceedingJoinPoint pig, Object obj) throws NoSuchMethodException {
        Method method = ((MethodSignature) pig.getSignature()).getMethod();
        Class<?> returnType = pig.getTarget().getClass().getMethod(method.getName(), method.getParameterTypes()).getReturnType();
        if (Result.class.equals(returnType)) {
            obj = Results.newFailedResult(CommonStateCode.FAILED,SwitchConfig.requestForbidEssay);
        }
        return obj;
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值