1

I am using jwt token with spring security tuto .

After successfully generating the token on login and passing it to my angular application in localStorage object. I have one problem that on logout I delete the token but still using the same token I can hit secured api through Postman.

How can i delete the user token when he goes to logout url of the front application ?

Updated: I created this logout function in my service:

public void logout(HttpServletRequest request){
    if(request != null){
        String authHeader = request.getHeader("Authorization");
        if (authHeader != null) {
            String tokenValue = authHeader.replaceFirst("(?i)" + "bearer", "").trim();
            log.info("Token to remove value = {}", tokenValue);
            OAuth2AccessToken accessToken = tokenStore.readAccessToken(tokenValue);
            if (accessToken != null) {
                tokenStore.removeAccessToken(accessToken);
                log.info("Token has been removed");
            }
            else{
                log.info("accessToken not found");
            }
            log.info("logging ");
        }
        
    }
    
    
}

The logic thing to happen is that the token provided to the logout url gets deleted from my mongo database but when i login again i get the same token generated.

Ganesh
  • 5,808
  • 2
  • 21
  • 41
Youssef Boudaya
  • 887
  • 2
  • 17
  • 29

2 Answers2

2

JWT is a stateless approach so this means validating JWT token you should not really go into database, as this loses the statelessness. So this is in one word. Not possible. That is why your JWT token should be short lived.so that, the token will be inactive within sometime.

1

We can not control JWT token that is provided by Server. Only thing that we can do is delete it for that particular session (either it's browser login session or tab specific).

In front end we check for valid JWT token which might also have user info for every user login and going delete (only in browser storages like cookies, local storage or session storage) it on particular session expiry like logout but we are not going to delete on server side because it's Stateless.

We will not validate new token with old one, because it might be same if it did not exceed the token expiration time.

For your actual question answer is, if we delete the token in frond end that intend to be stop user for that session and request another token if he login again but that does not mean to be invalidate or expire currently using token for it's lifetime.

So token still might be valid if you take the same to request secure api.

JWT token only controlled by it's expiration time setup on server side. You can check here for more info about it:

Don't confuse between deleting and setting the expiration time. There might be lesser situations to use the same token from different devices (like application login, postman and etc..).

Ganesh
  • 5,808
  • 2
  • 21
  • 41