简体   繁体   中英

Managing session in java spring

I am a newbie in Spring, so I am explaining my requirement.

My requirement is to create an web - application in spring, where sign in / log in should be from 1 system. If user gets logged in , in another browser/system the previous should be out of session.

How can I achieve this ? Any document link or any concept that I need to learn will be helpful.

You can use spring-session-redis - it's an API with implementation for managing a user's session information with Redis .

Application uses command line to execute GET request on same server running on different ports , to explain how the session works. You can build POST , DELETE and other HTTP request.

HttpServletRequest and HttpServletResponse interfaces are implemented by the web-container wrapping your application. If you are using Spring boot it uses embedded Tomcat instance, if you are deploying your WAR application on Weblogic, they are implemented by Weblogic. However, the interfaces are the same and depending on your configuration on those containers, they maintain Session objects. Using this session object, you can add key-value pairs and maintain same set coming from the same user in consequent requests.

 @RequestMapping(value = "/hello")     
 public Object hello(HttpServletRequest request, HttpServletResponse response){request.getSession().setAttribute("key",mySessionObject);}

when you want to retrieve the session object you have just added, just use getAttribute() method with same key.

request.getSession().getAttribute("key")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM