0

与此 github 问题相关:

https://github.com/auth0/auth0-java/issues/228

我需要了解如何使用 JWT Token 管理复杂的声明对象

我正在尝试为 Jitsimeet 生成一个 JWt 令牌,但我对如何使用 java API 有一些疑问,这里我需要在 jwt 上编码的有效负载:

标头(使用 RS256 公钥验证)

{
  "kid": "jitsi/custom_key_name",
  "typ": "JWT",
  "alg": "RS256"
}

有效载荷

{
  "context": {
    "user": {
      "avatar": "https:/gravatar.com/avatar/abc123",
      "name": "John Doe",
      "email": "jdoe@example.com",
      "id": "abcd:a1b2c3-d4e5f6-0abc1-23de-abcdef01fedcba"
    },
    "group": "a123-123-456-789"
  },
  "aud": "jitsi",
  "iss": "my_client",
  "sub": "meet.jit.si",
  "room": "*",
  "exp": 1500006923
}

这里是我的java代码

    public static String generateFrom(String username,String email,String room,String appid,String hostname) {
        try {
            Date expirationDate = Date.from(ZonedDateTime.now().plusHours(24).toInstant());
            Date issuedAt = Date.from(ZonedDateTime.now().toInstant());

            User user = new User();
            user.setAvatar("");
            user.setEmail(email);
            user.setName(username);
            user.setId(username);

            Context  context = new Context();
            context.setUser(user);


            JwtToken jwtToken = new JwtToken();
            jwtToken.setContext(context);
            jwtToken.setAud(appid);
            jwtToken.setIss(appid);
            jwtToken.setSub(hostname);
            jwtToken.setRoom(room);
            jwtToken.setExp(String.valueOf(expirationDate.getTime()));

            //Claim claimUser = new com.auth0.jwt.JWTCreator.Builder().

            //HEADER
            Map<String, Object> headerClaims = new HashMap<String,Object>();
            headerClaims.put(PublicClaims.KEY_ID, "jitsi/"+appid);//"kid"
            headerClaims.put(PublicClaims.ALGORITHM,"HS256");//"alg
            headerClaims.put(PublicClaims.TYPE,"JWT");//"typ"

            //PAYLOAD
            Map<String, Object> claims = new HashMap<String, Object>();
            claims.put("context",jwtToken.getContext());
            claims.put("aud",jwtToken.getAud());
            claims.put("iss",jwtToken.getIss());
            claims.put("sub",jwtToken.getSub());
            claims.put("room",jwtToken.getRoom());
            claims.put("exp",jwtToken.getExp());    
            //https://github.com/auth0/java-jwt
            return JWT.create()
                    .withHeader(headerClaims)
                    .withIssuer(jwtToken.getIss())
                    .withSubject(jwtToken.getSub())
                    .withAudience(jwtToken.getAud())
                    .withIssuedAt(issuedAt)
                    .withExpiresAt(expirationDate)
                    //CLAIM                 
                    .withClaim("aud",jwtToken.getAud())
                    .withClaim("iss",jwtToken.getIss())
                    .withClaim("sub",jwtToken.getSub())
                    .withClaim("room",jwtToken.getRoom())
                    .withClaim("exp",jwtToken.getExp())
                    .withClaim("context",jwtToken.getContext().toString())                                  
                    .sign(getAlgorithm());
        } catch (UnsupportedEncodingException | JWTCreationException ex) {
            throw new JWTCreationException(ex.getMessage(),ex);
        }
    }

用于设置声明“上下文”的 java 代码是错误的。我必须如何使用java api来完成这个?

问候。

4

0 回答 0