Java Spring安全配置@Order非唯一异常
我尝试在我的Spring安全配置中注册多个过滤器,但是我总是得到相同的异常: 2015年11月4日14:35:23.792警告[RMI TCP连接(3)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.refresh 上下文初始化期间遇到异常-正在取消 刷新尝试 org.springframework.beans.factory.BeanCreationException:错误 创建名为的bean 'org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration': 自动连线依赖项的注入失败;嵌套异常是 java.lang.IllegalStateException:@Order on WebSecurity配置程序必须 要独一无二。订单100已使用,因此无法在上使用 com.payment21.webapp.MultiHttpSecurityConfig$ApiWebSecurity配置适配器$$EnhancerBySpringCGLIB$$35c79fe4@1d381684 也是 由于我自己的尝试无效,我尝试了与之完全相同的代码,如图所示: 为了隔离错误,我尝试用基于Java的方法替换web.xml,但也没有成功。我不知道怎么了,医生有问题吗?我的应用程序中有什么东西会干扰配置吗?系统正在正常启动,除非我注册第二个WebSecurity配置程序 这些是我的依赖项:Java Spring安全配置@Order非唯一异常,java,spring,spring-security,configuration,Java,Spring,Spring Security,Configuration,我尝试在我的Spring安全配置中注册多个过滤器,但是我总是得到相同的异常: 2015年11月4日14:35:23.792警告[RMI TCP连接(3)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.refresh 上下文初始化期间遇到异常-正在取消 刷新尝试 org.springframework.beans.factory.BeanCreationExcepti
compile 'org.springframework:spring-webmvc:4.2.2.RELEASE'
compile 'org.springframework:spring-messaging:4.2.2.RELEASE'
compile 'org.springframework:spring-websocket:4.2.2.RELEASE'
compile 'org.springframework:spring-aop:4.2.2.RELEASE'
compile'javax.servlet:javax.servlet-api:3.0.1'
compile 'org.springframework.security:spring-security-web:4.0.3.RELEASE'
compile 'org.springframework.security:spring-security-config:4.0.3.RELEASE'
您可能在spring配置的某个地方有带有
@order(100)
注释的配置。尝试删除@order(100)
注释或给出一些其他订单值。我发现了错误。。。从来没有人在代码段中发布导入。我们正在使用一个多模块项目设置,IntelliJ没有识别Spring注释并使用它
org.apache.logging.log4j.core.config.Order
而不是
org.springframework.core.annotation.Order
因为Spring没有解析正确的注释,所以它假设两种配置的默认值都为100。通常,当同一个bean解析两次时,会发生此异常。 例如,如果一个
@Configuration
文件导入了一个解析同一bean的applicationContext.xml,当应用程序启动时,尝试注册它两次(在您的例子中是MultiHttpSecurityConfig
),您会收到此错误
我解决了从XML中删除bean定义的错误。我也遇到了同样的问题,我解决了将@Configuration注释移动到类级别的错误
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
当我将此描述添加到安全配置中时,我的问题得到了解决:
@Configuration("MySecurityConfig")
public class MySecurityConfig extends WebSecurityConfigurerAdapter
值得注意的是,@Order注释应该在类级别。这有点令人困惑,因为@Journeycorner配置是一个多类示例。我的导入示例:)
可能您已经用@EnableWebSecurity注释了另一个类。请注意,只有一个类可以实现此注释。希望这会有帮助 可能您已经用@EnableWebSecurity注释了另一个类。请注意,只有一个类可以实现此注释。希望这会有帮助
package com.ie.springboot.configuaration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("mkyong").password("123456").roles("USER");
auth.inMemoryAuthentication().withUser("admin").password("{noop}123456").roles("ADMIN");
auth.inMemoryAuthentication().withUser("dba").password("123456").roles("DBA");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
.antMatchers("/*").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_DBA')")
.and().formLogin();
http.csrf().disable();
}
}
我最近也遇到了同样的问题,但在任何地方都找不到正确的答案。最后,我回顾了git历史记录中的步骤,发现所做的唯一更改是将@RefreshScope注释添加到我的类中
通过删除@RefreshScope注释,我的应用程序成功了。将@Order(1000)放在第二个WebSecurity配置适配器上对我有效。这是因为Spring security在引擎盖下使用,而此适配器正在使用Order(100),因此Spring不允许重复订单顺序。在我的情况下,我已经将
@enableAuth2sso
注释放在用@SpringBootApplication
注释的类上,但是我还有一个扩展websecurityConfigureAdapter
的单独类。正如@enableAuth2sso
的文档所述:
如果用户提供了一个现有的WebSecurityConfigureAdapter,并用@EnableOAuth2Sso进行了注释,则可以通过添加身份验证筛选器和身份验证入口点来增强它。如果用户只有@enableAuth2sso,但在WebSecurityConfigureAdapter上没有@enableAuth2sso,则会添加一个@enableAuth2sso,并保护所有路径
由于添加了一个默认适配器,我最终得到了两个,这导致了异常。解决方案当然是将
@enableAuth2sso
注释移动到扩展WebSecurityConfigureAdapter
的类中。在我的例子中,我有两个标签@EnableWebSecurity
和
@EnableGlobalMethodSecurity(securedEnabled=true)
。当我删除@EnableWebSecurity
标记时,问题得到了解决 在我的例子中,在父项目中建立了另一个安全配置类。我使用excludeFilters标记排除父安全配置类,并添加配置类spring直接使用该配置类
@ComponentScan(basePackages = {
"com.example.parentProject", // this is the package that have securit config staff
},
excludeFilters = {@ComponentScan.Filter(
type = FilterType.ASSIGNABLE_TYPE,
value = {
SecurityConfig.class //which is inhareted from the parent package
})
})
public class SpringApp{
.....
}
检查了每个java文件的“@Order”,什么都没有。有可能在XML文件中有一个“隐式的”@Order吗?如果你看到stacktrace,它清楚地提到“WebSecurityConfigureAdapter”的默认值是@Order(100)100,无需在任何地方提及它,这是一些深层次的东西…-)你是如何解决这个问题的?我在使用IDEA编译时遇到了确切的问题。我的应用程序中没有@Order,但它在WebSecurity配置适配器上仍然变得非常混乱!使用SecurityConfiguration类中的
@Order(1000)
,我将我的订单值更改为1000,然后在我的类中添加了@Order(1000),这对你复制粘贴其他答案有效吗?我两次解决了安全bean。一旦被@ComponentScan
注释加载,该注释包含包含安全bean的目录。另一次是通过位于同一目录中的@SpringBootApplication
注释。在移除
package com.ie.springboot.configuaration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("mkyong").password("123456").roles("USER");
auth.inMemoryAuthentication().withUser("admin").password("{noop}123456").roles("ADMIN");
auth.inMemoryAuthentication().withUser("dba").password("123456").roles("DBA");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
.antMatchers("/*").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_DBA')")
.and().formLogin();
http.csrf().disable();
}
}
@ComponentScan(basePackages = {
"com.example.parentProject", // this is the package that have securit config staff
},
excludeFilters = {@ComponentScan.Filter(
type = FilterType.ASSIGNABLE_TYPE,
value = {
SecurityConfig.class //which is inhareted from the parent package
})
})
public class SpringApp{
.....
}