JWTApiTest

package com.example.jwtdemo;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class JWTApiTest {

private ObjectMapper objectMapper = new ObjectMapper();

private Logger log = LoggerFactory.getLogger(JWTApiTest.class);

@Test
public void sayHello() {
log.info("Hello JWT!");
}

@Test
public void createToken() {
Algorithm algorithm = Algorithm.HMAC256("thisasjldkfn");

String token = JWT.create().withIssuer("auth0").sign(algorithm);
log.info(token);
}

@Test
public void verifyToken() throws JsonProcessingException {
String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhdXRoMCJ9.GWxauGiVkFotvKwgf_J8-XIvmAJ3VDhikVFYag8vSxQ";
Algorithm algorithm = Algorithm.HMAC256("thisasjldkfn");
JWTVerifier verifier = JWT.require(algorithm).withIssuer("auth0").build();
DecodedJWT jwt = verifier.verify(token);

log.info(objectMapper.writeValueAsString(algorithm));
}

@Test
public void decodeToken() throws JsonProcessingException {
String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhdXRoMCJ9.GWxauGiVkFotvKwgf_J8-XIvmAJ3VDhikVFYag8vSxQ";
DecodedJWT decodedJWT = JWT.decode(token);
log.info(objectMapper.writeValueAsString(decodedJWT));
}
}

转载于:https://www.cnblogs.com/amdyes/p/10106176.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值