简体   繁体   English

如何更改jwt访问令牌的其他信息

[英]How to change an additional information of jwt access token

I'm working with Spring Security and use JWT as access token , When a client sends the access token to server I must change an additional information (metadata) of this token and return a new one. 我正在使用Spring Security并将JWT用作访问令牌,当客户端将访问令牌发送到服务器时,我必须更改此令牌的其他信息(元数据)并返回一个新的信息。

How can I achieve that ? 我该如何实现?

i try with this code but not working 我尝试使用此代码,但无法正常工作

        String authorization = Context.getHeader("Authorization");

        if (authorization != null) {
            String tokenValue = authorization.replace("Bearer", "").trim();
            OAuth2AccessToken accessToken = tokenStore.readAccessToken(tokenValue);
            accessToken.getAdditionalInformation().put("activeProfileId", defaultProfileId);
            return accessToken.getValue();
        }

        return null;

You should get your metadata ("claims") from the token, then add them to a new JWT builder that will return a new token. 您应该从令牌中获取元数据(“声明”),然后将它们添加到新的JWT构建器中,该构建器将返回新的令牌。 The new JWT must be entered in HttpResponse to forward it to the client. 必须在HttpResponse中输入新的JWT才能将其转发到客户端。 Instead, the client will have to implement an interceptor to retrieve it in a comfortable and transparent way. 取而代之的是,客户端将必须实现一个拦截器以舒适且透明的方式检索它。

You've to get all Additional Information as HashMap and place them in OAuth2Authentication. 您必须以HashMap的形式获取所有其他信息,并将其放入OAuth2Authentication中。 stackoverflow.com/a/19057480/11951081 stackoverflow.com/a/19057480/11951081

In ajax should be: 在ajax中应该是:

https://api.jquery.com/category/ajax/global-ajax-event-handlers/ https://api.jquery.com/category/ajax/global-ajax-event-handlers/

$.ajaxSetup({
    beforeSend: function (xhr) {
        xhr.setRequestHeader('Authorization', <Jwt>)
    },
    success:function(event,jqXHR,ajaxOptions,data ){
        console.log(ajaxOptions.getResponseHeader('Authorization'))
    }
})

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

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