Home How to redirect to j_spring_security_check
Reply: 0

How to redirect to j_spring_security_check

user3630
1#
user3630 Published in June 18, 2018, 3:36 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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-3.2.xsd">

    <!-- 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" />
    </http>





<beans:bean id="daoAuthenticationProvider"
 class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
  <beans:property name="userDetailsService" ref="userDetailsService"/>
</beans:bean>

<beans:bean id="authenticationManager"
    class="org.springframework.security.authentication.ProviderManager">
  <beans:property name="providers">
    <beans:list>
      <beans:ref local="daoAuthenticationProvider" />
    </beans:list>
  </beans:property>
</beans:bean>


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

</beans:beans>

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.

You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO