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
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.
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.)
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.
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.
The best solution to this would be the use of Spring Security. Take a look at this: https://spring.io/guides/gs/securing-web/.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With