Spring Security 配置@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:创建名为“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();
        }
    }
}

为了隔离错误,我尝试用基于 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'

原文由 Journeycorner 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 660
2 个回答

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

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

代替

org.springframework.core.annotation.Order

由于 Spring 没有解析正确的注解,它假设两种配置的默认值为 100。

原文由 Journeycorner 发布,翻译遵循 CC BY-SA 3.0 许可协议

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

原文由 Guchelkaben 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题