Spring安全配置@Order不是唯一的例外

Jou*_*ner 17 java configuration spring spring-security

我试图在我的Spring Security配置中注册多个过滤器,但是我总是得到相同的异常:

04-Nov-2015 14:35:23.792警告[RMI TCP连接(3)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.refresh在上下文初始化期间遇到异常 - 取消刷新尝试org.springframework.beans .factory.BeanCreationException:创建名为'org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration'的bean时出错:注入自动连接的依赖项失败; 嵌套异常是java.lang.IllegalStateException:WebSecurityConfigurers上的@Order必须是唯一的.已经使用了100的订单,因此它不能用于com.payment21.webapp.MultiHttpSecurityConfig$ApiWebSecurityConfigurationAdapter$$EnhancerBySpringCGLIB $ 35c79fe4@1d381684.

由于我自己的尝试不起作用,我尝试了与Spring Security参考中显示的完全相同的代码:

@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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

为了隔离错误,我尝试用基于Java的方法替换web.xml,但它也没有用.我不知道出了什么问题,这个文件错了吗?我的应用程序中的某些东西可以搞乱配置吗?系统正常启动,除非我注册了第二个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'
Run Code Online (Sandbox Code Playgroud)

小智 14

值得注意的是,@ 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("/");
}

}
Run Code Online (Sandbox Code Playgroud)


Jou*_*ner 8

我发现错误...没有人在片段中发布导入.我们正在使用多模块项目设置,IntelliJ无法识别Spring注释并使用

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

代替

org.springframework.core.annotation.Order

由于Spring没有解析正确的注释,因此假设两个配置的默认值为100.

  • 你是怎么解决的?在使用IDEA进行编译时,我遇到了确切的问题.我的应用程序中没有@Order,但它仍然在WebSecurityConfigurerAdapter上变得非常困惑! (6认同)
  • 在 SecurityConfiguration 类中使用“@Order(1000)” (2认同)

小智 7

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

我解决了从XML中删除bean定义的错误.


Guc*_*ben 5

也许您已经用@EnableWebSecurity注释注释了另一个类。请注意,只有一个类可以实现此注释。希望对您有所帮助!