JWT Generate Tokens using a Tokenization Method

To generate a token using the JWT method, you can use any arbitrary library that implements the web standard RFC7519 JWT. You can refer to libraries such as https://jwt.io. For the Payload, include the token information, use an encryption key for the Secret, and apply the HS256 algorithm to generate the token.

Code Example

The following is an example of encrypting the token information using the encryption key "this_is_test_secret_key_at_least_32_char".

{
    "uid": "test-user-2",
    "fullname": "박토끼",
    "email": "tokki.park@test.com",
    "ts": 1596611792131
}

Python

The following code is an example of generating a JWT token using the pyjwt library (https://github.com/jpadilla/pyjwt).

```python
import jwt

token = jwt.encode({
    "uid": "test-user-2",
    "fullname": "박토끼",
    "email": "tokki.park@test.com",
    "ts": 1596611792131
}, 'this_is_test_secret_key_at_least_32_char', algorithm='HS256')

print(token.decode('utf-8'))
# Output: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiJ0ZXN0LXVzZXItMiIsImZ1bGxuYW1lIjoiXHViYzE1XHVkMWEwXHViMDdjIiwiZW1haWwiOiJ0b2traS5wYXJrQHRlc3QuY29tIiwidHMiOjE1OTY2MTE3OTIxMzF9.ve6WJli1HrtU03SlWuDKAmYxFSM6dmxOe-1DGH65J8o
# The output value may vary depending on the time, so please verify it at <https://jwt.io>.
```

C#

The following code is an example of generating a JWT token using the System.IdentityModel.Tokens.Jwt library (https://www.nuget.org/packages/System.IdentityModel.Tokens.Jwt/).

```csharp
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Text;

namespace jwt_test
{
    class Test
    {
        static void Main(String[] args)
        {
            var tokenHandler = new JwtSecurityTokenHandler();
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Claims = new Dictionary<string, object>
                {
                    {"uid", "test-user-2" },
                    {"fullname", "박토끼" },
                    {"email", "tokki.park@test.com" },
                    {"ts", 1596611792131L },
                },
                SigningCredentials = new SigningCredentials(
                    new SymmetricSecurityKey(Encoding.UTF8.GetBytes("this_is_test_secret_key_at_least_32_char")),
                    SecurityAlgorithms.HmacSha256Signature
                )
            };

            var stoken = tokenHandler.CreateToken(tokenDescriptor);
            var token = tokenHandler.WriteToken(stoken);

            Console.WriteLine(token);
            // Output: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiJ0ZXN0LXVzZXItMiIsImZ1bGxuYW1lIjoi67CV7Yag64G8IiwiZW1haWwiOiJ0b2traS5wYXJrQHRlc3QuY29tIiwidHMiOjE1OTY2MTE3OTIxMzEsIm5iZiI6MTU5Njc2NzQwMSwiZXhwIjoxNTk2NzcxMDAxLCJpYXQiOjE1OTY3Njc0MDF9.5Ol83EyKiRWc6FoSJPEqv4zJFlIowMDKmvVPsomQVWQ
            // The output value may vary depending on the time, so please verify it at <https://jwt.io>.
        }
    }
}
```

Java

The following code is an example of generating a JWT token using the jjwt library (https://github.com/jwtk/jjwt).

```java
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

public class Main {
    public static void main(String args[]) {
        String token = Jwts.builder()
          .setHeaderParam("typ", "JWT")  // Optional
          .claim("uid", "test-user-2")
          .claim("fullname", "박토끼")
          .claim("email", "tokki.park@test.com")
          .claim("ts", 1596611792131L)
          .signWith(SignatureAlgorithm.HS256, "this_is_test_secret_key_at_least_32_char".getBytes())     
          .compact();

        System.out.println(token);
        // Output: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiJ0ZXN0LXVzZXItMiIsImZ1bGxuYW1lIjoi67CV7Yag64G8IiwiZW1haWwiOiJ0b2traS5wYXJrQHRlc3QuY29tIiwidHMiOjE1OTY2MTE3OTIxMzF9.akIXRvSad0wjv6y86ko7-KkJHwjMfTPDwXQOYOlixHQ
        // The output value may vary depending on the time, so please verify it at <https://jwt.io>.
    }
}
```

Last updated