0

我是 Spring AOP 的新手,正在努力实现以下目标。

我想定义一个注释@RequirePermission,它接受参数字符串或枚举,并基于该字符串/枚举我将在建议中进行一些计算。因此,应首先验证 Controller 中定义的具有 @RequiredPermissions 的任何方法。到目前为止,这是我的代码。

方面:

package com.myapp.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import com.myapp.security.RequirePermissionType;
import com.myapp.security.RequirePermission;
@Aspect
public class RequirePermissionAspect {

    @Pointcut("within(@org.springframework.stereotype.Controller *)")
    public void controllerBean() {}

    @Pointcut("execution(@com.myapp.security.RequirePermission * com.myapp.controller.*.*(..))")
    public void methodPointcut() {}

    @Before("controllerBean() && methodPointcut() ")
    public void afterMethodInControllerClass(com.myapp.security.RequirePermissionType name) {
        System.out.println("before advice..");
        System.out.println("before advice.."+name.name());
    }

注解:

package com.myapp.security;

public enum RequirePermissionType {
    VIEW, MANAGE, IMPORT, SUPER;
}

@Documented
@Target(ElementType.METHOD )
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface RequirePermission {
    /**
     * 
     * @return
     */
    RequirePermissionType name() default RequirePermissionType.VIEW ;
}

现在,如果在控制器方法中使用注释,如下代码可以工作:

    @RequestMapping(value={"create"})
    @RequirePermission
    public String createRegion(Model model){
    }

但以下不起作用:

    @RequestMapping(value={"create"})
    @RequirePermission(name=RequirePermissionType.VIEW)
    public String createRegion(Model model){
    }

任何人都可以指导我缺少什么以及如何实现这一点。

4

1 回答 1

1

@vamslip 这里是更新的方面类。其余一切都保持不变。更新方面:

package com.myapp.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import com.myapp.security.RequirePermissionType;
import com.myapp.security.RequirePermission;
@Aspect
public class RequirePermissionAspect {

@Pointcut("within(@org.springframework.stereotype.Controller *)")
public void controllerBean() {}

@Pointcut("execution(@com.myapp.security.RequirePermission * com.myapp.controller.*.*(..)) && @annotation(name)") 
public void methodPointcut(com.myapp.security.RequirePermission name) {} 

@Before("controllerBean() && methodPointcut(name) ") 
public void afterMethodInControllerClass(com.myapp.security.RequirePermissionType name) { 
    System.out.println("before advice.."); 
    System.out.println("before advice.."+name.name()); 
}
}
于 2013-02-03T20:34:23.870 回答