3

I am trying to add Spring Security - OAuth2 to my Rest API which resides on embedded Jetty. I am getting "No bean named 'springSecurityFilterChain' is defined" error. When I add a ContextLoadListener with context.addEventListener( new ContextLoaderListener() ); I start getting

Cannot initialize context because there is already a root application context present - check whether you have multiple ContextLoader* definitions in your web.xml!" 

error. The following is the structure of my Code

public class Launcher
{
  public static void main( String[] args )
  {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext( CMApplicationConfig.class, TestConfig.class);  
    JettyServer server = new JettyServer();
    server.start( ctx );
  }
}


public class JettyServer
  public void start( AnnotationConfigApplicationContext c )
  {
    ServletContextHandler context   = new ServletContextHandler( ServletContextHandler.SESSIONS );

    // Set application context parent of webapp context. The purpose is sharing singleton objects which reside on the application side with Jetty context       
    final GenericWebApplicationContext webApplicationContext = new GenericWebApplicationContext();
    webApplicationContext.setServletContext(context.getServletContext());
    webApplicationContext.setParent(c);
    webApplicationContext.refresh();

    context.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webApplicationContext);
    context.setContextPath( "/" );

    ServletHolder springServlet = context.addServlet( org.springframework.web.servlet.DispatcherServlet.class, "/" );
    springServlet.setInitParameter( "contextClass", AnnotationConfigWebApplicationContext.class.getName() );
    springServlet.setInitParameter( "contextConfigLocation", "config.CMWebContextConfiguration");
    springServlet.setInitOrder( 1 );

    // Add spring security      
    context.addFilter(new FilterHolder( new DelegatingFilterProxy( "springSecurityFilterChain" ) ),"/*", EnumSet.allOf( DispatcherType.class ));

    //This gives: Cannot initialize context because there is already a root application context present - check whether you have multiple ContextLoader
    context.addEventListener( new ContextLoaderListener() );

    // Configure server
    _jettyServer = new Server( createThreadPool() );
    _jettyServer.setConnectors( createConnectors( _jettyServer ) );
    _jettyServer.setHandler( context );

    try
    {
        _jettyServer.start();
    }
    catch ( Exception ex )
    {
        ex.printStackTrace();
    }
}
Aly
  • 81
  • 4
  • But where is the springSecurityFilterChain ? Have you defined this somewhere ? – ArunM Mar 31 '15 at 11:25
  • 2
    Hi @ArunM. Thanks for the comment. Actually I am trying to migrate this configuration from a standalone server configuration to embedded jetty configuration (I'm new to spring-security). As I searched this should be sufficient? `context.addFilter(new FilterHolder( new DelegatingFilterProxy( "springSecurityFilterChain" ) ),"/*", EnumSet.allOf( DispatcherType.class ));` . Should I make any other definition? – Aly Mar 31 '15 at 11:53
  • Did you check this question: http://stackoverflow.com/q/19053848/363573 ? – Stephan Dec 29 '15 at 11:53

0 Answers0