|
@@ -1,6 +1,15 @@
|
|
|
package thyyxxk.webserver.utils;
|
|
|
|
|
|
+import cn.hutool.core.util.CharsetUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.crypto.SecureUtil;
|
|
|
+import cn.hutool.crypto.SmUtil;
|
|
|
+import cn.hutool.crypto.asymmetric.KeyType;
|
|
|
+import cn.hutool.crypto.asymmetric.SM2;
|
|
|
+import cn.hutool.crypto.asymmetric.Sign;
|
|
|
+import cn.hutool.crypto.asymmetric.SignAlgorithm;
|
|
|
+import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
|
|
|
+import cn.hutool.crypto.symmetric.SymmetricCrypto;
|
|
|
import cn.hutool.jwt.JWT;
|
|
|
import cn.hutool.jwt.JWTUtil;
|
|
|
import org.springframework.stereotype.Component;
|
|
@@ -8,6 +17,9 @@ import org.springframework.web.context.request.RequestContextHolder;
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.security.KeyPair;
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
import java.util.Objects;
|
|
@@ -19,7 +31,8 @@ import java.util.Objects;
|
|
|
public class TokenUtil {
|
|
|
private static final String PRIVATE_KEY = "w2XS014bk6Ma7tYh";
|
|
|
|
|
|
- private TokenUtil() {}
|
|
|
+ private TokenUtil() {
|
|
|
+ }
|
|
|
|
|
|
private static volatile TokenUtil INSTANCE = null;
|
|
|
|
|
@@ -34,15 +47,17 @@ public class TokenUtil {
|
|
|
return INSTANCE;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
public String createToken(String code) {
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
|
map.put("code", code);
|
|
|
map.put("expire_time", System.currentTimeMillis() + 60 * 60 * 1000 * 240);
|
|
|
- return JWTUtil.createToken(map, PRIVATE_KEY.getBytes());
|
|
|
+ map.put("companyLogo", "thyyxxkzy");
|
|
|
+ return JWTUtil.createToken(map, PRIVATE_KEY.getBytes(StandardCharsets.UTF_8));
|
|
|
}
|
|
|
|
|
|
- public void verifyToken(String token) {
|
|
|
- JWTUtil.verify(token, PRIVATE_KEY.getBytes());
|
|
|
+ public boolean verifyToken(String token) {
|
|
|
+ return JWTUtil.verify(token, PRIVATE_KEY.getBytes(StandardCharsets.UTF_8));
|
|
|
}
|
|
|
|
|
|
public String getTokenUserId() {
|
|
@@ -72,4 +87,18 @@ public class TokenUtil {
|
|
|
.getRequestAttributes();
|
|
|
return requestAttributes == null ? null : requestAttributes.getRequest();
|
|
|
}
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String text = "thyyxxkzy";
|
|
|
+ KeyPair pair = SecureUtil.generateKeyPair("SM2");
|
|
|
+ byte[] privateKey = pair.getPrivate().getEncoded();
|
|
|
+ byte[] publicKey = pair.getPublic().getEncoded();
|
|
|
+ SM2 sm2 = SmUtil.sm2(privateKey, publicKey);
|
|
|
+ // 公钥加密,私钥解密
|
|
|
+ String encryptStr = sm2.encryptBcd(text, KeyType.PublicKey);
|
|
|
+ String decryptStr = StrUtil.utf8Str(sm2.decryptFromBcd(encryptStr, KeyType.PrivateKey));
|
|
|
+ System.out.println(encryptStr);
|
|
|
+ System.out.println(decryptStr);
|
|
|
+ }
|
|
|
+
|
|
|
}
|