apringboot aop权限控制

定义切面:

@Aspect
@Component
public class LoginInterceptor {

    @Around("@annotation(lock)")
    public Object around(ProceedingJoinPoint pjp, Lock lock) {
        //获取注解里的值
        System.out.println("in around");
        try {
            return pjp.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return null;
        }
    }

    @Pointcut("execution(public * com.lx.activemq.controller.*.*(..))")
    public void checkToken(){

        System.out.println("aop 拦截器");
    }

    @Around("checkToken()")
    public ResponseMessage validateRole(ProceedingJoinPoint pjp) throws Throwable{
        System.out.println("aop 拦截器");
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();
        Enumeration<String> enumeration = request.getHeaderNames();
        request.getParameterNames();
        StringBuffer headers = new StringBuffer();
        while (enumeration.hasMoreElements()) {
            String name = enumeration.nextElement();
            String value = request.getHeader(name);
           System.out.println(name + ":" + value);
        }
        if(request.getParameter("flag").equals("0")){
            pjp.proceed();
        }
        return new ResponseMessage().noAccess("没有登录权限");
    }
}
posted @ 2019-06-13 18:16  luck-monkey  阅读(345)  评论(0编辑  收藏  举报