【问题标题】:Adding Spring Security as a dependency causes the app behave differently添加 Spring Security 作为依赖项会导致应用程序行为不同
【发布时间】:2014-05-08 11:52:18
【问题描述】:

简而言之:当我将spring-boot-starter-security 添加到我的build.gradle 时,会发生奇怪的事情。这是我的提炼代码(根本没有其他代码):

public class App {
    public static void main(String[] args) {
        try {
            SpringApplication.run(AppConfiguration.class, args);
        } catch(BeanCreationException e) {
            System.out.printf("HURRAY: %s\n", e.getBeanName());
        }
    }

    @Configuration
    @EnableAutoConfiguration
    @ComponentScan          // !!! NOTE: extends WebMvcConfigurerAdapter !!!
    public static class AppConfiguration extends WebMvcConfigurerAdapter {
        @Autowired
        private MyService myService;
    }

    @Component
    public static class MyService {
        public MyService() {
            throw new RuntimeException("Error");
        }
    }
}

这是我的build.gradle

dependencies {
    testCompile group: "junit", name: "junit", version: "4.11"
    compile group: "org.springframework.boot", name: "spring-boot-starter-web", version: "1.0.0.RC5"
    // compile group: "org.springframework.boot", name: "spring-boot-starter-security", version: "1.0.0.RC5" // HERE
}

我期望的行为是BeanCreationException 将被抛出,catch 将打印HURRAY。它以这种方式工作。当我尝试在build.gradle 中添加spring-boot-starter-security 依赖项时,会发生奇怪的事情。一旦我取消注释该行,抛出的异常是ApplicationContextException。如果我递归调用getCause(),结果是这样的:

org.springframework.context.ApplicationContextException
org.springframework.boot.context.embedded.EmbeddedServletContainerException
org.apache.catalina.LifecycleException
org.apache.catalina.LifecycleException
org.apache.catalina.LifecycleException

你可以看到,根本没有BeanCreationException

当我运行应用程序时,the logs clearly say 在某处有一个BeanCreationException

有没有可靠的方法来捕捉BeanCreationException

【问题讨论】:

  • 我猜没有BeanCreationException 因为还有其他问题。不确定是什么。你能显示堆栈跟踪吗? (忽略。刚刚看到日志。)

标签: java spring tomcat spring-security spring-boot


【解决方案1】:

BeanCreationException 被抛出另一个线程(Spring Security 是一个过滤器,过滤器在嵌入式容器中以特殊方式初始化)。我不确定我们是否可以强制 Tomcat 使用主线程(我尝试过一次并放弃了,但这并不一定意味着这是不可能的)。如果你可以改进它,请在 github 中提出建议。

【讨论】:

  • 谢谢戴夫。我刚刚发现的一件有趣的事情是,这个“问题”(问题?)只在我使用 Tomcat 时出现。当我切换到 Jetty 时,它会正确抛出 BeanCreationException
猜你喜欢
  • 2020-03-05
  • 2019-08-07
  • 2018-10-07
  • 1970-01-01
  • 2020-03-07
  • 1970-01-01
  • 2019-02-09
  • 2015-12-31
  • 2017-04-09
相关资源
最近更新 更多