2015-11-04 21 views
3

我试着在我的春季安全配置来注册多个过滤器,但我总是得到相同的异常:春季安全配置@Order不是唯一的例外

04 - 11月 - 2015年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.confi guration.WebSecurityConfiguration': 注入自动装配依赖失败;嵌套的异常是 java.lang.IllegalStateException:WebSecurityConfigurers上的@Order必须是唯一的 。 100的订单已被使用,所以它不能用于 com.payment21.webapp.MultiHttpSecurityConfig$ApiW[email protected]1d381684 。

由于我自己的努力没有工作,我试过完全相同的代码如图所示Spring Security reference

@EnableWebSecurity 
public class MultiHttpSecurityConfig { 
    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) { 
     auth 
      .inMemoryAuthentication() 
       .withUser("user").password("password").roles("USER").and() 
       .withUser("admin").password("password").roles("USER", "ADMIN"); 
    } 

    @Configuration 
    @Order(1)               
    public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter { 
     protected void configure(HttpSecurity http) throws Exception { 
      http 
       .antMatcher("/api/**")        
       .authorizeRequests() 
        .anyRequest().hasRole("ADMIN") 
        .and() 
       .httpBasic(); 
     } 
    } 

    @Configuration             
    public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter { 

     @Override 
     protected void configure(HttpSecurity http) throws Exception { 
      http 
       .authorizeRequests() 
        .anyRequest().authenticated() 
        .and() 
       .formLogin(); 
     } 
    } 
} 

要找出错误我试图取代通过web.xml中基于Java的方法,但它也没有工作。我不知道什么是错,文件是错的?我的应用程序中的某些东西可能与配置混乱吗?系统正常启动,除非我注册第二个WebSecurityConfigAdapter。

那是我的依赖关系:

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' 

回答

3

我发现错误... noone曾经在snippets中发布导入。我们使用了多模块项目设置,和IntelliJ不承认春天注释和使用

org.apache.logging.log4j.core.config.Order

代替

org.springframework.core.annotation.Order

由于春节不解析正确的注解,它假设defaul两种配置的t值均为100。

+0

这是一些深层次的东西.. :-) –

+3

你是如何解决这个问题的?编译IDEA时遇到确切的问题。我的应用程序中没有@Order,但它仍然对WebSecurityConfigurerAdapter非常困惑! –

0

你可能得了配置与@order(100)标注在某处你Spring配置。请尝试删除@order(100)注释或提供其他订单值。

+0

检查每个java文件的“@Order”,什么都没有。是否有可能像XML文件中的“隐式”“@Order”? – Journeycorner

+0

如果你看到堆栈跟踪,它清楚地提到“WebSecurityConfigurerAdapter”有@order(100) – Imrank

+0

100是默认值,不需要在任何地方提及 – eis

3

通常,当同一个bean被解析两次时,会发生此异常。 例如,如果@Configuration文件导入解析同一个bean的applicationContext.xml,当应用程序启动时会尝试注册它(在您的情况下是MultiHttpSecurityConfig)两次,并且出现此错误。

我解决了错误的XML

0

取出bean定义我有同样的问题,我解决了这个错误移动@Configuration注释一流水平。

@Configuration 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 
0

我的问题得到有效解决,当我加入这个描述的安全配置:

@Configuration("MySecurityConfig") 
public class MySecurityConfig extends WebSecurityConfigurerAdapter 
4

这可能是值得一提,在@Order注释应在一流水平。这有点令人困惑,因为@Journeycorner配置是一个多类的例子。我的例子与进口:)

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.method.configuration.EnableGlobalMethodSecurity; 
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; 

import com.someco.entity.User; 
import com.someco.service.SpringDataJpaUserDetailsService; 

@Configuration("CustomSecurityConfig") 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
@Order(1000)               
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 

@Autowired 
private SpringDataJpaUserDetailsService userDetailsService; 

@Override 
protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
    auth 
     .userDetailsService(this.userDetailsService) 
      .passwordEncoder(User.PASSWORD_ENCODER); 
} 

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http 
     .authorizeRequests() 
      .antMatchers("/built/**", "/main.css").permitAll() 
      .anyRequest().authenticated() 
      .and() 
     .formLogin() 
      .defaultSuccessUrl("/", true) 
      .permitAll() 
      .and() 
     .httpBasic() 
      .and() 
     .csrf().disable() 
     .logout() 
      .logoutSuccessUrl("/"); 
} 

}