shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.walker.web;
 
import com.walker.web.token.JwtTokenGenerator;
import com.walker.web.util.IdUtils;
import com.walker.web.util.IpUtils;
import com.walker.web.util.Location;
import org.junit.Test;
import org.springframework.web.client.RestTemplate;
 
public class TokenTest {
 
    private static final String TOKEN_SECRET = "zbc_mike_@2022";
 
//    @Test
    public void testUid(){
        System.out.println(IdUtils.fastSimpleUUID());
    }
 
//    @Test
    public void testLocationIp(){
        String ip = "221.15.155.4";
        RestTemplate restTemplate = new RestTemplate();
        Location location = IpUtils.getLocationAli(ip, restTemplate);
        if(location != null){
            System.out.println(location);
        } else {
            System.out.println("未查询到归属地");
        }
    }
 
//    @Test
    public void testCreateToken(){
        TokenGenerator tokenGenerator = new JwtTokenGenerator();
//        String token = tokenGenerator.createToken("userId:123456789", 3, TOKEN_SECRET);
        String token = tokenGenerator.createToken("test_key", "1", 30, TOKEN_SECRET);
        System.out.println("token = " + token);
    }
 
//    @Test
    public void testValidateTokenn(){
//        String token = "eyJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJ3YWxrZXJzb2Z0IiwiaWF0IjoxNjY1MzkxNDIyLCJzdWIiOiJ1c2VySWQ6MTIzNDU2Nzg5IiwiZXhwIjoxNjY1MzkxNjAyfQ.sLoS0K4Qz4KSAO0icTrQo3L4rt17TBjIWiQld5hscsWkY1oxdM7qJucxAlP9D1up2Mrbnq1zU5M2OIfhbX_2uA";
        String token = "eyJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJ3YWxrZXJzb2Z0IiwiaWF0IjoxNjY3MzA1NDQyLCJsb2dpbl91c2VyX2tleSI6InRlc3Rfa2V5IiwidXNlcl9pZF9rZXkiOiJ1c2VySWQ6MTIzNDU2Nzg5IiwiZXhwIjoxNjY3MzA1NjIyfQ.i365flYgaDXC9AucYebG_GqNua56ugVUatxlM5P0w7PsgQgHxoWF0S8P40qL-b8Phqnf-ZX0Caqx1xLwfUF_qA";
        TokenGenerator tokenGenerator = new JwtTokenGenerator();
        try{
            String data = tokenGenerator.validateToken(token, TOKEN_SECRET);
            System.out.println("data = " + data);
        } catch (TokenException ex){
            ex.printStackTrace();
            System.out.println(ex.getTitle());
        }
    }
}