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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
| package com.xtuer.util;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONException; import com.alibaba.fastjson.TypeReference; import lombok.Getter; import lombok.Setter; import org.apache.commons.lang3.StringUtils; import org.springframework.util.Assert;
import java.util.Collections; import java.util.Map; import java.util.TreeMap;
public class Jwt {
public static boolean checkToken(String jwtToken, String appKey) { if (StringUtils.isBlank(jwtToken)) { return false; }
int dotIndex = jwtToken.indexOf("."); if (dotIndex == -1) { return false; }
try { Map<String, String> params = Jwt.params(jwtToken);
try { Long expiredAt = Long.parseLong(params.get("expiredAt")); if (expiredAt < System.currentTimeMillis()) { return false; } } catch (NumberFormatException ex) {}
String signature = jwtToken.substring(dotIndex+1); return signature.equals(Jwt.sign(params, appKey)); } catch (JSONException ex) { return false; } }
public static Map<String, String> params(String jwtToken) { int dotIndex = jwtToken.indexOf("."); if (dotIndex == -1) { return Collections.emptyMap(); }
String payload = Utils.unbase64UrlSafe(jwtToken.substring(0, dotIndex));
try { return JSON.parseObject(payload, new TypeReference<TreeMap<String, String>>() {}); } catch (NumberFormatException ex) { return Collections.emptyMap(); } }
private static String sign(Map<String, String> params, String appKey) { Map<String, String> sortedMap = new TreeMap<>(params); StringBuilder toSignedText = new StringBuilder(appKey);
for (Map.Entry<String, String> entry : sortedMap.entrySet()) { toSignedText.append(entry.getValue()); }
return Utils.md5(toSignedText.toString()); }
public static Builder create(String appId, String appKey) { return new Builder(appId, appKey); }
@Getter @Setter public static class Builder { private Long expiredAt; private String appId; private String appKey; private TreeMap<String, String> params = new TreeMap<>();
public Builder(String appId, String appKey) { Assert.notNull(appId, "JWT appId cannot be null"); Assert.notNull(appKey, "JWT appKey cannot be null"); this.appId = appId; this.appKey = appKey; }
public Builder expiredAt(long expiredAt) { this.expiredAt = expiredAt; return this; }
public Builder param(String name, String value) { Assert.notNull(name, "JWT param name cannot be null"); Assert.notNull(value, "JWT param value cannot be null"); params.put(name, value); return this; }
public String token() { params.put("appId", appId); params.put("signedAt", System.currentTimeMillis()+""); if (expiredAt != null) { params.put("expiredAt", expiredAt+""); }
String payload = Utils.base64UrlSafe(JSON.toJSONString(params)); String signature = Jwt.sign(params, appKey); return payload + "." + signature; } }
public static void main(String[] args) throws InterruptedException { String appId = "school-1"; String appKey = "Passw0rd"; String token;
token = Jwt.create(appId, appKey).param("username", "放下").token(); System.out.println(token); System.out.println(Jwt.checkToken(token, appKey)); System.out.println(StringUtils.repeat("-", 120));
token = Jwt.create(appId, appKey).param("username", "放下").expiredAt(System.currentTimeMillis() + 2000).token(); System.out.println(token); System.out.println(Jwt.checkToken(token, appKey)); Thread.sleep(2500); System.out.println(Jwt.checkToken(token, appKey));
System.out.println(Jwt.params(token)); System.out.println(StringUtils.repeat("-", 80));
System.out.println(Jwt.checkToken("", appKey)); System.out.println(Jwt.checkToken("xxx", appKey)); System.out.println(Jwt.checkToken("xxx.yyy", appKey)); } }
|