简体   繁体   中英

Grails spring security - how to redirect to the same page after login?

I believe if a page requires authentication, the spring security will automatically take the user to the log in page and after successful log in redirect to the requested page. In this case I have a page (say, /products/productx) that is accessible by both public as well as logged in users. However, if the user is logged in, the page will show more information regarding 'productx'. So in the 'productx.gsp' I provide a link for the user to log in like this:

To see more info you must be <g:link controller="login" action="auth">logged in</g:link>

After the user logs in by clicking the link how do I redirect the user to '/products/productx'? I do not want to set the 'defaultTargetUrl' in 'Config.groovy', as I want to use the above feature in several different situations (redirecting to different pages after log in). Any input will be appreciated, thank you!

You'll most likely need to set defaultTargetUrl in Config.groovy regardless if you're determined to use the login page method; my solution for this issue was to use an interstitial controller action in a @Secured controller to examine the relevant principal data and redirect the user to the appropriate location based on business logic.

If you're looking to just keep them on the same page after logging in, perhaps the ajax authentication methods might be more appropriate in this instance?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM