Home How to redirect to j_spring_security_check
Reply: 0

How to redirect to j_spring_security_check

user7904 Published in September 21, 2018, 8:03 am

I have a java spring mvc web application and I have implemented the login part using spring security. The version of spring security that I use is 3.2.5. My spring-security.xml file is as follows:

<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    <!-- enable use-expressions -->
    <http auto-config="true" use-expressions="true">
    <intercept-url pattern="/login" access="isAnonymous()" />

        <form-login login-page="/login" default-target-url="/welcome"
            authentication-failure-url="/login?error" username-parameter="username"
            password-parameter="password" />
        <logout logout-success-url="/login?logout" invalidate-session="false" />

        <!-- access denied page -->
        <access-denied-handler error-page="/403" />

<beans:bean id="daoAuthenticationProvider"
  <beans:property name="userDetailsService" ref="userDetailsService"/>

<beans:bean id="authenticationManager"
  <beans:property name="providers">
      <beans:ref local="daoAuthenticationProvider" />

    <!-- Declare an authentication-manager to use a custom userDetailsService -->
        <authentication-provider user-service-ref="userDetailsService">
        <password-encoder hash="bcrypt" />


Now I have a requirement where I will display some content initially and then the user has to login to view the full content. So I provide a sign in link which will bring up the sign in form. But when a user logs in, the default page is shown. I want the user to be redirected to the URL where the user is already in. I have also tried to POST the form to one of my custom controller methods and then redirect to spring security check from there. I intend to store the current URL to session from the method and redirect the user to that method from the default target method of spring security. But redirecting to spring security check is always giving me invalid username or password error since it is not able to authenticate the user. My custom method would be as follows:

@RequestMapping(value = "/edu-login", method = RequestMethod.POST)
public String eduLogin(@ModelAttribute ("username") String username, @ModelAttribute ("password") String password, Model model, HttpServletRequest request, HttpServletResponse response, RedirectAttributes ra)
        //My custom logic to stroe url to session
        ra.addAttribute("username", username);
        ra.addAttribute("password", password);
        return "redirect:/j_spring_security_check";

Is there any solution for my problem. All I want is the user to return to the url on the browser after login.

share|improve this question
  • Could you give an example (URLs) for the flow? What do you mean by I want the user to be redirected to the URL where the user is already in? Is that an static URL for all pages? Do you use the same URL for initially content and full content? – dur Feb 13 at 12:33

1 Answer 1

active oldest votes
up vote 0 down vote
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.389022 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO