简体   繁体   English

在解析期间,JWT令牌始终被接收为过期

[英]JWT Token is always received as expired while parsing

I am using JWT Authentictaion in one of my application along with Spring Boot/Security and its my first take on JWT. 我在一个应用程序中使用JWT Authentictaion以及Spring Boot / Security及其对JWT的第一次学习。

Following are my set and get authentication methods: 以下是我的设置和获取身份验证方法:

static void addAuthentication(HttpServletResponse res, JWTPayload payload) {
    // all authentication related data like authorities and permissions can be 
    // embed to the token in a map using setClaims()
    Map<String, Object> claims = new HashMap<String, Object>();
    claims.put("roles", payload.getRoles());
    claims.put("permissions", payload.getPermissions());
    String JWT = Jwts.builder()
        .setSubject(payload.getUsername())
        .setClaims(claims)
        .setExpiration(new Date(System.currentTimeMillis() + EXPIRATIONTIME))
        .signWith(SignatureAlgorithm.HS512, SECRET_KEY)
        .compact();
    res.addHeader(HEADER_STRING, TOKEN_PREFIX + " " + JWT);
  }


  /**
   * this method retrives the token from the header and validates it.
   * this method is called from the JWTAuthentication filter which is
   * used against all the incoming calls except the login.
   * @param request
   * @return
   */
  static Authentication getAuthentication(HttpServletRequest request) {
    String token = request.getHeader(HEADER_STRING);
    if (token != null) {
      // parse the token.
      String user = Jwts.parser()
          .setSigningKey(SECRET_KEY)
          .parseClaimsJws(token.replace(TOKEN_PREFIX, ""))
          .getBody()
          .getSubject();

      return user != null ?
          new UsernamePasswordAuthenticationToken(user, null, emptyList()) :
          null;
    }
    return null;
  }

The JWT is generated and received in headers just fine. 生成JWT并在标头中接收它就好了。 However, if used in subsequent API call, I receive following error. 但是,如果在后续的API调用中使用,则会收到以下错误。

io.jsonwebtoken.ExpiredJwtException: JWT expired at 2018-10-31T16:06:05Z. Current time: 2018-10-31T16:06:08Z, a difference of 3421 milliseconds.  Allowed clock skew: 0 milliseconds.

The exception says allowed clock skew is 0 milliseconds. 例外情况是允许的时钟偏移为0毫秒。 In my above code EXPIRATIONTIME is set to 30000 (I believe this is set in seconds). 在我上面的代码中, EXPIRATIONTIME设置为30000(我相信这是以秒为单位设置的)。 I have tried increasing this value too but I still get the error. 我也尝试增加此值,但仍然收到错误。

Please suggest what am I doing wrong ? 请指出我做错了什么?

Not sure if you have already got an answer, but someone might benefit from this some time. 不知道您是否已经找到答案,但是有些人可能会从中受益。 I had the same problem initially and I thought it was an issue with JWT. 最初我有同样的问题,我认为这是JWT的问题。 But, when I debugged my code, got to know that I was doing a silly mistake and the expiration date was set in the past. 但是,当我调试代码时,才知道我犯了一个愚蠢的错误,并且到期日期已确定为过去。

So, for testing this I have created a sample program that you can execute standalone. 因此,为了进行测试,我创建了一个示例程序,您可以独立执行该程序。 Check this and modify your code accordingly. 对此进行检查并相应地修改您的代码。 Hope this helps. 希望这可以帮助。

import java.security.Key;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;

import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

public class TestJWTToken {
    private static final String API_KEY = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    public static void main(String... args) {
        String jwt = createJWT();
        System.out.println("JWT: " + jwt);
        parseJWT(jwt);
    }

    private static String createJWT() {
        Calendar cal = Calendar.getInstance(Locale.UK);
        Calendar cal1 = Calendar.getInstance(Locale.UK);
        cal1.setTime(cal.getTime());
        cal1.add(Calendar.SECOND, 300);

        byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(API_KEY);
        Key signingKey = new SecretKeySpec(apiKeySecretBytes, SignatureAlgorithm.HS256.getJcaName());

        Map<String, Object> map = new HashMap<>();
        map.put("alg", "HS256");
        map.put("typ", "JWT");

        String someId = UUID.randomUUID().toString();

        return Jwts.builder().setHeader(map).setIssuer("service_provider").setSubject("consumer_provider_connectivity_token")
                .claim("some_id", someId).setIssuedAt(cal.getTime()).setExpiration(cal1.getTime())
                .signWith(SignatureAlgorithm.HS256, signingKey).compact();
    }

    private static void parseJWT(String jwt) {
        Jws<Claims> jwsClaims = Jwts.parser().setSigningKey(DatatypeConverter.parseBase64Binary(API_KEY)).parseClaimsJws(jwt);
        System.out.println("JWT decoded: " + jwsClaims);

        Claims claims = jwsClaims.getBody();
        System.out.println("Subject: " + claims.getSubject());
        System.out.println("Issuer: " + claims.getIssuer());
        System.out.println("Issued at: " + claims.getIssuedAt());
        System.out.println("Expiration: " + claims.getExpiration());
        System.out.println("Some_Id: " + claims.get("some_id"));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM