Storing and Retrieving Data in Session using Spring MVC - spring

Want to save User information in session and want to get that back from session using Spring MVC 3.
Can any one give an example
But this is giving an exception
org.springframework.web.HttpSessionRequiredException: Session attribute 'user' required - not found in session

Related

Spring Boot - How to save an audited entity as "System" when using user based auditing?

I'm working with an audited entity in my Spring Boot app (#CreatedBy, #LastModifiedBy etc.) and setting the current auditor with a session scoped bean based on the current logged in user. The auditing with a logged in user works just fine. But I'm also periodically pulling data with #Scheduled and I need to save the pulled data as the audited entity into my DB. The problem is that trying to save an audited entity without a logged in user still calls getCurrentAuditor() in AuditorAwareImpl which gets the current user from a session scoped bean, which is set when the user logs in, but beacause there is no user or session it causes this error:
Scope 'session' is not active for the current thread;
So basically I need my audited entity to be able to be saved by logged in users but also by the "System" (without a logged in user). Is there a reasonable solution to this problem or is my approach just wrong?

Spring SessionAttributes or httpsession

In my Spring MVC project I have a jsp page , registration page , in which there is a form. In this form the user inserts his data (name, surname and so on).I created a bean User and I bound the form with this bean.But now I have a problem.How can i put the user object into a session in a method of a controller?
Can I use #SessionAttributes,httpsession or I set the scope of the bean to session? How can i solve it?Sorry for my English. Thanks
You can use HttpSession so just need to set your user object in your session and access anywhere into the system.
Refer this httpsession documentation for setting session value.

Set User Object in session using Spring

I would like to set the User Object which contains the details of the user into session in my Spring Application.
I would like to use this session object when I would like to retrieve the details of the logged in user on various JSP pages.
I am using Spring 3 and Spring Security 3
I would like to set the User object in the session from my custom authentication class which is not a controller
How can I achieve this?
I assume you have some implementation of UserDetailsService class. You can return any User object implementing UserDetails from loadUserByUsername(). This object is then automatically placed in your HTTP session. It can then be retrieved with:
User user = (User)SecurityContextHolder.
getContext().getAuthentication().getPrincipal();
Spring Security handles everything you need automatically.

GWT - How to check session in a datasourceservlet?

I have a visualization DataSourceServlet in a GWT application, which is used to generate a data table and return data table to visualization api.
The datasourceservlet can only be accessed by a authenticated user.
I am using getThreadLocalRequest to check for session in other RemoteServiceServlet, but it is not available for DatasourceServlet.
How can i check if the user has a valid session in DatasourceServlet?
According to the javadoc below, DataSourceServlet exposes doGet and doPost methods which contain HttpServletRequest and HttpServletResponse as parameters.
http://code.google.com/apis/chart/interactive/docs/dev/dsl_javadocs/com/google/visualization/datasource/DataSourceServlet.html
You would get the session object from the request parameter like it is done for servlets in general. request.getSession()

Access to User ID in Spring

I'm doing some proof-of-concept work with Spring MVC and security. So far I've managed to write a simple web-app which has a secure webpage which requires a user to login and have the correct role before accessing the database and listing some data. I'm using Spring 2.0.8 by the way.
What I require is that, after the user has logged on, is to access the user principal object for the current session to pass into my DAO layer. I'd like to do this through the standard bean wiring, so it will have to be something determined at runtime.
Any pointers to get started ?
Cheers
Neil
SecurityContextHolder#getContext() will return a SecurityContext associated with the current user request.
From there, you can call getAuthentication().getPrincipal() to get the data associated with the logged-in user.
There is no need to inject any bean, the static method in SecurityContextHolder will take care of accessing the correct thread-local data.

Resources