0

I'm using spring security in my project. I have a service as follow:

public interface A {

   @PostFilter("hasPermission(filterObject, 'read')")
   List<MyEntity> method1();

   @PostFilter("hasPermission(filterObject, 'read')")
   List<MyEntity> method2();
}

In Implementation method1() I use method2(), But PostFilter in method2() don't work in this state.

Why?

4

1 回答 1

3

你的观察是正确的。

为了处理安全注释,Spring 使用代理。代理是一个动态生成的类,位于调用者和实际实现之间。因此,当您使用接口 A 时,您实际上并没有直接调用您的实现,而是一个安全层。

默认情况下,Spring 使用接口代理;代理实现有问题的接口。这意味着仅当您使用 A作为接口时才调用安全性。当从实现类本身调用方法时,不会强制执行安全性,因为实现不知道代理。

通过使用类代理,安全注释可以在从类本身调用方法时起作用,因为代理扩展了实现。但是,仍然只有公共方法上的注释有效。

有关代理的更深入解释,请参阅Spring 框架手册中的代理机制

于 2016-04-25T07:01:00.670 回答