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.
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
But when running in embedded Jetty I see no WebApplicationContext
getting initialized.
I have this 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;
}
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
When I add spring-boot-web
it throws:
java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
Also I tried to register the DelegatingFilterProxy
with Jetty but then it throws:
java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
I'm guessing I need to tell Jetty to use WebApplicationContext
, but how?
main()
. – albogdanoServletContainer
to boot a JerseyResourceConfig
class. – albogdanoJettyServerCustomizer
register it as a bean in your context and spring boot will apply it for you. The port etc. will all be set by Spring Boot. You could also register the config as aServletBean
so that it would be registered by Spring Boot. – M. Deinum