Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails Spring Security - Always redirect to specific page after login?

I have a grails application that is using the spring security plugin for authentication. If my session expires, and I click a link in the application it takes me to the login screen then tries to redirect to the page I was on previously.

I would like to configure spring security to always redirect to the home page instead of the last page the user clicked on. Is there a setting that controls this behavior?

like image 950
Michael Avatar asked Jun 01 '14 18:06

Michael


People also ask

How do I redirect a requested URL after login?

The most common ways to implement redirection logic after login are: using HTTP Referer header. saving the original request in the session. appending original URL to the redirected login URL.

How do I redirect after login in spring boot?

By default, Spring Security will redirect after login to the secured ressource you tried to access. If you wish to always redirect to a specific URL, you can force that through the HttpSecurity configuration object. Assuming you are using a recent version of Spring Boot, you should be able to use JavaConfig.

Which of the following option would redirect user to the predefined home HTM page after each successful login?

2.1. The part of this configuration to focus on is the defaultSuccessUrl() method. After a successful login, any user will be redirected to homepage. html.

What is the use of spring boot security authentication Handler class?

Interface AuthenticationSuccessHandler Strategy used to handle a successful user authentication. Implementations can do whatever they want but typical behaviour would be to control the navigation to the subsequent destination (using a redirect or a forward).


3 Answers

Beyond setting the defaultTargetUrl you also need to tell Spring Security to force the use of that default target URL. Your Config.groovy should look something like this:

grails.plugins.springsecurity.successHandler.alwaysUseDefault = true
grails.plugins.springsecurity.successHandler.defaultTargetUrl = '/home'

You can research further options using the Spring Security API documentation for the SavedRequestAwareAuthenticationSuccessHandler, should you need them.

UPDATE: Later versions of the plugin use grails.plugin and not grails.plugins

like image 112
Joshua Moore Avatar answered Oct 14 '22 12:10

Joshua Moore


I am using grails 2.4.4 and it should be:

grails.plugin.springsecurity.successHandler.alwaysUseDefault = true
grails.plugin.springsecurity.successHandler.defaultTargetUrl = '/your-url'

Instead of:

grails.plugins.springsecurity.successHandler.alwaysUseDefault = true
grails.plugins.springsecurity.successHandler.defaultTargetUrl = '/your-url'
like image 23
Bhuwan Gautam Avatar answered Oct 14 '22 10:10

Bhuwan Gautam


Yes, there is a setting successHandler.defaultTargetUrl (defaults to root context)

grails.plugin.springsecurity.successHandler.defaultTargetUrl = '/home'

where /home represents route to home page.

like image 2
dmahapatro Avatar answered Oct 14 '22 12:10

dmahapatro