有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用SpringAOP实现基于注释的安全性?

我是Spring AOP(以及一般的AOP)的新手,需要实现以下内容:

@HasPermission(operation=SecurityOperation.ACTIVITY_EDIT, object="#act")
public Activity updateActivity(Activity act)
{
   ...
}

@HasPermission是我的自定义批注,用于标记所有需要预授权的方法。我正在使用基于ApacheShiro的自定义安全检查实现。通常,我想我需要定义与所有带注释的方法相匹配的切入点,并提供方面的实现(在之前或之后)

我的问题是重复的。方面实现

  • 如何从注释中提取操作对象参数
  • 如何解析对象定义中的SpEL表达式并将对象作为“act”参数传递

共 (1) 个答案

  1. # 1 楼答案

    我知道这是一个迟来的答案,但在我们将一些JavaEE项目迁移到Spring之后,我们基于AspectJ建立了一些基本的安全模型:

    首先,我们使用自定义@OperationAuthorization注释我们的服务方法:

    @OperationAuthorization
    public ListOfUserGroupsTo getUserGroupsByClientId(Integer clientId) throws GenericException {
        return userGroupRepository.getAllUserGroupsForClient(clientId);
    }
    

    然后我们有一个具有@Aspect&@Component注释,用于截取具有特定注释的方法:

    @Aspect 
    @Component
    public class AuthorizationAspect {
    
    @Autowired
    AuthorizationService authorizationService;
    
    @Before(value = "@annotation(ch.avelon.alcedo.authorization.annotations.OperationAuthorization)")
    public void before(JoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
    
        authorizationService.checkOperationAuthorization(method, args);
    }
    

    授权服务中,传递一个包含所有参数的方法。检查客户端是否有权获取用户组。如果不是:抛出异常,方法停止