title: Access application context from spring securty context
body: <p>I have a Spring MVC application which uses spring security for authorization.</p>
  
  <p>I've implemented a custom AuthenticationProvider which authorizes users.</p>
  
  <p>I'd like this custom AuthenticationProvider to access a bean defined in the application context.</p>
  
  <p>Is this possible? If so, how?</p>
  
  <p>web.xml:</p>
  
  <pre><code> ...
   &lt;context-param&gt;
    &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
    &lt;param-value&gt;WEB-INF/spring-security.xml&lt;/param-value&gt;
   &lt;/context-param&gt;
    ...
   &lt;listener&gt;
    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
   &lt;/listener&gt;
    ...
   &lt;servlet&gt;
    &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
    &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
    &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
   &lt;/servlet&gt;
  
   &lt;servlet-mapping&gt;
    &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
    &lt;url-pattern&gt;/&lt;/url-pattern&gt;
   &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p>spring-security.xml:</p>
  
  <pre><code>  ...
    &lt;authentication-manager&gt;
      &lt;authentication-provider ref="customAuthenticationProvider"/&gt;
    &lt;/authentication-manager&gt;
  
    &lt;beans:bean class="com.example.davvstest.CustomAuthenticationProvider" id="customAuthenticationProvider"&gt;
      &lt;beans:property name="loginService" ref="loginService" /&gt;
    &lt;/beans:bean&gt;
    ...
  </code></pre>
  
  <p>dispatcher-servlet.xml:</p>
  
  <pre><code>  &lt;bean class="com.example.davvstest.LoginService" name="loginService"&gt;
    &lt;/bean&gt;
  </code></pre>
  
  <p>CustomAuthenticationProvider.java:</p>
  
  <pre><code>package com.example.davvstest;
  
  import org.springframework.security.authentication.AuthenticationProvider;
  import org.springframework.security.core.Authentication;
  import org.springframework.security.core.AuthenticationException;
  
  public class CustomAuthenticationProvider implements AuthenticationProvider {
  
      private LoginService loginService;
  
      public LoginService getLoginService() {
          return loginService;
      }
  
      public void setLoginService(LoginService loginService) {
          this.loginService = loginService;
      }
  
      @Override
      public Authentication authenticate(Authentication authentication)
              throws AuthenticationException {
          if (!loginService.checkAuth(authentication.getName())){
              throw new BadUserNameException("Bad username");
          }
          return authentication;
      }
  
      @Override
      public boolean supports(Class&lt;?&gt; authentication) {
          return true;
      }
  
  }
  </code></pre>
  
  <p>LoginService.java:</p>
  
  <pre><code>package com.example.davvstest;
  
  public class LoginService {
  
      public LoginService() {
      }
  
      public boolean checkAuth(String username){
          return true;
      }
  }
  </code></pre>
  
  <p>The error I get is:</p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.filterChains': Cannot resolve reference to bean 'org.springframework.security.web.DefaultSecurityFilterChain#0' while setting bean property 'sourceList' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.DefaultSecurityFilterChain#0': Cannot resolve reference to bean 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0' while setting constructor argument with key [1]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0': Cannot resolve reference to bean 'org.springframework.security.authentication.ProviderManager#0' while setting bean property 'authenticationManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authentication.ProviderManager#0': Cannot resolve reference to bean 'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authenticationManager': Cannot resolve reference to bean 'customAuthenticationProvider' while setting constructor argument with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customAuthenticationProvider' defined in ServletContext resource [/WEB-INF/spring-security.xml]: Cannot resolve reference to bean 'loginService' while setting bean property 'loginService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'loginService' is defined
  </code></pre>