Reputation: 7719
I'm using JWT authentication for my Spring Boot application. The front-end is going to be a single-page-application.
Should I handle logout functionality on the server side? As far as I understand, there's no way to invalidate a JWT token unless we have a stateful server (storing logged out tokens for the maximum lifetime of a token).
The SPA passes the JWT token every time making a request in its header, and it can delete it from the localStorage when the user visits /logout
without making a call to the server.
What are the potential issues? Is this idea used anywhere else? What is the best practice?
Upvotes: 2
Views: 1884
Reputation: 862
First of all you must decide wether you want a statefull server, or not.
this is easy. Just send a logout request to the server and throw the session away. That's it. That's the safest way.
I like stateless servers because you don't have to manage the state. But of course you have a tradeoff. In this case the securety. There is no way to logout because you don't have a session that you can invalidate on the server side.
So an attacker which steels your JWT-token can use the session until it ends and there's nothing to do to prevent this.
But you can do something to avoid that the atacker can get the JWT Token. Here are some things that you can do and that you have done already right
But these mechanisms are all there to make it harder for an atacker. A real logout is not possible.
If you use JWT correctly your server will have a "state". You have to define a secret that is the same on all servers. That's something you must be aware of, if you use multiple servers.
Upvotes: 5