【问题标题】:Add headers when generating encoded JWT token生成编码的 JWT 令牌时添加标头
【发布时间】:2021-07-10 10:18:41
【问题描述】:

我有以下代码来生成 JWT 令牌。

public TestTokenGenerator() {
        try {
            if (signer == null) {
                KeyFactory keyFactory = KeyFactory.getInstance("RSA");
                byte[] keySpecBytes = Base64.decodeBase64(getKey());
                PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keySpecBytes);
                PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
                signer = new RsaSigner((RSAPrivateKey) privateKey, "SHA512withRSA");
            }
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    public static void main(String[] args) {
        TestTokenGenerator gen = new TestTokenGenerator();
        gen.customerJwt("MYUSER");  
    }

    public String customerJwt(String userCode) {
        String token = jwt(userCode, "wd.idp.ext.abc.com");
        return token;
    }

    private String jwt(String userCode, String iss) {

        Map<String, Object> claims = new HashMap<>();
        claims.put("user_name", userCode);
        claims.put("sub", userCode);
        claims.put("amr", "[aht]");

        String jwt = JwtHelper.encode(OBJECT_MAPPER.formatMap(claims), signer).getEncoded();

        log.info("User: {}, Token: {}, Claims: {}", userCode, jwt, claims);
        return jwt;
    }

    private String getKey() {
        return "WEQRwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCJ2sTFNuBVzhey"
    }

如何将标头(例如“kid”:“b0ec688af03dd7875116bf6d36c3584256ieirir”)添加到上述令牌。

上面的代码在令牌头中缺少kid,如下所示

【问题讨论】:

标签: java spring-security jwt


【解决方案1】:

设法添加了带有以下附加两行的令牌标头。

Map<String, String> headers = ImmutableMap.of("kid", 
                 "b0ec688af03dd7875116bf6d36c3584256ieirir");

String jwt = JwtHelper.encode(OBJECT_MAPPER.formatMap(claims), signer, 
        headers).getEncoded();

完整代码:

public TestTokenGenerator() {
        try {
            if (signer == null) {
                KeyFactory keyFactory = KeyFactory.getInstance("RSA");
                byte[] keySpecBytes = Base64.decodeBase64(getKey());
                PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keySpecBytes);
                PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
                signer = new RsaSigner((RSAPrivateKey) privateKey, "SHA512withRSA");
            }
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    public static void main(String[] args) {
        TestTokenGenerator gen = new TestTokenGenerator();
        gen.customerJwt("MYUSER");  
    }

    public String customerJwt(String userCode) {
        String token = jwt(userCode, "wd.idp.ext.abc.com");
        return token;
    }

    private String jwt(String userCode, String iss) {

        Map<String, Object> claims = new HashMap<>();
        Map<String, String> headers = ImmutableMap.of("kid", 
             "b0ec688af03dd7875116bf6d36c3584256ieirir");   >>>>>> New Line >>>>>>>
        claims.put("user_name", userCode);
        claims.put("sub", userCode);
        claims.put("amr", "[aht]");

        String jwt = JwtHelper.encode(OBJECT_MAPPER.formatMap(claims), signer, 
        headers).getEncoded();    >>>>>> New Line >>>>>>>>>>>>>>

        log.info("User: {}, Token: {}, Claims: {}", userCode, jwt, claims);
        return jwt;
    }

    private String getKey() {
        return "WEQRwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCJ2sTFNuBVzhey"
    }

【讨论】:

    猜你喜欢
    • 2018-08-29
    • 2021-07-29
    • 2021-07-22
    • 1970-01-01
    • 2018-07-31
    • 2019-10-22
    • 1970-01-01
    • 2020-03-05
    • 2021-12-21
    相关资源
    最近更新 更多