Bearer Token Là Gì


What is JSON Web Token?

JSON Web Token ( is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. can be signed using a secret (with the HMAC algorithm) or a public/private key pair using RSA or ECDSA.

Bạn đang xem: Bearer token là gì

Although can be encrypted to also provide secrecy between parties, we will focus on signed tokens. Signed tokens can verify the integrity of the claims contained within it, while encrypted tokens hide those claims from other parties. When tokens are signed using public/private key pairs, the signature also certifies that only the party holding the private key is the one that signed it.

When should you use JSON Web Tokens?

Here are some scenarios where JSON Web Tokens are useful:

Authorization: This is the most common scenario for using Once the user is logged in, each subsequent request will include the, allowing the user to access routes, services, and resources that are permitted with that token. Single Sign On is a feature that widely uses nowadays, because of its small overhead and its ability to be easily used across different domains.

Information Exchange: JSON Web Tokens are a good way of securely transmitting information between parties. Because can be signed—for example, using public/private key pairs—you can be sure the senders are who they say they are. Additionally, as the signature is calculated using the header and the payload, you can also verify that the content hasn"t been tampered with.

What is the JSON Web Token structure?

In its compact form, JSON Web Tokens consist of three parts separated by dots (.), which are:


Therefore, a typically looks like the following.


Let"s break down the different parts.


The header typically consists of two parts: the type of the token, which is, and the signing algorithm being used, such as HMAC SHA256 or RSA.

For example:

{ "alg": "HS256", "typ": ""}Then, this JSON is Base64Url encoded to form the first part of the


The second part of the token is the payload, which contains the claims. Claims are statements about an entity (typically, the user) and additional data.There are three types of claims: registered, public, and private claims.

An example payload could be:

{ "sub": "1234567890", "name": "John Doe", "admin": true}The payload is then Base64Url encoded to form the second part of the JSON Web Token.

Do note that for signed tokens this information, though protected against tampering, is readable by anyone. Do not put secret information in the payload or header elements of a unless it is encrypted.


To create the signature part you have to take the encoded header, the encoded payload, a secret, the algorithm specified in the header, and sign that.

For example if you want to use the HMAC SHA256 algorithm, the signature will be created in the following way:

HMACSHA256( base64UrlEncode(header) + "." + base64UrlEncode(payload), secret)The signature is used to verify the message wasn"t changed along the way, and, in the case of tokens signed with a private key, it can also verify that the sender of the is who it says it is.

Putting all together

The output is three Base64-URL strings separated by dots that can be easily passed in HTML and HTTP environments, while being more compact when compared to XML-based standards such as SAML.

Xem thêm: Cardiovascular Disease Là Gì, Bệnh Tim Mạch (Cvd) Ở Việt Nam

The following shows a that has the previous header and payload encoded, and it is signed with a secret.


If you want to play with and put these concepts into practice, you can use Debugger to decode, verify, and generate


How do JSON Web Tokens work?

In authentication, when the user successfully logs in using their credentials, a JSON Web Token will be returned. Since tokens are credentials, great care must be taken to prevent security issues. In general, you should not keep tokens longer than required.

You also should not store sensitive session data in browser storage due to lack of security.

Whenever the user wants to access a protected route or resource, the user agent should send the, typically in the Authorization header using the Bearer schema. The content of the header should look like the following:

Authorization: Bearer This can be, in certain cases, a stateless authorization mechanism. The server"s protected routes will check for a valid in the Authorization header, and if it"s present, the user will be allowed to access protected resources. If the contains the necessary data, the need to query the database for certain operations may be reduced, though this may not always be the case.

If the token is sent in the Authorization header, Cross-Origin Resource Sharing (CORS) won"t be an issue as it doesn"t use cookies.

The following diagram shows how a is obtained and used to access APIs or resources:


When the authorization is granted, the authorization server returns an access token to the application.The application uses the access token to access a protected resource (like an API).

Do note that with signed tokens, all the information contained within the token is exposed to users or other parties, even though they are unable to change it. This means you should not put secret information within the token.

Why should we use JSON Web Tokens?

Let"s talk about the benefits of JSON Web Tokens ( when compared to Simple Web Tokens (SWT) and Security Assertion Markup Language Tokens (SAML).

As JSON is less verbose than XML, when it is encoded its size is also smaller, making more compact than SAML. This makes a good choice to be passed in HTML and HTTP environments.

Security-wise, SWT can only be symmetrically signed by a shared secret using the HMAC algorithm. However, and SAML tokens can use a public/private key pair in the form of a X.509 certificate for signing. Signing XML with XML Digital Signature without introducing obscure security holes is very difficult when compared to the simplicity of signing JSON.

JSON parsers are common in most programming languages because they map directly to objects. Conversely, XML doesn"t have a natural document-to-object mapping. This makes it easier to work with than SAML assertions.

Regarding usage, is used at Internet scale. This highlights the ease of client-side processing of the JSON Web token on multiple platforms, especially mobile.

Xem thêm: Ống Upvc Là Gì ? Cấu Tạo Và Đặc Tính Của Ống Nhựa Upvc Ống Nước Nhựa Pvc,Chịu Nhiệt Ppr,Hdpe,Upvc Là Gì

Comparison of the length of an encoded and an encoded SAML

If you want to read more about JSON Web Tokens and even start using them to perform authentication in your own applications, browse to the JSON Web Token landing page at Auth0.