注解方式实现aop权限管理

来源:互联网 发布:ubuntu桌面菜单不见了 编辑:程序博客网 时间:2024/05/21 19:38

一个切面类,一个自定义注解接口

自定义注解接口:

@Target({ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documented@Inheritedpublic @interface LoginAnnotation {}

切面类:

@Aspect//切面类@Configuration //springboot配置类public class LoginAspect{}

在切面类中配置切点,以及环绕(在切面中执行的方法)

切点:

    @Pointcut("@annotation(com..*.LoginAnnotation)")    public void controllerLoginAspect(){    }

环绕:

    @Around("controllerLoginAspect()&&@annotation(loginAnnotation)")    public Object controllerAround(ProceedingJoinPoint joinPoint, LoginAnnotation loginAnnotation) throws Throwable{        Object result;        String userToken="";        //userToken从request的header取        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();        HttpServletRequest request = attributes.getRequest();        userToken=request.getHeader("userToken");        if (userToken == null) {            return ResultDtoFactory.toFailure(00000001, new String("没有权限"));        }        Object obj= service层.isLogin(userToken);                if(obj!=null){            //有权限            doSomething;            result=joinPoint.proceed();        }else{            result=ResultDtoFactory.toFailure(00000001, new String("没有权限"));        }        return result;    }
0 0
原创粉丝点击