负载JWT结构



我试图设置我的JWT令牌,但它有一个不同的有效载荷结构像往常一样,我如何设置这种有效载荷结构?

{
"https://daml.com/ledger-api": {
"ledgerId": "sandbox",
"applicationId": "foobar",
"actAs": ["Alice"]    
}
}

我只能这样设置:

{           
"sub": "https://daml.com/ledger-api",
"ledgerId": "sandbox",
"applicationId": "foobar",
"actAs": "[Alice]"
}

java代码:

void getJWT() throws UnsupportedEncodingException {

Claims claims = Jwts.claims().setSubject("https://daml.com/ledger-api");
claims.put("ledgerId","sandbox");
claims.put("applicationId", "foobar");
claims.put("actAs", "[Alice]");
String jwtToken = Jwts.builder()
.setClaims(claims)
.signWith(SignatureAlgorithm.HS256, "sYn2kjibddFAWtnPJ2AFlL8WXmohJMCvigQggaEypa5E=".getBytes("UTF-8"))
.setHeaderParam("typ", Header.JWT_TYPE)
.compact();
System.out.println("The generated jwt token is as follows:" + jwtToken);
}

如何设置这种有效负载结构?

{
"https://daml.com/ledger-api": {
"ledgerId": "sandbox",
"applicationId": "foobar",
"actAs": ["Alice"]    
}
}

您可以通过如下所述修改代码轻松实现上述有效负载结构。

void getJWT() throws UnsupportedEncodingException {
Map<String, String> claims = new HashMap<String, String>();
claims.put("ledgerId", "sandbox");
claims.put("applicationId", "foobar");
claims.put("actAs", "[Alice]");
Map<String, Object> claimsMap = new HashMap<String, Object>();
claimsMap.put("https://daml.com/ledger-api", claims);
String jwtToken = Jwts.builder()
.setClaims(claimsMap)
.signWith(SignatureAlgorithm.HS256, "sYn2kjibddFAWtnPJ2AFlL8WXmohJMCvigQggaEypa5E=".getBytes("UTF-8"))
.setHeaderParam("typ", Header.JWT_TYPE)
.compact();
System.out.println("The generated jwt token is as follows:" + jwtToken);
}

最新更新