Jakub Pilimon
Jakub Pilimon

Reputation: 141

Spring: Object stored and used in Sitemesh decorator

This is probably a newbie question.

I have a table USER which contains info about login, pass and authorities. Depending on authority or role, detail information about each user can be found in one of following: Teacher, Student, Parent. When the User logs in, the information stored in USER table can be easly taken from security context.

I want to display first name and last name all the time in the header after log in - these can be fetched from the other tables.

My question is this: how do I handle storing one of these objects in session all the time? Or is it okay just to store User (its stored by spring) and then fetch particular table every time I need detail information?

I use spring security 3, hibernate, jsp, sitemash.

For more clarification:

I know how to deal with logged user and to restrict some content. Login details (id, pass, role) are stored in USER table and this is ok - I can fetch it and show whereever I want. The problem is that the details about a particular user (address, name, email, etc) are stored in in another table (STUDENT, TEACHER, PARENT - depending on the role in USER table). This is what I want to know on every page - for example to show his/her name.

Upvotes: 3

Views: 209

Answers (1)

Jakub Pilimon
Jakub Pilimon

Reputation: 141

TO cut it short - 1. you need to extend spring User to provide additional fields. 2. you need to implement UserDetailsService interface and reference it in the security context. 3. Now you can fetch your object in a controller like this: authentication.getPrincipal() - rememebr to cast to your type.

Additionaly - personally i always have AbstracController which is a base for every controller in my project. There, among others, I have method which returns current principal.

Upvotes: 1

Related Questions