title: Spring Security j_spring_security_check not invoked
body: <p>I have a Spring WebApp using custom User, Roles, Permissions tables custom (read "naive") authentication.</p>
  
  <p>I am migrating the code to use Spring Security now. I read tutorials and got to the point where my login.jsp page, css, js, png files can be accessed ANONYMOUSLY. I have a form with action attribute as "j_spring_security_check". On submitting the form the browser performs an HTTP Post to this URL which results in a 404.</p>
  
  <p>Now I am not mapping j_spring_security_check using <code>RequestMapping</code>. Is that required? When should we have a request mapping for this URL?</p>
  
  <p>In my authentication provider I provide a reference to a bean of a class which implements UserDetailsService. I am expecting Spring to perform the authentication by invoking loadUserByUserName but this method never gets invoked. Why is the method not invoked? Have I misunderstood how authentication should work? Do I need to provide a custom request mapping for j_spring_security_check to make it work?</p>
  
  <p>Here is my Custom User Details Service:</p>
  
  <pre><code>@Service(value="myUserDetailsService")
  public class LoginUserService implements UserDetailsService {
  
    @Autowired
    private UserRepository userRepository;
  
    @Override
    public UserDetails loadUserByUsername(String username)
        throws UsernameNotFoundException {
  
      System.out.println("here");
  
      User user = userRepository.findUser(username);
      if (user != null)
        return new V2VUserDetails(user);
      else
        return null;
    }
  
  }
  </code></pre>
  
  <p>Here is my security XML:</p>
  
  <pre><code>    &lt;http pattern="/**/*.css" security="none" /&gt;
      &lt;http pattern="/**/*.js" security="none" /&gt;
      &lt;http pattern="/**/*.png" security="none" /&gt;
  
      &lt;http auto-config="true"&gt;
        &lt;intercept-url pattern="/login.html*" access="IS_AUTHENTICATED_ANONYMOUSLY" /&gt;
        &lt;intercept-url pattern="/j_spring_security_check" access="IS_AUTHENTICATED_ANONYMOUSLY" /&gt;
        &lt;intercept-url pattern="/**" access="ROLE_USER" /&gt;
        &lt;form-login login-page="/login.html"
                    login-processing-url="/j_spring_security_check"
                    default-target-url="/welcomePage.html"
                    authentication-failure-url="/welcomePage.html"
                    always-use-default-target="true" /&gt;
      &lt;/http&gt;
  
    &lt;authentication-manager&gt;
      &lt;authentication-provider user-service-ref='myUserDetailsService'/&gt;
    &lt;/authentication-manager&gt;
  
    &lt;beans:bean id="myUserDetailsService"
      class="security.LoginUserService"&gt;
    &lt;/beans:bean&gt;
  </code></pre>
  
  <p>I checked several answers on Stackoverflow and other sites but could not fix the problem.</p>
  
  <p><strong>Edit</strong>
  Tried the suggestion given <a href="http://bit.ly/13BmtER" rel="nofollow">here</a>. Now getting BeanFactory not initialized error.</p>
  
  <p><strong>Edit</strong>
      
          contextConfigLocation
          /WEB-INF/security-v2v-servlet.xml
      </p>
  
  <pre><code>&lt;filter&gt;
    &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
    &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt;
  &lt;/filter&gt;
  
  &lt;filter-mapping&gt;
    &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
    &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
    &lt;dispatcher&gt;REQUEST&lt;/dispatcher&gt;
    &lt;dispatcher&gt;FORWARD&lt;/dispatcher&gt;
  &lt;/filter-mapping&gt;
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p>Current web.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
           id="WebApp_ID" version="2.5"&gt;
  
      &lt;display-name&gt;Spring3MVC&lt;/display-name&gt;
  
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/security-v2v-servlet.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;welcome-file-list&gt;
          &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
      &lt;error-page&gt;
          &lt;error-code&gt;500&lt;/error-code&gt;
          &lt;location&gt;/errorPage.jsp&lt;/location&gt;
      &lt;/error-page&gt;
      &lt;error-page&gt;
          &lt;error-code&gt;404&lt;/error-code&gt;
          &lt;location&gt;/errorPage.jsp&lt;/location&gt;
      &lt;/error-page&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;v2v&lt;/servlet-name&gt;
          &lt;servlet-class&gt;
              org.springframework.web.servlet.DispatcherServlet
          &lt;/servlet-class&gt;
          &lt;load-on-startup&gt;2&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;Resource Servlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.ResourceServlet&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;v2v&lt;/servlet-name&gt;
          &lt;url-pattern&gt;*.html&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;v2v&lt;/servlet-name&gt;
          &lt;url-pattern&gt;*.zip&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;default&lt;/servlet-name&gt;
          &lt;url-pattern&gt;*.css&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;default&lt;/servlet-name&gt;
          &lt;url-pattern&gt;*.js&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;default&lt;/servlet-name&gt;
          &lt;url-pattern&gt;*.jpeg&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;default&lt;/servlet-name&gt;
          &lt;url-pattern&gt;*.gif&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;default&lt;/servlet-name&gt;
          &lt;url-pattern&gt;*.png&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;default&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/j_spring_security_check&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
      &lt;filter&gt;
        &lt;filter-name&gt;UserAddFilter&lt;/filter-name&gt;
        &lt;filter-class&gt;
            filter.UserInfoAddToThreadFilter
        &lt;/filter-class&gt;
      &lt;/filter&gt;
      &lt;filter-mapping&gt;
        &lt;filter-name&gt;UserAddFilter&lt;/filter-name&gt;
        &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  
      &lt;filter&gt;
        &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
        &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt;
      &lt;/filter&gt;
      &lt;filter-mapping&gt;
        &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
        &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
        &lt;dispatcher&gt;REQUEST&lt;/dispatcher&gt;
        &lt;dispatcher&gt;FORWARD&lt;/dispatcher&gt;
      &lt;/filter-mapping&gt;
  
  
  
      &lt;listener&gt;
        &lt;listener-class&gt;org.springframework.web.util.Log4jConfigListener&lt;/listener-class&gt;
      &lt;/listener&gt;
  
  &lt;/web-app&gt;
  </code></pre>