简体   繁体   中英

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.

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. The new JWT must be entered in HttpResponse to forward it to the client. 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. stackoverflow.com/a/19057480/11951081

In ajax should be:

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'))
    }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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