Reputation: 6563
I'm creating application using Spring MVC and Hibernate where people can register and store some data in their accounts. I created registration and login processes. And now i have a question, how can I keep user session? I don't want to keep user id in each page of my application. For example: once the user has logged he arrives at page
someurl/overview/1
Where id is user's id. I would like it to looks like this for each logged user:
someurl/overview/
What are the best practices how to save user data in session using Spring and save it if user closed site and then opened it?
Thanks in advance.
Upvotes: 2
Views: 4990
Reputation: 2087
You can save user data in session by a number of ways.. simplest would be:
public ModelAndView yourMethod(HttpSession session){
session.setAttribute("user", 1);
return new ModelAndView("home");
}
you will be available with session all the time if you are using spring components, so just add this parameter and use it.
EDIT: can also use Spring's session attributes: http://fruzenshtein.com/spring-mvc-session/ if it satisfies your requirements.
EDIT(Thanks to @Serge Ballesta for putting it in comments, Just updating the post FYI)
even if API does not enforce it, specification requires that all session attributes be Serializable. Think of that if storing more than an id.
Upvotes: 4