AOP 与 Spring 安全性

AOP vs Spring Security

下面这些有什么区别...

@org.aspectj.lang.annotation.Aspect
public class Test {
   //@Pointcut, @Around etc etc..
}

public aspect Test {

}

为了安全使用什么比较好..

在 spring 应用中

勾选here

Aspect declarations are supported by the org.aspectj.lang.annotation.Aspect annotation. The declaration:

 @Aspect
 public class Foo {}

Is equivalent to:

 public aspect Foo {}

注意:第一个被spring检测到。后者需要AspectJ。

第二个问题。比较是不可能的。因为前者是框架,后者是范式。 Spring 安全性使用 AOP 来保护方法调用,AOP 本身并不是一种安全机制。当然,除非您打算使用 AOP 来构建自己的安全性,这是重新发明轮子。

@Aspect 注释标记您的 class Test 将使您的 class 成为一个方面,意味着 Spring 的 ApplicationContextBeanFactory 现在将读取并扫描您的 class 以查找 advicespointcutsjoinpoints,您可以定义额外的 cross-cutting functinality(您保留的功能与您的业务逻辑分开),您希望为某些事件执行,例如方法调用之前、方法调用之后、方法抛出异常之后等。

AOP (Aspect-oriented programming) 是 spring 遵循的设计模式,可为您提供横切功能,而 Spring Security 是完整 spring 框架的一部分,您可以使用它来保护您的应用程序

Spring 安全性在内部使用 AOP 和过滤器来完成它的工作。

第一个是注释式语法,Spring AOP 和纯 AspectJ 都可以使用,第二个是本机 AspectJ 语法,仅适用于纯 AspectJ,不适用于基于代理的 Spring AOP。但是纯 AspectJ(由 AspectJ 编译器编译)也可以合并到 Spring 项目中,如 Spring 手册第 Using AspectJ with Spring applications.

章中所述

至于哪种方法更适合实现安全性,只要您不详细描述您的用例,这是一个哲学问题,但我的一般建议是:如果您使用 Spring 框架反正也用它的安全机制。如果你只考虑使用 Spring 因为你想使用 Spring AOP,我宁愿建议你远离仅用于单一目的的大型框架,并在你的 POJO 应用程序中使用纯 AspectJ 来实现安全。