Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Session in Spring Boot application

My project is a springboot application with basic crud functionality with a login page using HTML and CSS only. How do I add session for login and logout

like image 638
Akhilesh Avatar asked Apr 22 '17 17:04

Akhilesh


People also ask

What is a session in spring boot?

Spring Session consists of the following modules: Spring Session Core - provides core Spring Session functionalities and APIs. Spring Session Data Redis - provides SessionRepository and ReactiveSessionRepository implementation backed by Redis and configuration support.

Can we use session in spring boot?

Spring Session Hazelcast Moreover, in order to manage Spring Boot Session Management, the HTTPSession will be used to store session information with persistent storage (Mysql) by using Spring Session JDBC. (To achieve Spring Boot Session Management using Redis, refer this example.)

How does Spring Boot generate session ID?

getSessionId(); This relies on Spring's RequestContextHolder , so it should be used with Spring MVC's DispatcherServlet or you should have a RequestContextListener declared. Also session will be created if not exists.


2 Answers

As others have suggested, you can use Spring security. Or if you don't want to deal with the complexities of Spring Security, you can get HttpSession object in your controller's handlers' methods' arguments. You can set values or objects in that session using HttpSession.setAttribute("name you want to refer to", actual value or object) once a user logs in. And when a user presses logout, you can use HttpSession.invalidate(); to finish the session.

like image 132
Faraz Avatar answered Oct 06 '22 03:10

Faraz


The best solution to this would be the use of Spring Security. Take a look at this: https://spring.io/guides/gs/securing-web/.

like image 44
Derek Mwachinga Avatar answered Oct 06 '22 03:10

Derek Mwachinga