您的位置:首页 > 科技 > IT业 > app软件开发培训班_百度霸屏全网推广_一站式营销平台_有没有专门做营销的公司

app软件开发培训班_百度霸屏全网推广_一站式营销平台_有没有专门做营销的公司

2025/1/5 14:52:11 来源:https://blog.csdn.net/m0_37996243/article/details/144894260  浏览:    关键词:app软件开发培训班_百度霸屏全网推广_一站式营销平台_有没有专门做营销的公司
app软件开发培训班_百度霸屏全网推广_一站式营销平台_有没有专门做营销的公司
        <dependency><groupId>org.bitbucket.b_c</groupId><artifactId>jose4j</artifactId><version>0.6.4</version></dependency>
package com.buptnu.util;public class UserToken {private String username;public UserToken() {}public UserToken(String username) {this.username = username;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}}
package com.buptnu.util;import java.security.Key;import org.jose4j.jws.AlgorithmIdentifiers;
import org.jose4j.jws.JsonWebSignature;
import org.jose4j.jwt.JwtClaims;
import org.jose4j.jwt.consumer.JwtConsumer;
import org.jose4j.jwt.consumer.JwtConsumerBuilder;
import org.jose4j.keys.HmacKey;public class TokenUtil {/*** 过期时间:1分钟*/private static final int JWT_EXPIRE = 1;/*** 密钥*/private static final String JWT_PRIVATE_KEY = "KRMEftu3JTRnlaFoRjnEcg==";/*** 生成token* @param userToken* @param expire* @return* @throws Exception*/public static String generateToken(UserToken userToken) throws Exception {JwtClaims claims = new JwtClaims();claims.setSubject(userToken.getUsername());claims.setExpirationTimeMinutesInTheFuture(JWT_EXPIRE);Key key = new HmacKey(JWT_PRIVATE_KEY.getBytes("UTF-8"));JsonWebSignature jws = new JsonWebSignature();jws.setPayload(claims.toJson());jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.HMAC_SHA256);jws.setKey(key);jws.setDoKeyValidation(false); // relaxes the key length requirement//签名String token = jws.getCompactSerialization();return token;}/*** 解析token* @param token* @return* @throws Exception*/public static UserToken getInfoFromToken(String token) throws Exception {if (token == null) {return null;}Key key = new HmacKey(JWT_PRIVATE_KEY.getBytes("UTF-8"));JwtConsumer jwtConsumer = new JwtConsumerBuilder().setRequireExpirationTime().setAllowedClockSkewInSeconds(30).setRequireSubject().setVerificationKey(key).setRelaxVerificationKeyValidation() // relaxes key length requirement.build();JwtClaims processedClaims = jwtConsumer.processToClaims(token);return new UserToken(processedClaims.getSubject());}public static void main(String[] args) throws Exception {UserToken userToken = new UserToken("宇文泰");String token = generateToken(userToken);System.out.println("token是:" + token);Thread.sleep(30000);// 30秒后读取userToken = getInfoFromToken(token);if (userToken != null) {System.out.println("获取到名称:" + userToken.getUsername() + "。");}Thread.sleep(35000);// 35秒后读取,因为设置了允许偏差时间 setAllowedClockSkewInSeconds,所以不会过期userToken = getInfoFromToken(token);if (userToken != null) {System.out.println("获取到名称:" + userToken.getUsername() + "。");}Thread.sleep(30000);// 30秒后读取try {getInfoFromToken(token);} catch (Exception e) {System.out.println("已过期,没有获取到名称!");e.printStackTrace();}}}
token是:eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiLlrofmlofms7AiLCJleHAiOjE3MzU4MjA1MjB9.gY_enml8dCPKk7fyYUW2vteRo4IzQtMXNP-kdALmA6U
获取到名称:宇文泰。
获取到名称:宇文泰。
已过期,没有获取到名称!
org.jose4j.jwt.consumer.InvalidJwtException: JWT (claims->{"sub":"宇文泰","exp":1735820520}) rejected due to invalid claims. Additional details: [[1] The JWT is no longer valid - the evaluation time NumericDate{1735820556 -> 2025-1-2 下午08时22分36秒} is on or after the Expiration Time (exp=NumericDate{1735820520 -> 2025-1-2 下午08时22分00秒}) claim value (even when providing 30 seconds of leeway to account for clock skew).]at org.jose4j.jwt.consumer.JwtConsumer.validate(JwtConsumer.java:466)at org.jose4j.jwt.consumer.JwtConsumer.processContext(JwtConsumer.java:311)at org.jose4j.jwt.consumer.JwtConsumer.process(JwtConsumer.java:433)at org.jose4j.jwt.consumer.JwtConsumer.processToClaims(JwtConsumer.java:171)at com.buptnu.util.TokenUtil.getInfoFromToken(TokenUtil.java:71)at com.buptnu.util.TokenUtil.main(TokenUtil.java:92)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com