how many way to access the scope variables in spring-mvc - spring

Some one please me to find out the spring mvc examples,
Because usually, once we log in into the application we will create a session and put some objects into session . we will access later point of time , request scope as well. but spring MVC3 is difficult to understand even documentation also confusing, but every one giving example is basic examples only.

You can access these objects in a JSP/JSTL:
applicationScope
cookie
header
headerValues
initParam
pageContext
pageScope
param
paramValues
requestScope
sessionScope
As well as any request attributes that you add, including model attributes (who's default name is command).
More info here: http://www.informit.com/articles/article.aspx?p=30946&seqNum=7
If you want to access HttpRequest, HttpResponse, HttpSession, add them as arguments to a Spring Controller Handler Method . Spring will pass them in for you.

Related

How to get the current SecurityContext in a Spring Controller without using SecurityContextHolder static methods?

In my application, when a user updates their username, I want to clear that authentication from the security context because the old username was used in basic auth calls.
In my controller doing the update, this is easy enough with
SecurityContextHolder.getContext().setAuthentication(null);
But I'm looking for a way to access the current security context without using static methods for ease of unit testing (not to mention the static call doesn't seem very "springy").
The answer here looks pretty close to what I'm looking for, but I'm hoping there's a way to do this without writing a wrapping class. I also tried to inject SecurityContextHolderStrategy into my controller constructor (as was implied I might be able to do in that answer and the related jira) and there was no bean defined of that type.
So: Is there a way to access the current SecurityContext in a Spring Controller without using SecurityContextHolder static methods?
My version of spring security is 3.2.5.RELEASE.

Proper usage of #SessionAttribute Annotation

I have difficult understand the proper usage of #SessionAttribute annotation.
I wonder does the #SessionAttribute is used to store user authentication object or use to store the form object that exist within the session only.
I want to check whether a use has been login before invoke the handler.
I really confuse between these three class object.
Session scope bean
#SessionAttribute
HttpSession
Please give a proper example of #SessionAttribute usage and pron/cons of each of this
Thanks.
#SessionAttribute is for temporarily storing model objects in the session. Examples include storing a set of search criteria or storing data for a multi-page wizard.
If you're after checking for authentication status, while in theory you could probably hack something together with #SessionAttribute, you're much better off using Spring Security. There are many other authentication and authorization concerns that you aren't addressing if you don't have a security solution in place.

Use cases of Request and Session scoped beans

Can someone list the practical use cases of Request , Session and Global-Session scoped beans ? In most of the projects I have been using singleton and prototype . I understand that request scope beans are instantiated per request and in session scoped beans , the beans are instantiated when a session gets started .
Please enlighten me on the practical aspects .
So far we're using request scoped beans for information that should only be valid on one page like the result of a search or the confirmation of an order. The bean will be valid until the page is reloaded.
A session scoped bean is useful to hold authentication information getting invalidated when the session is closed (by timeout or logout). You can store other user information that you don't want to reload with every request here as well. Or another use case for us is to store a conversation scope in the session scope which we use to persist information between requests but to that we can assign a custom timeout and invalidation condition.
Pretty much any information that needs to be available after the request needs to be stored in the session scope. The only exception we use is with a view scope that stores information in the page's view map to be available after ajax requests for example in wizzards.
Singleton scope however means, that the information exists only once per application so if two users request your page they will access the same information. This is usefull for controllers, as they shouldn't store data anyway.
A prototype scope is the same as initialitzing an object with new, as it is created every time you inject it. We don't use this at all at the moment.

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