External Purchase Report 401 error

We are producing a function to submit an Apple external purchase report. When I sent the report, I created a jwt token and put it in the header There are times when you operate normally and suddenly get a 401 error. When I checked the entity to log before sending the report, I found that the header was well contained and the token changed every time I called.

Once you get 401 error, you have to shut down your server(tomcat) once and run it again May I know what kind of problem is causing this phenomenon?

Or can I find a way to fix the problem? The server is using aws ec2 load balancer The back language is java spring boot


jwt token create code

private String keyId="******";
private String issuerId="******";
private String bundleId = "ai.******";

Instant now = Instant.now();
Date issuedAt = Date.from(now);
Date expiresAt = Date.from(now.plusSeconds(20 * 60));
public String createToken(){
    try {
        PrivateKey key = getPrivateKey();

        return Jwts.builder()
                .setHeaderParam("alg", "ES256")
                .setHeaderParam("kid", keyId)
                .setHeaderParam("typ", "JWT")
                .setIssuer(issuerId)
                .setIssuedAt(issuedAt)
                .setExpiration(expiresAt)
                .setAudience("appstoreconnect-v1")
                .signWith(key, SignatureAlgorithm.ES256)
                .claim("bid",bundleId)
                .compact();
    }catch (Exception e){
        e.printStackTrace();
        throw new RuntimeException("JWT error", e);
    }
}

private static PrivateKey getPrivateKey() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    InputStream privateKey = new ClassPathResource("certs/SubscriptionKey_***********.p8").getInputStream();

    String result = new BufferedReader(new InputStreamReader(privateKey)) .lines().collect(Collectors.joining("\n"));

    String key = result.replace("-----BEGIN PRIVATE KEY-----\n", "")
            .replace("-----END PRIVATE KEY-----", "")
            .replace("\n", ""); 
    byte[] decoded = Base64.getDecoder().decode(key); 
    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decoded);
    KeyFactory keyFactory = KeyFactory.getInstance("EC");
    return keyFactory.generatePrivate(keySpec);
}
External Purchase Report 401 error
 
 
Q