mirror of
https://gitee.com/huoyo/ko-time.git
synced 2025-12-06 16:58:26 +08:00
remove jwt
This commit is contained in:
parent
48823969f1
commit
69f72ce63b
8
pom.xml
8
pom.xml
@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>cn.langpy</groupId>
|
||||
<artifactId>ko-time</artifactId>
|
||||
<version>2.2.2</version>
|
||||
<version>2.2.3</version>
|
||||
<name>KoTime</name>
|
||||
<description>A springboot tool for tracking the paths of the methods,which can help you to find methods's performances easily.</description>
|
||||
<licenses>
|
||||
@ -23,7 +23,6 @@
|
||||
<developerConnection>scm:git:https://gitee.com/huoyo/ko-time.git</developerConnection>
|
||||
</scm>
|
||||
<properties>
|
||||
<jwt.version>4.0.0-beta.0</jwt.version>
|
||||
<aspecttj.version>1.9.5</aspecttj.version>
|
||||
<tomcat.version>9.0.36</tomcat.version>
|
||||
<spring-context.version>5.2.7.RELEASE</spring-context.version>
|
||||
@ -33,11 +32,6 @@
|
||||
<spring-redis.version>2.6.4</spring-redis.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.auth0</groupId>
|
||||
<artifactId>java-jwt</artifactId>
|
||||
<version>${jwt.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
|
||||
@ -1,49 +1,85 @@
|
||||
package cn.langpy.kotime.util;
|
||||
|
||||
import com.auth0.jwt.JWT;
|
||||
import com.auth0.jwt.JWTVerifier;
|
||||
import com.auth0.jwt.algorithms.Algorithm;
|
||||
import com.auth0.jwt.exceptions.JWTVerificationException;
|
||||
import com.auth0.jwt.interfaces.DecodedJWT;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
import java.util.*;
|
||||
|
||||
public class KoUtil {
|
||||
private final static String koTimeSecret = UUID.randomUUID().toString().replace("-", "");
|
||||
|
||||
private final static List<Integer> choices = randomSecretIndexs();
|
||||
|
||||
public static String login(String userName) {
|
||||
Algorithm algorithm = Algorithm.HMAC256(koTimeSecret);
|
||||
String token = JWT.create()
|
||||
.withIssuer("kotime")
|
||||
.withSubject(userName)
|
||||
.withExpiresAt(new Date(System.currentTimeMillis() + (12*60*60*1000)))
|
||||
.withClaim("author", "KoTime")
|
||||
.sign(algorithm);
|
||||
String token = encode(userName + "KOTO" + (System.currentTimeMillis() + (12 * 60 * 60 * 1000)));
|
||||
return token;
|
||||
}
|
||||
|
||||
|
||||
public static boolean checkLogin(String token) {
|
||||
try {
|
||||
Algorithm algorithm = Algorithm.HMAC256(koTimeSecret);
|
||||
JWTVerifier verifier = JWT.require(algorithm).build();
|
||||
DecodedJWT jwt = verifier.verify(token);
|
||||
return true;
|
||||
}catch (JWTVerificationException verificationException){
|
||||
throw new KoTimeNotLoginException("can not find login information for kotime,please login first!");
|
||||
}
|
||||
public static void checkLogin(String token) {
|
||||
decode(token);
|
||||
}
|
||||
|
||||
public static boolean isLogin(String token) {
|
||||
try {
|
||||
Algorithm algorithm = Algorithm.HMAC256(koTimeSecret);
|
||||
JWTVerifier verifier = JWT.require(algorithm).build();
|
||||
DecodedJWT jwt = verifier.verify(token);
|
||||
decode(token);
|
||||
return true;
|
||||
}catch (JWTVerificationException verificationException){
|
||||
} catch (Exception verificationException) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static List<Integer> randomSecretIndexs() {
|
||||
List<Integer> choices = new ArrayList<>();
|
||||
Random random = new Random();
|
||||
for (int i = 0; i < 20; i++) {
|
||||
int intrandom = random.nextInt(20);
|
||||
if (choices.contains(intrandom)) {
|
||||
continue;
|
||||
}
|
||||
choices.add(intrandom);
|
||||
}
|
||||
return choices;
|
||||
}
|
||||
|
||||
|
||||
private static String encode(String text) {
|
||||
Base64.Encoder encoder = Base64.getEncoder();
|
||||
String encode = encoder.encodeToString(text.getBytes());
|
||||
int choicesSize = choices.size();
|
||||
for (int i = 0; i < choicesSize; i++) {
|
||||
Integer choice = choices.get(i);
|
||||
String pre = encode.substring(0, choice);
|
||||
String suf = encode.substring(choice);
|
||||
encode = pre + koTimeSecret.substring(i, i + 1) + suf;
|
||||
}
|
||||
return encode;
|
||||
}
|
||||
|
||||
private static String decode(String token) {
|
||||
int tokenLength = token.length();
|
||||
int choicesSize = choices.size();
|
||||
for (int i = choicesSize - 1; i >= 0; i--) {
|
||||
Integer choice = choices.get(i);
|
||||
String pre = token.substring(0, choice);
|
||||
String suf = token.substring(choice + 1);
|
||||
String secretAt = koTimeSecret.substring(i, i + 1);
|
||||
if ((choice + 1) > tokenLength) {
|
||||
throw new InvalidAuthInfoException("error token!");
|
||||
}
|
||||
String tokenAt = token.substring(choice, choice + 1);
|
||||
if (!secretAt.equals(tokenAt)) {
|
||||
throw new InvalidAuthInfoException("error token!");
|
||||
}
|
||||
token = pre + suf;
|
||||
}
|
||||
Base64.Decoder decoder = Base64.getDecoder();
|
||||
byte[] decode = decoder.decode(token);
|
||||
String decodeStr = new String(decode);
|
||||
String[] split = decodeStr.split("KOTO");
|
||||
Long expireTime = Long.valueOf(split[1]);
|
||||
if (expireTime < System.currentTimeMillis()) {
|
||||
throw new InvalidAuthInfoException("expired time!");
|
||||
}
|
||||
return decodeStr;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user