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