title: Add Spring security Java Config to Spring config
body: <p>i'm working on a spring application lately, and i wanted to add spring security to my app.</p>
  
  <p>I had a simple page containing a login form working like a charm, but when i wanted to add the spring security configuration to the app i get an error.</p>
  
  <p>Here's the code of my SecurityConfig file :</p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
  /*@Autowired
  public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
      auth
              .inMemoryAuthentication()
              .withUser("user").password("password").roles("USER");
  }*/
  
  @Override
  protected void configure(HttpSecurity http) throws Exception {
      http
            .authorizeRequests()
              /*.antMatchers("/infermier*//**").hasRole("INFERMIER")
              .antMatchers("/docteur*//**").hasRole("DOCTEUR")
              .antMatchers("/user*//**").hasRole("USER")*/
              .anyRequest().authenticated()
            .and()
              .formLogin()
              .loginProcessingUrl("/login")
              .permitAll();
  }
  
  }
  </code></pre>
  
  <p>And here's the log :</p>
  
  <pre><code>Error invoking ServletContainerInitializer org.springframework.web.SpringServletContainerInitializer
  java.lang.NoClassDefFoundError: org/springframework/security/config/annotation/web/configuration/WebSecurityConfigurerAdapter
      at java.lang.ClassLoader.defineClass1(Native Method)
      at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
      at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
      at org.glassfish.web.loader.WebappClassLoader.findClass(WebappClassLoader.java:1183)
      at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1728)
      at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1611)
      at configuration.WebInitializer.getRootConfigClasses(WebInitializer.java:12)
      at org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer.createRootApplicationContext(AbstractAnnotationConfigDispatcherServletInitializer.java:50)
      at org.springframework.web.context.AbstractContextLoaderInitializer.registerContextLoaderListener(AbstractContextLoaderInitializer.java:57)
      at org.springframework.web.context.AbstractContextLoaderInitializer.onStartup(AbstractContextLoaderInitializer.java:47)
      at org.springframework.web.servlet.support.AbstractDispatcherServletInitializer.onStartup(AbstractDispatcherServletInitializer.java:66)
      at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:175)
      at org.apache.catalina.core.StandardContext.callServletContainerInitializers(StandardContext.java:5997)
      at com.sun.enterprise.web.WebModule.callServletContainerInitializers(WebModule.java:774)
      at org.apache.catalina.core.StandardContext.start(StandardContext.java:5895)
      at com.sun.enterprise.web.WebModule.start(WebModule.java:691)
      at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:1041)
      at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:1024)
      at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:747)
      at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:2278)
      at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1924)
      at com.sun.enterprise.web.WebApplication.start(WebApplication.java:139)
      at org.glassfish.internal.data.EngineRef.start(EngineRef.java:122)
    Startup of context /servicemed failed due to previous errors]]
  
  [2014-08-13T12:57:16.429+0100] [glassfish 4.0] [SEVERE] [AS-WEB-CORE-00175] [javax.enterprise.web.core] [tid: _ThreadID=37 _ThreadName=admin-listener(4)] [timeMillis: 1407931036429] [levelValue: 1000] [[
    Exception during cleanup after start failed
  org.apache.catalina.LifecycleException: Manager has not yet been started
      at org.apache.catalina.session.StandardManager.stop(StandardManager.java:934)
      at org.apache.catalina.core.StandardContext.stop(StandardContext.java:6099)
      at com.sun.enterprise.web.WebModule.stop(WebModule.java:720)
      at org.apache.catalina.core.StandardContext.start(StandardContext.java:5916)
      at com.sun.enterprise.web.WebModule.start(WebModule.java:691)
      at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:1041)
      at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:1024)
      at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:747)
      at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:2278)
      at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1924)
      at com.sun.enterprise.web.WebApplication.start(WebApplication.java:139)
      at org.glassfish.internal.data.EngineRef.start(EngineRef.java:122)
  org.apache.catalina.LifecycleException: org.apache.catalina.LifecycleException: java.lang.NoClassDefFoundError: org/springframework/security/config/annotation/web/configuration/WebSecurityConfigurerAdapter
      at org.apache.catalina.core.StandardContext.start(StandardContext.java:5920)
      at com.sun.enterprise.web.WebModule.start(WebModule.java:691)
      at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:1041)
      at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:1024)
      at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:747)
      at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:2278)
      at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1924)
      at com.sun.enterprise.web.WebApplication.start(WebApplication.java:139)
      at org.glassfish.internal.data.EngineRef.start(EngineRef.java:122)
  </code></pre>
  
  <p>PS: I did added it to the MVCInitializer :</p>
  
  <pre><code>@Override
      protected Class&lt;?&gt;[] getRootConfigClasses() {
          return new Class[] { AppConfig.class,SecurityConfig.class};
      }
  </code></pre>