JWTApiTest

 1 package com.example.jwtdemo;
 2 
 3 import com.auth0.jwt.JWT;
 4 import com.auth0.jwt.JWTVerifier;
 5 import com.auth0.jwt.algorithms.Algorithm;
 6 import com.auth0.jwt.interfaces.DecodedJWT;
 7 import com.fasterxml.jackson.core.JsonProcessingException;
 8 import com.fasterxml.jackson.databind.ObjectMapper;
 9 import org.junit.Test;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12 import org.springframework.boot.test.context.SpringBootTest;
13 
14 @SpringBootTest
15 public class JWTApiTest {
16 
17     private ObjectMapper objectMapper = new ObjectMapper();
18 
19     private Logger log = LoggerFactory.getLogger(JWTApiTest.class);
20 
21     @Test
22     public void sayHello() {
23         log.info("Hello JWT!");
24     }
25 
26     @Test
27     public void createToken() {
28         Algorithm algorithm = Algorithm.HMAC256("thisasjldkfn");
29 
30         String token = JWT.create().withIssuer("auth0").sign(algorithm);
31         log.info(token);
32     }
33 
34     @Test
35     public void verifyToken() throws JsonProcessingException {
36         String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhdXRoMCJ9.GWxauGiVkFotvKwgf_J8-XIvmAJ3VDhikVFYag8vSxQ";
37         Algorithm algorithm = Algorithm.HMAC256("thisasjldkfn");
38         JWTVerifier verifier = JWT.require(algorithm).withIssuer("auth0").build();
39         DecodedJWT jwt = verifier.verify(token);
40 
41         log.info(objectMapper.writeValueAsString(algorithm));
42     }
43 
44     @Test
45     public void decodeToken() throws JsonProcessingException {
46         String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhdXRoMCJ9.GWxauGiVkFotvKwgf_J8-XIvmAJ3VDhikVFYag8vSxQ";
47         DecodedJWT decodedJWT = JWT.decode(token);
48         log.info(objectMapper.writeValueAsString(decodedJWT));
49     }
50 }

 

posted @ 2018-12-12 00:15  AMDYES  阅读(100)  评论(0)    收藏  举报