ChatGPT解决这个技术问题 Extra ChatGPT

What's the difference between JWTs and Bearer Token?

I'm learning something about Authorization like Basic, Digest, OAuth2.0, JWTs, and Bearer Token.

Now I have a question.

You know the JWTs is being used as an Access_Token in the OAuth2.0 standard. JWTs appears at RFC 7519, and Bearer Token is at RFC 6750 .

For example, the Bearer:

Authorization: Bearer <token>

I used to send token to server by AJAX or add token to the query string of the url. I know that a token can also be sent by adding it to a request header. Does that mean that token should be added to Authorization Bearer header?

Could you please tell me the relationship between JWTs and Bearer Token? Thanks a lot.


r
rmharrison

Short answer

JWTs are a convenient way to encode and verify claims.

A Bearer token is just string, potentially arbitrary, that is used for authorization.

Context (story time)

A few years ago, before the JWT revolution, a <token> was just a string with no intrinsic meaning, e.g. 2pWS6RQmdZpE0TQ93X. That token was then looked-up in a database, which held the claims for that token. The downside of this approach is that DB access (or a cache) is required everytime the token is used.

JWTs encode and verify (via signing) their own claims. This allows folks to issue short-lived JWTs that are stateless (read: self-contained, don't depend on anybody else). They do not need to hit the DB. This reduces DB load and simplifies application architecture because only the service that issues the JWTs needs to worry about hitting the DB/persistence layer (the refresh_token you've probably come across).


Thanks, and how about Mac in Authorization, are the Mac and Bearer the same?
Best answered elsewhere, e.g.: dzone.com/articles/oauth-20-bearer-token-profile
I beg your pardon, sir after some years but what secret key? If the secret key is just one, it is ok. But what if we have some companies with which we share our API with different secret key for each one? What should we do? Should we store the secret keys in Db? If so, db relation still exists. Doesn't it?
T
Thilo

JWT is an encoding standard for tokens that contains a JSON data payload that can be signed and encrypted.

JWT can be used for many things, among those are bearer tokens, i.e. a piece of information that you can present to some service that by virtue of you having it (you being the "bearer") grants you access to something.

Bearer tokens can be included in an HTTP request in different ways, one of them (probably the preferred one) being the Authorization header. But you could also put it into a request parameter, a cookie or the request body. That is mostly between you and the server you are trying to access.


Then how to parse the token in Authorization header, there is a Bearer , I have to use String.slice(), is there any middleware to parse it?
I'm using Node.js
Use the auth-header package if you want a minimal parser
Or just use String.slice() :)
I beg your pardon, sir after some years but what secret key? If the secret key is just one, it is ok. But what if we have some companies with which we share our API with different secret key for each one? What should we do? Should we store the secret keys in Db? If so, db relation still exists. Doesn't it?
C
Community

JWTs work with two types of token, Parameter Token: Access token pass as parameter. Bearer Token: it's pass in header with 'Bearer'.

Please read the following question also:

What are Bearer Tokens and token_type in OAuth 2?