Добавление безопасности spring во встроенный Jetty

Я пытаюсь добавить Spring Security - OAuth2 в свой Rest API, который находится на встроенном Jetty. Я получаю сообщение об ошибке «Бан с именем SpringSecurityFilterChain не определен». Когда я добавляю ContextLoadListener с context.addEventListener( new ContextLoaderListener() );, я начинаю получать

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

ошибка. Ниже приведена структура моего кода.

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();
    }
}

person Aly    schedule 31.03.2015    source источник
comment
Но где springSecurityFilterChain? Вы это где-то определили?   -  person ArunM    schedule 31.03.2015
comment
Привет @АрунМ. Спасибо за комментарий. На самом деле я пытаюсь перенести эту конфигурацию из конфигурации автономного сервера во встроенную конфигурацию причала (я новичок в spring-security). Как я искал, этого должно быть достаточно? context.addFilter(new FilterHolder( new DelegatingFilterProxy( "springSecurityFilterChain" ) ),"/*", EnumSet.allOf( DispatcherType.class )); . Должен ли я дать какое-либо другое определение?   -  person Aly    schedule 31.03.2015
comment
Вы проверили этот вопрос: stackoverflow.com/q/19053848/363573?   -  person Stephan    schedule 29.12.2015