报表分享
parent
cce203363b
commit
2ffdf720e6
@ -0,0 +1,58 @@
|
||||
package com.anjiplus.template.gaea.business.util;
|
||||
|
||||
import com.anji.plus.gaea.exception.BusinessExceptionBuilder;
|
||||
import com.anjiplus.template.gaea.business.code.ResponseCode;
|
||||
import com.auth0.jwt.JWT;
|
||||
import com.auth0.jwt.JWTVerifier;
|
||||
import com.auth0.jwt.algorithms.Algorithm;
|
||||
import com.auth0.jwt.interfaces.Claim;
|
||||
import com.auth0.jwt.interfaces.DecodedJWT;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by raodeming on 2021/8/18.
|
||||
*/
|
||||
public class JwtUtil {
|
||||
|
||||
private static final String JWT_SECRET = "aj-report";
|
||||
|
||||
public static String createToken(String reportCode, String shareCode, Date expires) {
|
||||
String token = JWT.create()
|
||||
.withIssuedAt(new Date())
|
||||
.withExpiresAt(expires)
|
||||
.withClaim("reportCode", reportCode)
|
||||
.withClaim("shareCode", shareCode)
|
||||
.sign(Algorithm.HMAC256(JWT_SECRET));
|
||||
return token;
|
||||
}
|
||||
|
||||
|
||||
public static Map<String, Claim> getClaim(String token) {
|
||||
try {
|
||||
JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(JWT_SECRET)).build();
|
||||
DecodedJWT decodedJwt = jwtVerifier.verify(token);
|
||||
return decodedJwt.getClaims();
|
||||
} catch (Exception e) {
|
||||
throw BusinessExceptionBuilder.build(ResponseCode.REPORT_SHARE_LINK_INVALID, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static String getReportCode(String token) {
|
||||
Claim claim = getClaim(token).get("reportCode");
|
||||
if (null == claim) {
|
||||
throw BusinessExceptionBuilder.build(ResponseCode.REPORT_SHARE_LINK_INVALID);
|
||||
}
|
||||
return claim.asString();
|
||||
}
|
||||
|
||||
public static String getShareCode(String token) {
|
||||
Claim claim = getClaim(token).get("shareCode");
|
||||
if (null == claim) {
|
||||
throw BusinessExceptionBuilder.build(ResponseCode.REPORT_SHARE_LINK_INVALID);
|
||||
}
|
||||
return claim.asString();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package com.anjiplus.template.gaea.business.modules.reportshare.service.impl;
|
||||
|
||||
import com.auth0.jwt.JWT;
|
||||
import com.auth0.jwt.algorithms.Algorithm;
|
||||
import com.auth0.jwt.interfaces.DecodedJWT;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Created by raodeming on 2021/8/18.
|
||||
*/
|
||||
public class ReportShareServiceImplTest {
|
||||
|
||||
@Test
|
||||
public void jwtTest() throws InterruptedException {
|
||||
|
||||
long l = System.currentTimeMillis();
|
||||
|
||||
|
||||
String sign = JWT.create()
|
||||
.withIssuedAt(new Date())
|
||||
.withExpiresAt(new Date(l + 5000))
|
||||
.withClaim("reportCode", "report")
|
||||
.withClaim("shareCode", "1234567")
|
||||
.sign(Algorithm.HMAC256("111"));
|
||||
|
||||
|
||||
System.out.println(sign);
|
||||
|
||||
Thread.sleep(8000L);
|
||||
|
||||
DecodedJWT verify = JWT.require(Algorithm.HMAC256("111")).build().verify(sign);
|
||||
|
||||
Date expiresAt = verify.getExpiresAt();
|
||||
String reportCode = verify.getClaim("reportCode").asString();
|
||||
String shareCode = verify.getClaim("shareCode").asString();
|
||||
|
||||
|
||||
System.out.println(expiresAt);
|
||||
System.out.println(reportCode);
|
||||
System.out.println(shareCode);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue