2016-05-12 152 views
1

如果多个配置类具有@EnableGlobalMethodSecurity注释,那么是否使用了一个注释?多个@EnableGlobalMethodSecurity注释

在弹簧启动应用程序中,有两个WebSecurityConfigurerAdapter的实例 - 一个注释为@EnableGlobalMethodSecurity(secured = true),另一个为@EnableGlobalMethodSecurity(prePostEnabled = true)。但到目前为止,我无法使@PreAuthorize注释生效。只有一个注释可以看到它正在被应用。 如

@Configuration 
@Order(Ordered.HIGHEST_PRECEDENCE) 
@EnableGlobalMethodSecurity(prePostEnabled=true) 
public class FirstConfigurer extends WebSecurityConfigurerAdapter { 
... 

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(securedEnabled = true) 
public class AnotherConfiguration extends WebSecurityConfigurerAdapter{ 
... 

是否弹簧安全支持多种配置类是与@EnableGlobalMethodSecurity注解?
有没有办法查看实际配置的内容?

回答

0

我调试它(​​进入springframework源),看看它正在加载和以什么顺序。

我有一个类似的情况,我有一个库(我无法更改),配置@EnableGlobalMethodSecurity(securedEnabled = true)

,我想用@EnableGlobalMethodSecurity(prePostEnabled=true)

发生了什么这是该库的配置被加载在我的配置(只有最后一个被载入将被使用)

我试图用@Order(Ordered.LOWEST_PRECEDENCE)@EnableGlobalMethodSecurity(order = Ordered.HIGHEST_PRECEDENCE, ...但没有效果。

我的解决办法是通过我的配置是这样导入库配置设置的顺序,我想:

@Import(LibraryConfig.class) 
@EnableGlobalMethodSecurity(prePostEnabled=true) 
public class MyConfiguration extends GlobalMethodSecurityConfiguration { 
    /* my code */ 
} 

PLUS:我发现我的情况更复杂,因为图书馆是我使用配置@EnableGlobalMethodSecurity(securedEnabled = true)不止一次。 因此,行为有点不同,为了强制使用我的配置,我不得不重写此方法的实现:

@Bean 
public MethodSecurityMetadataSource methodSecurityMetadataSource() { 
    return super.methodSecurityMetadataSource(); 
} 

@Bean 
public MethodInterceptor methodSecurityInterceptor() throws Exception { 
    return super.methodSecurityInterceptor(); 
} 

@Bean 
public PreInvocationAuthorizationAdvice preInvocationAuthorizationAdvice() { 
    return super.preInvocationAuthorizationAdvice(); 
}