1.逐步检查工具类和坐标是否存在正确
(1)检查坐标
![](https://img-blog.csdnimg.cn/direct/ac76a49dd9814ca0b027ddfa83ef6961.png)
(2)检查工具类
![](https://img-blog.csdnimg.cn/direct/1102dc809dab42a19cd8cddcca85149d.png)
(3)导入domain依赖
![](https://img-blog.csdnimg.cn/direct/7967e52bc1cd48b683756f9546bd69a2.png)
2.测试
package com.czxy;
import com.czxy.domain.User;
import com.czxy.utils.JwtUtils;
import com.czxy.utils.RsaUtils;
import org.junit.Test;
/**
* @author 桐叔
* @email liangtong@itcast.cn
* @description
*/
public class TestJwt {
private static final String pubKeyPath = "D:\\rsa\\rsa.pub";
private static final String priKeyPath = "D:\\rsa\\rsa.pri";
@Test
public void testCreateToken() throws Exception {
//需要生成令牌的数据
User loginUser = new User();
loginUser.setUserName("jack");
loginUser.setPassword("6666");
loginUser.setGender(false);
//生成token:数据,过期时间,私钥
String token = JwtUtils.generateToken(loginUser, 1, RsaUtils.getPrivateKey(priKeyPath));
System.out.println(token);
}
@Test
public void testParseToken() throws Exception {
String token = "eyJhbGciOiJSUzI1NiJ9.eyJjbGFzcyI6ImNvbS5jenh5LmRvbWFpbi5Vc2VyIiwiZ2VuZGVyIjpmYWxzZSwicGFzc3dvcmQiOiI2NjY2IiwidXNlck5hbWUiOiJqYWNrIiwiZXhwIjoxNzAyNTQxNjcxfQ.YSZq8gE3Urx3CPXqT07FyzPgy8IEQWWVYWoNDRR42ed27IBaDWrIYZ4paJRefHL1eEy_7CPvlRhEJBzfBrihZ9hme2rEhZp8PSro8UuYTb6_73y3JWDlLvWE0wyosrh-69Y37jADB8xXN4s9i8BU7EQVDjNHhfKUu5b6vXfZQSg";
User loginUser = JwtUtils.getObjectFromToken(token, RsaUtils.getPublicKey(pubKeyPath), User.class);
System.out.println(loginUser);
}
}