简体   繁体   中英

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

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/ .

弹簧网络安全的简单教程请看这里这里的 链接

It is recommended to use Spring Security.

You can find a lot of example if you search for "spring security tutorial" in google.

For instance it is an offical tutorial with angular js (1.x) https://spring.io/guides/tutorials/spring-security-and-angular-js/

If you don't want to use spring security you have to create a http session and store the logged in user data in http session.

In spring you can inject the HttpSession to your bean and you can add session attributes, or you can create a session scoped bean.

Spring Security is best option for authentication and authorization. For login, if new user got logged in then you need to maintain session for particular user until logout. You can use Spring security session management.

http://www.baeldung.com/spring-security-session

You can set UserDetails object in http.setAttribute("USER_DETAILS", userDetails);

(userDetails is object of UserDetails which is in built class)

You can use this httpSession object like httpSession.getAttribute("USER_DETAILS"); for manipulation further. For logout use can use httpSession.invalidate() method.

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