1
13693261870
2022-09-16 fee60c3e25fac0982f3b8cb8feea7225c4ed22f8
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package com.terra.proxy.util;
 
import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.terra.proxy.bean.CustomerToken;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
 
import java.util.Date;
import java.util.Map;
 
public class TokenUtils {
    // token密钥
    private static String secret = "f4e2e52034348f86b67cde581c0f9eb5[lanbase]";
 
    public static CustomerToken decToken(String token) throws Exception {
        // 校验token
        if (StringUtils.isBlank(token)) {
            throw new Exception("未传入token");
        }
 
        Claims claims;
        try {
            claims = getClaimByToken(token);
        } catch (ExpiredJwtException e) {
            throw new Exception("token已过期");
        } catch (Exception e) {
            // e.printStackTrace();
            throw new Exception("token无效");
        }
        if (claims == null) {
 
            throw new Exception("无效的token");
        }
        if (isTokenExpired(claims.getExpiration())) {
            throw new Exception("token已失效");
        }
        String msg = claims.getSubject();
 
        CustomerToken cutToken = CustomerToken.fromString(msg);
        return cutToken;
    }
 
    public static Result validate(CustomerToken cutToken, String clientIp,
                                  String refererUrl) {
        Result r;
        if (cutToken.getIsPubzy()) {
            r = Result.ok(cutToken.getSubzyids());
        } else {
            String appId = cutToken.getAppId();
            // 无appId属通过浏览器直接访问的情况:校验 clientIp 与 Token里面的IP地址是否匹配,
            if (StringUtils.isBlank(appId) || StringUtils.equals("0", appId)) {
                // TODO
            } else {
                if (!clientIp.equalsIgnoreCase((String) cutToken.getClientIp())) {
                    if (StringUtils.isBlank(refererUrl)
                            || !refererUrl
                            .startsWith((String) cutToken.getAppUrl())) {
                        return Result.error(HttpStatus.SC_UNAUTHORIZED, "未授权的应用系统");
                    }
                }
            }
        }
 
        r = Result.ok(cutToken.getSubzyids());
        return r;
    }
 
    /**
     * 验证token
     *
     * @param token
     * @param requestUrl
     * @param clientIp
     * @param refererUrl
     * @return
     */
    public static Result tokenValidate(String token, String requestUrl,
                                       String clientIp, String refererUrl) {
        if (StringUtils.isBlank(token)) {
            return Result.error(HttpStatus.SC_UNAUTHORIZED, "未传入token");
        }
        Claims claims;
        try {
            claims = getClaimByToken(token);
        } catch (ExpiredJwtException e) {
            return Result.error(HttpStatus.SC_UNAUTHORIZED, "token已过期");
        } catch (Exception e) {
            return Result.error(HttpStatus.SC_UNAUTHORIZED, "token无效");
        }
        if (claims == null) {
            return Result.error(HttpStatus.SC_UNAUTHORIZED, "无效的token");
        }
        if (isTokenExpired(claims.getExpiration())) {
            return Result.error(HttpStatus.SC_UNAUTHORIZED, "token已失效");
        }
        String msg = claims.getSubject();
        Map<String, Object> map = JSON.parseObject(msg);
        if (StringUtils.isNotBlank(clientIp)) {
            if (!clientIp.equalsIgnoreCase((String) map.get("clientIp"))) {
                return Result.error(HttpStatus.SC_UNAUTHORIZED, "未授权的客户端");
            }
            if (StringUtils.isBlank(requestUrl)
                    || !requestUrl.startsWith((String) map.get("resourceUrl"))) {
                return Result.error(HttpStatus.SC_UNAUTHORIZED, "无权限访问请求的资源");
            }
        } else {
            if (StringUtils.isBlank(refererUrl)
                    || !refererUrl.startsWith((String) map.get("appUrl"))) {
                return Result.error(HttpStatus.SC_UNAUTHORIZED, "未授权的应用系统");
            }
        }
 
        Result r = Result.ok(msg);
        return r;
    }
 
    /**
     * token是否过期
     *
     * @param expiration
     * @return
     */
    public static boolean isTokenExpired(Date expiration) {
        return expiration.before(new Date());
    }
 
    /**
     * 解析token
     *
     * @param token
     * @return
     */
    public static Claims getClaimByToken(String token) {
        try {
            return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
        } catch (Exception e) {
            return null;
        }
    }
 
    /**
     * 解析token
     *
     * @param token
     * @return
     */
    public static CustomerToken getTokenInfo(String token) {
        try {
            String[] array = token.split("\\.");
            String t = Base64.decodeStr(array[1]);
            JSONObject jobj = JSONObject.parseObject(t, JSONObject.class);
            CustomerToken cutToken = CustomerToken.fromString(jobj.getString("sub"));
            return cutToken;
 
        } catch (Exception e) {
            return null;
        }
    }
 
    public static void main(String[] args) {
        CustomerToken tokenInfo1 = getTokenInfo("eyJ0eXBlIjoiSldUIiwiYWxnIjoiSFM1MTIifQ.eyJzdWIiOiJzZXJ2ZGwiLCJpYXQiOjE2MTU1MzE4NTksImV4cCI6MTY0NjYzNTg1OX0.eSGL-W5p76mjs9rvecxvXqE-BSy1MFcUyIcAFjwTw4ZMV70COlNwf-p-I3HmeVAG6IFZuwwKGdDv6H7NtMTBWw");
        System.out.println(tokenInfo1);
 
    }
}