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 JPA underlying mechanism without implementation
Accessing Session attributes in another variable using scope
Manage multiple request at the same time to spring mvc controller method
Spring Boot runnable jar can't load terracotta
spring addResourceHandler for text files
Extremely long exceptions logs in Spring Boot
Can't get spring-boot /health indicator to display full content
Always invalid username and password in Spring Security Oauth2
Access to Microservices via Eureka Server
How spring community manages version of the framework
Can I force refresh content/page using Spring Boot and JSP?
spring cloud dataflow source file | sink gpfdist speed?
Spring LdapTemplate - is ldap reachable?
Spring MVC to REST
Unexpected and unwanted div element in return from Spring RestController
issue while calling soap service using spring webservicetemplate

Categories

HOME
blockchain
webrtc
google-contacts
steam
qpython3
amazon-product-api
mainframe
codeeffects
ios10
hyperledger-fabric
gspread
schemacrawler
avl-tree
eval
metronic
keystore
xamarin-studio
iso
wordpress-theming
gatsby
webmethods
titan
quartus
factor-analysis
rails-activerecord
prediction
npm-install
tampermonkey
hanami
bus-error
classpath
hana-studio
m2e
spreedly
mapguide
gettext
elfinder
dql
poco-libraries
magiczoomplus
visualstudio.testtools
polyfills
spring-bean
upsert
facebook-chatbot
walmart-electrode
udev
selenium-firefoxdriver
efxclipse
hspec
icefaces
c#-interactive
pax-web
gherkin
contour
supportmapfragment
concur
eml
handlebars.java
altbeacon
self-hosting
java.util.calendar
persistence.xml
jenkins-scriptler
hyperthreading
enyo
qbfc
subresource-integrity
integral
manjaro
guzzle6
uptodate
low-level
alphablending
left-recursion
dmp
mov
funq
unison
zipalign
svcutil.exe
cdata
jqmobi
swrl
vmware-server
iconv
zend-lucene
vertical-scrolling
shim
hobo
flash-cs5.5
jquery-selectbox
path-manipulation
printing-web-page
paintcomponent
port-scanning
squeel
unreachable-code
microsoft.ink
method-signature
divx

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