spring mvc实现自定义注解
实现方式:使用@Aspect实现:
1.
新建注解接口:CheckSign
package com.soeasy.web.utils; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import java.lang.annotation.*; /** * Created by Administrator on 2017/10/25 0025. */ @Documented @Target({ElementType.METHOD}) //方法上 @Inherited @Retention(RetentionPolicy.RUNTIME) //运行时 @Order(Ordered.HIGHEST_PRECEDENCE) //最高优先级 public @interface CheckSign { }
2.
新建注解实现 CheckSignAspect 类:
package com.soeasy.web.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.soeasy.commons.FailException; import com.soeasy.utils.sign.Des3; import com.soeasy.utils.sign.SignConf; import org.apache.log4j.Logger; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; /** * 自定义注解@CheckSign的实现方法 * Created by Administrator on 2017/10/25 0025. */ @Aspect @Component public class CheckSignAspect { private Logger log = Logger.getLogger(this.getClass()); /** * 目标方法执行 * @param pjp */ @Around("@annotation(com.soeasy.web.utils.CheckSign)") //执行@接口 public Object beforeExec(ProceedingJoinPoint pjp) throws Throwable { //获取request信息 /*RequestAttributes ra = RequestContextHolder.getRequestAttributes(); ServletRequestAttributes sra = (ServletRequestAttributes)ra; HttpServletRequest request = sra.getRequest();*/ //获取post参数 for (int i = 0; i < pjp.getArgs().length; i++) { if(pjp.getArgs()[i] instanceof LinkedHashMap || pjp.getArgs()[i] instanceof JSONObject){ Map<String, Object> map = (Map<String,Object>) pjp.getArgs()[i]; } } return pjp.proceed(); } }