有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

嵌入式Jetty环境中的java Spring引导和Spring安全集成

我正试图从一个带有Spring引导和嵌入式Jetty的“fat”可执行JAR中的main()方法初始化Spring安全性。 我在Java配置中使用Spring安全性(没有web.xml)。问题是嵌入式Jetty无法注册springSecurityFilterChain过滤器

当我在Jetty(mvn jetty:run)中运行与WAR相同的JAR时,它工作正常,我看到:

Initializing Spring embedded WebApplicationContext

但是在嵌入式Jetty中运行时,我没有看到任何WebApplicationContext被初始化

我有这个{}:

@Bean
public EmbeddedServletContainerFactory servletContainerFactory() {
    JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory();
    factory.setPort(8080);
    factory.addServerCustomizers(new JettyServerCustomizer() {
        public void customize(Server server) {    
        // TODO: INITIALIZE SPRING SECURITY SOMEHOW...
        }
    });
    return factory;
}

我试图创建AbstractSecurityWebApplicationInitializer的子类,但它与我的SpringBootServletInitializer冲突。在mypom.xml中,我有以下依赖项:

  • spring-boot-starter-jetty
  • spring-boot-starter-logging
  • spring-boot-starter

当我添加spring-boot-web时,它抛出:

java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling

我还试图向Jetty注册DelegatingFilterProxy,但它抛出:

java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?

我猜我需要告诉Jetty使用WebApplicationContext,但是如何使用呢


共 (1) 个答案

  1. # 1 楼答案

    你的设置会禁用Spring Boot来发挥它的魔力。与其自己设置容器,不如让Spring Boot来处理。只需将JettyServerCustomizer作为@Bean添加到配置中即可。这将执行它,你可以做任何你需要的注册

    这仍然允许Spring Boot发挥其魔力,并且您已经注册了所需的额外端点

    另一个解决方案是将servlet作为ServletRegistrationBean添加到您的配置中,然后由Spring Boot自动添加