我正在尝试使用 AspectJ 建议模式配置 spring 全局方法安全性,以便我可以在所有 @Configurable
注释类中使用 @PreAuthorize
注释。这是我的 java 配置:
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, mode = AdviceMode.ASPECTJ)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration
和:
@EnableCaching
@SpringBootApplication
@EnableSpringConfigured
@EnableTransactionManagement(mode = AdviceMode.ASPECTJ)
@EnableLoadTimeWeaving(aspectjWeaving = EnableLoadTimeWeaving.AspectJWeaving.ENABLED)
public class WebApplication extends WebMvcConfigurerAdapter
这是我的@Configurable
类:
@Configurable
public class Entity {
@PreAuthorize("hasPermission(this, 'publish')")
public void method() { }
}
我还添加了 spring-security-aspects 作为依赖项。从 AspectJ 日志中,我可以清楚地看到 Spring 安全相关方面应用于我的 @Configurable
类,但是一旦我创建这些类的实例,我就会得到这个异常:
Post-processor tried to replace bean instance of type [com.example.Entity] with (proxy) object of type [com.sun.proxy.$Proxy130] - not supported for aspect-configured classes!
我使用的是 spring boot 版本 1.2.1 因此 spring security 版本是 3.2.5 。这似乎是这里讨论的错误:Spring Security AspectJMode with @EnableGlobalMethodSecurity not working
但是这个错误不应该影响我的 spring security 版本......这个问题有什么解决方法吗?
最佳答案
好的,我已经解决了这个问题。这是 spring boot 的 SecurityAutoConfiguration 类的问题。我不得不将它从自动配置中排除并手动配置 spring security - 没什么大不了的,但无论如何......
关于java - Spring boot + Spring security with AspectJ 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28658718/