Copy {
"uid" : "test-user-2" ,
"fullname" : "박토끼" ,
"email" : "tokki.park@test.com" ,
"ts" : 1596611792131
}
Copy 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' ))
# 出力結果: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiJ0ZXN0LXVzZXItMiIsImZ1bGxuYW1lIjoiXHViYzE1XHVkMWEwXHViMDdjIiwiZW1haWwiOiJ0b2traS5wYXJrQHRlc3QuY29tIiwidHMiOjE1OTY2MTE3OTIxMzF9.ve6WJli1HrtU03SlWuDKAmYxFSM6dmxOe-1DGH65J8o
# 出力結果は時間によって異なる場合があるため、<https://jwt.io>で検証してください。
Copy 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);
// 出力結果: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiJ0ZXN0LXVzZXItMiIsImZ1bGxuYW1lIjoi67CV7Yag64G8IiwiZW1haWwiOiJ0b2traS5wYXJrQHRlc3QuY29tIiwidHMiOjE1OTY2MTE3OTIxMzEsIm5iZiI6MTU5Njc2NzQwMSwiZXhwIjoxNTk2NzcxMDAxLCJpYXQiOjE1OTY3Njc0MDF9.5Ol83EyKiRWc6FoSJPEqv4zJFlIowMDKmvVPsomQVWQ
// 出力結果は時間によって異なる場合があるため、<https://jwt.io>で検証してください。
}
}
}
Copy import io . jsonwebtoken . Jwts ;
import io . jsonwebtoken . SignatureAlgorithm ;
public class Main {
public static void main ( String args[]) {
String token = Jwts . builder ()
. setHeaderParam ( "typ" , "JWT" ) // 省略可能
. 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);
// 出力結果: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiJ0ZXN0LXVzZXItMiIsImZ1bGxuYW1lIjoi67CV7Yag64G8IiwiZW1haWwiOiJ0b2traS5wYXJrQHRlc3QuY29tIiwidHMiOjE1OTY2MTE3OTIxMzF9.akIXRvSad0wjv6y86ko7-KkJHwjMfTPDwXQOYOlixHQ
// 出力結果は時間によって異なる場合があるため、<https://jwt.io>で検証してください。
}
}