Logout in Spring Security

In the previous tutorial, I introduced you to the basic authentication and authorization in Spring Security. If you pay attention, you will see and wonder that after logging in to the application, how will we logout? I will guide you on how to logout in Spring Security in this tutorial!

I also have a simple Spring Security application as follows:

Logout in Spring Security

With Spring Security’s configuration file, the content is as follows:

To logout in Spring Security, we will only need to call the “/logout” request provided by Spring Security by default. Then, all user login information will be cleared and our application will redirect itself to the “/login?logout” request.

To see this clearly, let’s edit the home.jsp file in the folder src/main/webapp/WEB-INF/views/ to add a link for the “/logout” request as follows:

Here, I use the JSTL library to add this link.

The results after running the application, accessing the application, logging in with the “khanh” user will be as follows:

Logout in Spring Security

Now, if you click on the Logout link, you will see the following result:

Logout in Spring Security

This is the confirm window that asking you really want to logout or not? Click Log Out button to confirm your need!

The following result will be as follows:

Logout in Spring Security

Chia sẽ bài viết này ...Share on Facebook
0Tweet about this on Twitter
Share on LinkedIn

Add Comment