1

I have a case in developing a project. I have some courses link in footer of my application and whenever a user click on the same we will show him the login or sign up pop up(if he is not logged in) or redirect to the course page (If he is logged in). So now what i want that whenever a user click on course before login he/she should be redirected to the course detail page just after login.

I am using spring security for authentication. Let me know how can i achieve this using spring security authentication?

Harish Garg
  • 139
  • 8

1 Answers1

0

this is the trivial use case.

the links that you have in the footer should be to a "secured zone" (for example ".../secured/page1", ".../secured/page2"). you configure in your security-beans.xml what is this "secured zone".

when user clicks and tries to get to a secured resource, spring checks whether the user has authenticated. if not, it redirects him to the login page. (in the login page you can add link to "create account").

this is how you configure the "secured zone":

<sec:http authentication-manager-ref="authenticationManager">
   <sec:intercept-url pattern="/secured/**" access="ROLE_USER" />
   ...

Spring saves the "redirect url", and if user is not logged in he is redirected to the login page, and after login spring uses the "redirect uri" and sends him to the 'secured resource' which is the course page in your case. In detail: after login, class SavedRequestAwareAuthenticationSuccessHandler uses RequestCache that holds the original request URI.

If you need popup, see this: Spring security opening a popup login

Community
  • 1
  • 1
OhadR
  • 8,276
  • 3
  • 47
  • 53
  • Yes you are right but my case is little bit different. I need to send the user to course detai page if he/she click on course link and then login. If he directly login he will be redirected to home page as per our flow – Harish Garg Oct 29 '14 at 17:12
  • you said that whenever a user click on course before login, he should be redirected to the course detail page just after login. if that is the case, i've updated my answer - have a look. – OhadR Oct 30 '14 at 06:44
  • Let me clear you the case : I have some links on my login page and a form for login (where i am using the spring security). Now when user login directly he/she will be logged in and will be redirected to home page. But if user click on the link before login then a pop up will be opened and user can login from there but now we need to redirect it to some other page. How can i achieve this? If you have any running demo please provide it – Harish Garg Oct 30 '14 at 17:27
  • the pop up is something crucial? is it a requirement? redirect to the login page is good enough? – OhadR Oct 30 '14 at 18:31
  • http://stackoverflow.com/questions/25607154/spring-security-opening-a-popup-login – OhadR Oct 30 '14 at 19:57
  • problem solved using this link : http://stackoverflow.com/questions/17779366/spring-security-targeturlparameter-does-not-redirect – Harish Garg Oct 31 '14 at 19:04