嵌入式Jetty环境中的Spring Boot和Spring Security集成 [英] Spring Boot and Spring Security integration in embedded Jetty environment
问题描述
我正在尝试使用Spring Boot和嵌入式Jetty,通过胖可执行JAR中的 main()
方法初始化Spring Security。
我将Spring Security与Java配置一起使用(没有web.xml)。问题是嵌入式Jetty无法注册 springSecurityFilterChain
过滤器。
I'm trying to initialize Spring Security from a main()
method in a "fat" executable JAR with Spring Boot and embedded Jetty.
I use Spring Security with Java config (no web.xml). The problem is that embedded Jetty fails to register the springSecurityFilterChain
filter.
当我在Jetty中运行与WAR相同的JAR( mvn jetty:run
)时,它正常工作,我看到这个:
When I run the same JAR as a WAR in Jetty (mvn jetty:run
) it works normally and I see this:
Initializing Spring embedded WebApplicationContext
但是在嵌入式Jetty中运行时,我看不到 WebApplicationContext
初始化。
But when running in embedded Jetty I see no WebApplicationContext
getting initialized.
我有这个 EmbeddedServletContainerFactory
:
@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
冲突。在我的 pom.xml
中,我具有以下依赖性:
I've tried creating a subclass of AbstractSecurityWebApplicationInitializer
but it is in conflict with my SpringBootServletInitializer
. In my pom.xml
I have these dependencies:
-
spring-boot-starter-jetty
-
spring-boot-starter-logging
-
spring-boot-starter
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
时,还需要ServletContext,我也尝试注册了使用Jetty的DelegatingFilterProxy
,但随后抛出:
Also I tried to register the DelegatingFilterProxy
with Jetty but then it throws:
java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
我猜我需要告诉Jetty使用 WebApplicationContext
,但是如何?
I'm guessing I need to tell Jetty to use WebApplicationContext
, but how?
推荐答案
您的设置禁用了Spring Boot的魔力。不用自己设置容器,而是让Spring Boot处理它。您可以简单地将 JettyServerCustomizer
作为 @Bean
添加到配置中。这将执行它,您可以根据需要进行注册。
Your setup disables Spring Boot to do its magic. Instead of setting up the container yourself let Spring Boot handle it. You can simply add your JettyServerCustomizer
to the configuration as a @Bean
. This will execute it and you can do your registration of whatever you need.
这仍然允许Spring Boot发挥作用,并且您已经注册了您需要的其他端点。
This still allows Spring Boot to do its magic and you have registered the additional endpoint y ou need.
另一种解决方案是将servlet作为 ServletRegistrationBean
添加到您的配置中,然后由Spring Boot自动添加。
Another solution could be to add the servlet as a ServletRegistrationBean
to your configuration it will then automatically be added by Spring Boot.
这篇关于嵌入式Jetty环境中的Spring Boot和Spring Security集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!