Commit 13907747 authored by tangtuo's avatar tangtuo

修改jwt登录过期的报错信息

parent f0f2c7f3
......@@ -7,6 +7,7 @@ import com.fzm.common.entity.AbstractUser;
import com.fzm.common.enums.ResultCode;
import com.fzm.common.exception.GlobalException;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.extern.slf4j.Slf4j;
......@@ -53,8 +54,9 @@ public class JwtUtil {
.setSigningKey(SECRET)
.parseClaimsJws(token)
.getBody();
} catch (ExpiredJwtException e) {
throw GlobalException.newException(ResultCode.UNAUTHORIZED, "登录已过期");
} catch (Exception e) {
log.info("JWT格式验证失败:{}", token);
throw GlobalException.newException(ResultCode.UNAUTHORIZED, e.getMessage());
}
return claims;
......@@ -197,4 +199,20 @@ public class JwtUtil {
return false;
}
public static void main(String[] args) {
HashMap<String, Object> claims = new HashMap<>();
claims.put(CLAIM_KEY_CREATED, new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 48));
claims.put(CLAIM_KEY_APP_ID, "portal");
claims.put(CLAIM_KEY_USERID, 1);
String token = Jwts.builder()
.setClaims(claims)
.setExpiration(new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24))
.signWith(SignatureAlgorithm.HS512, SECRET)
.compact();
System.out.println(token);
boolean expired = isTokenExpired(token);
System.out.println(expired);
/*Integer userId = getUserIdFromToken("eyJhbGciOiJIUzUxMiJ9.eyJjcmVhdGVkIjoxNjI3ODAxMzIzMDE5LCJhcHBJZCI6InBvcnRhbCIsImV4cCI6MTYyNzg4NzcyMywidXNlcklkIjoxfQ.xch9RdpHupq64l3I2RvU09E2giCYfn2lfHmv1OYQhVpEVqe9NcjZ2mxcm4_2XapC1AHNGP6TRQI3BB3G4ZB-FA");
System.out.println(userId);*/
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment