Invalidating JSON Web Tokens

前端 未结 28 2404
夕颜
夕颜 2020-11-22 06:17

For a new node.js project I\'m working on, I\'m thinking about switching over from a cookie based session approach (by this, I mean, storing an id to a key-value store conta

28条回答
  •  无人及你
    2020-11-22 07:03

    I did it the following way:

    1. Generate a unique hash, and then store it in redis and your JWT. This can be called a session
      • We'll also store the number of requests the particular JWT has made - Each time a jwt is sent to the server, we increment the requests integer. (this is optional)

    So when a user logs in, a unique hash is created, stored in redis and injected into your JWT.

    When a user tries to visit a protected endpoint, you'll grab the unique session hash from your JWT, query redis and see if it's a match!

    We can extend from this and make our JWT even more secure, here's how:

    Every X requests a particular JWT has made, we generate a new unique session, store it in our JWT, and then blacklist the previous one.

    This means that the JWT is constantly changing and stops stale JWT's being hacked, stolen, or something else.

提交回复
热议问题