spring


Declaring HttpSession Object in Controller


We have defined a controller and need to declare the HttpSession object. I can autowire it or pass as an object to the method. What is the difference between the two options and which is preferable?
Option 1
#Controller
public class UserController {
#Autowired
HttpSession session;
..
..
}
Option 2
#Controller
public class UserController {
#RequestMapping(value="/user", method=RequestMethod.GET)
public #ResponseBody User getUser(HttpSession session) {
..
..
}
}
I am a beginner, I think it is the second
Because, the first way, the session is shared, that is, all the methods inside the class can operate the session, for example, increase and modify. Is thread insecure.
In a Controller, you should use Option2.
A session is start from a http request begin, end until the request stop, but the controller is available until the application stoped. So you should not autowired the HttpSession in Controller.
One of the situation you can autowired the http session like that:
#Component
#Scope(value=WebApplicationContext.SCOPE_SESSION)
public class CurrentUserHolder{
#Autowired
private HttpSession session;
public User currentUser() {
return this.get();
}
#Override
public User get() {
return (User)session.getAttribute("currentUser");
}
}
So you can get current user in a service which can not get the session:
#Service
public class UserService{
private #Autowired CurrentUserHolder currentUserHolder;
......
}

Related Links

Spring Data Couchbase and Spring Data MongoDB at the same time use
Http Batch Operations for Spring Framework
From TransactionProxyFactoryBean to a Declarative transaction management?
Spring MVC: how secure access to static content with authorization check
Autowiring Optional<object> in Spring 4.1
Applying groupBy to Spring specification query
Spring Java configuration - Autowire issue
STOMP or XMPP - Over websocket
How to map a SpringServlet to root and also serve static content in root?
Spring Mongodb: How to configurer mongoDB with MongoClientFactoryBean
How to get Spring Security auto logout time, when using invalidate-session=“false”?
Spring Batch to read and retry on failuire over a xml list(and not entire file)
DB2 SQL Error: SQLCODE=-270 exception thrown by jpa paging item reader
I Am trying to configure Mongo readonly connections to the secondary using Spring Jdbc on a Java Enterprise Application
My Spring Boot app cannot see the Thymeleaf templates
What is the magic behind Field #Autowired

Categories

HOME
semantic-ui
npm
reverse-engineering
uibutton
reportportal
google-sheets-api
javamail
apple-numbers
java-home
apiconnect
requirejs
gimp
slim-3
click
intellij-plugin
finite-group-theory
image-recognition
flann
squarespace
cloudflare
atlassian-plugin-sdk
primes
badge
opencart2.3
rhandsontable
widevine
pdflatex
fabric-digits
openshift-enterprise
mmdrawercontroller
mms
android-preferences
ical-dotnet
knockout-3.0
bing-translator-api
crop
context-free-language
dojox.grid.datagrid
timesten
ti-basic
broadcast
multi-agent
scd
paas
communication-protocol
lotus
efxclipse
git-annex
json-schema-validator
webkit2
metalsmith
dojox.charting
windows-nt
image-editing
bluesnap
optionbutton
sem
webhdfs
digits
coypu
adobe-indesign
jazz
robocode
multipleselection
start-job
cppdepend
dbsetup
dvcs
crystal-reports-10
arangodb-php
layered
ng-hide
backbone-relational
manage.py
fortran77
magickwand
fluidsynth
trailing-slash
phpsh
returnurl
gdt
responsetext
objectbrowser
text-size
tcxgrid
datacontract
galaxy-tab
ihttphandler
svn-hooks
camtasia
database-diagramming
document-library

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App