Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring is not redirecting to default target url?

I am using spring security for authentication. authentication is working fine. but after authentication it is not redirecting to the html that have mentioned using default target url in spring security configuration file. i am getting simple message Success. but not the html page that have configured. i have added below line for redirection.

<form-login login-page="/login.jsp" default-target-url="/welcome.html"/>

am i missing anything to configure.

Thanks!

like image 771
user1016403 Avatar asked Dec 28 '11 15:12

user1016403


3 Answers

If a user is sent to the login page after requesting a protected resource, they will be sent to the originally requested page after successful login. The default-target-url will only be used if the user logged in without requesting a protected resource first (i.e. they navigated directly to the login page). If you always want to go to the default-target-url you can specify always-use-default-target="true" as shown in the example below

<form-login login-page="/login.jsp" 
            default-target-url="/welcome.html" 
            always-use-default-target="true"/>
like image 98
Rob Winch Avatar answered Jan 04 '23 13:01

Rob Winch


I had similar problem and above answer gave me a hint, but since I am using configs directly from Java class following helped me:

In docs.spring.io you can find:

defaultSuccessUrl(String defaultSuccessUrl, boolean alwaysUse)

Hence when changed

.defaultSuccessUrl("/home")

to

.defaultSuccessUrl("/home", true)

problem was solved.

like image 42
Ula Avatar answered Jan 04 '23 11:01

Ula


An interesting gotcha here is if the browser requests a resource that requires an authenticated session - e.g. requesting a javascript file which requires the user to be logged in, this would generate a 403 error which if you have error pages configured in spring or web.xml would redirect the user to - however the user would never see this.

Then when the user does log in, the last thing spring thinks the user requested was an error page and so the user gets directed to the error page and not the default page as you configure in spring

like image 33
brommersman Avatar answered Jan 04 '23 13:01

brommersman