title: Spring security:How to map j_spring_security_check in controller?
body: <p>I'm trying to integrate spring security into existing code base which is in spring mvc.</p>
  
  <p>In my existing code base i v'e a client delegate in web project which interacts with service delegate in service project which in turn interacts with service interface of back-end layer.It is the service interface impl of back-end layer which i'm making implement the UserDetailsService interface.</p>
  
  <p>P.S - I'm creating/converting/passing on various objects i.e. Model,DO,DTO,Request,Response etc across all the layers.</p>
  
  <p>So now,when i click on submit i want the control to go through the delegate from web to back-end layer till the class which implements UserDetailsService interface. How can i achieve this?</p>
  
  <p>[Update]:After going through <a href="https://stackoverflow.com/questions/5971453/spring-security-custom-filter">this link</a> i changed my security xml like shown below:</p>
  
  <pre><code>     &lt;security:http auto-config="true" use-expressions="true"&gt;
      &lt;security:intercept-url pattern="/welcome.html" access="hasRole('ROLE_USER')" /&gt;
      &lt;security:form-login login-page="/portalLogin.html" login-processing-url="/signIn" always-use-default-target="true"
      username-parameter="username" password-parameter="password"
          default-target-url="/welcome.html" authentication-failure-url="/loginfailed.html" /&gt;
      &lt;security:logout logout-success-url="/logout.html" /&gt;
  &lt;/security:http&gt; 
  </code></pre>
  
  <p>My LoginController is like this:</p>
  
  <pre><code>      @RequestMapping("/signIn")
          public ModelAndView onClickLogin(@ModelAttribute("dashboard")
          LoginModel loginModel,Model model){
          try{
          delegate.clickLogin(loginModel);
          if(null != loginModel.getError()){
              // return new ModelAndView("error");
          }       
          }catch(Exception exception){
          }
          return new ModelAndView("redirect:welcome.html");
  
          }
  </code></pre>
  
  <p>[Update]:Sorry for very late update.Controller Mapping issue is resolved, now i'm facing issues because of UsernamePasswordAuthenticationFilter.After comparing logs generated by above code with properly working code logs,i figured out that spring security is not firing 'UsernamePasswordAuthenticationFilter' for some reasons in my case.'UsernamePasswordAuthenticationFilter' is the one which authenticates the user.</p>
  
  <pre><code>            //Logs when authentication is done 
              [while using spring's default /j_spring_security_check].
  
      DEBUG FilterChainProxy - /j_spring_security_check at position 3 of 10 in additional filter chain; firing Filter: 'UsernamePasswordAuthenticationFilter'
      DEBUG UsernamePasswordAuthenticationFilter - Request is to process authentication
      DEBUG ProviderManager - Authentication attempt using org.springframework.security.authentication.dao.DaoAuthenticationProvider
      session created
      DEBUG SessionImpl - Opened session at timestamp: 13661947944
      SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=[] updates=[] deletions=[] collectionCreations=[] collectionRemovals=[] collectionUpdates=[] unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])
      After openSession
      DEBUG SessionImpl - Opened session at timestamp: 13661947944
      Criteria before eq::this::CriteriaImpl(com.infy.itrade.core.entity.LoginDO:this[][])::CriteriaImpl(com.infy.itrade.core.entity.LoginDO:this[][])...
  
      //Logs when authentication is not done [while using custom login processing url : /signIn.html].
      DEBUG FilterChainProxy - /signIn.html at position 3 of 10 in additional filter chain; firing Filter: 'UsernamePasswordAuthenticationFilter' //UsernamePasswordAuthenticationFilter is not doing authentication process instead it skips to next filter.
      DEBUG FilterChainProxy - /signIn.html at position 4 of 10 in additional filter chain; firing Filter: 'BasicAuthenticationFilter'
      DEBUG FilterChainProxy - /signIn.html at position 5 of 10 in additional filter chain; firing Filter: 'RequestCacheAwareFilter'
  </code></pre>
  
  <p>I created a custom filter 'MyFilter' which extends 'AbstractAuthenticationProcessingFilter'. But still 'MyFilter' doesn't do any authentication as the log shows the following:</p>
  
  <pre><code>    DEBUG FilterChainProxy - /signIn.html at position 2 of 9 in additional filter chain; firing Filter: 'LogoutFilter'
      DEBUG FilterChainProxy - /signIn.html at position 3 of 9 in additional filter chain; firing Filter: 'MyFilter'
      DEBUG FilterChainProxy - /signIn.html at position 4 of 9 in additional filter chain; firing Filter: 'RequestCacheAwareFilter'
  </code></pre>
  
  <p>MyFilter:</p>
  
  <pre><code>    public class MyFilter extends AbstractAuthenticationProcessingFilter {
  
      private static final String DEFAULT_FILTER_PROCESSES_URL = "/signIn";
      private static final String POST = "POST";
      public static final String SPRING_SECURITY_FORM_USERNAME_KEY = "username";
      public static final String SPRING_SECURITY_FORM_PASSWORD_KEY = "password";
      public static final String SPRING_SECURITY_LAST_USERNAME_KEY = "SPRING_SECURITY_LAST_USERNAME";
  
      private String usernameParameter = SPRING_SECURITY_FORM_USERNAME_KEY;
      private String passwordParameter = SPRING_SECURITY_FORM_PASSWORD_KEY;
  
      public MyFilter() {
          super(DEFAULT_FILTER_PROCESSES_URL);
      }
  
      public MyFilter(String defaultFilterProcessesUrl) {
          super(defaultFilterProcessesUrl);
          // TODO Auto-generated constructor stub
      }
  
      @Override
      public Authentication attemptAuthentication(HttpServletRequest request,
              HttpServletResponse arg1) throws AuthenticationException,
              IOException, ServletException {
          // TODO Auto-generated method stub
          String username = obtainUsername(request);
          String password = obtainPassword(request);
  
          if (username == null) {
              username = "";
          }
  
          if (password == null) {
              password = "";
          }
  
          username = username.trim();
          UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
                  username, password);
          HttpSession session = request.getSession(false);
          if (session != null || getAllowSessionCreation()) {
              request.getSession().setAttribute(
                      SPRING_SECURITY_LAST_USERNAME_KEY,
                      TextEscapeUtils.escapeEntities(username));
          }
          setDetails(request, authRequest);
  
          return this.getAuthenticationManager().authenticate(authRequest);
      }
  
      @Override
      public void doFilter(ServletRequest req, ServletResponse res,
              FilterChain chain) throws IOException, ServletException {
          final HttpServletRequest request = (HttpServletRequest) req;
          final HttpServletResponse response = (HttpServletResponse) res;
          if (request.getMethod().equals(POST)) {
              // If the incoming request is a POST, then we send it up
              // to the AbstractAuthenticationProcessingFilter.
              super.doFilter(request, response, chain);
          } else {
              // If it's a GET, we ignore this request and send it
              // to the next filter in the chain. In this case, that
              // pretty much means the request will hit the /login
              // controller which will process the request to show the
              // login page.
              chain.doFilter(request, response);
          }
      }
      ...
      }
  </code></pre>
  
  <p>Context xml:
          
              
              
              
          </p>
  
  <pre><code>    &lt;bean id="loginUrlAuthenticationEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint"&gt;
      &lt;property name="loginFormUrl" value="/portalLogin.html"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="MyFilter" class="com.infosys.itrade.core.dao.security.MyFilter"&gt;
          &lt;property name="authenticationManager" ref="authenticationManager"/&gt;
          &lt;property name="authenticationSuccessHandler" ref="successHandler"&gt;&lt;/property&gt;
          &lt;property name="authenticationFailureHandler" ref="failureHandler"&gt;&lt;/property&gt;
          &lt;property name="filterProcessesUrl" value="/signIn"&gt;&lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="successHandler" class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler"&gt;
          &lt;property name="defaultTargetUrl" value="/welcome.html" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="failureHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler"&gt;
          &lt;property name="defaultFailureUrl" value="/loginfailed.html" /&gt;
      &lt;/bean&gt;
  
      &lt;security:authentication-manager alias="authenticationManager"&gt;
          &lt;security:authentication-provider
              user-service-ref="LoginUserDetailsService"&gt;
          &lt;/security:authentication-provider&gt;
      &lt;/security:authentication-manager&gt;
  
      &lt;bean id="LoginUserDetailsService"
          class="com.infy.itrade.core.service.login.LoginServiceImpl"&gt;
          &lt;property name="logindao" ref="loginDAOImpl" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="loginDAOImpl" class="com.infy.itrade.core.dao.login.LoginDAODBImpl"&gt;
          &lt;property name="sessionFactory"&gt; &lt;ref bean ="sessionFactory"/&gt; &lt;/property&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>What am i missing here?</p>
  
  <p>[Update]: Since i couldn't get this working i changed my approach as suggested by others here i.e. I'm avoiding the architectural flow for login module alone. But right now i'm facing a different issue with the new approach for which i'll ask a different question. Thanks.</p>