我正在尝试找到一种使用香草Spring实现和JwtTokenStore撤销Oauth2 JWT刷新令牌的方法。

首先:有人可以确认没有类似于/ oauth / token的API允许我撤销刷新令牌吗?

我想添加一个自定义API,该API会沿以下各行删除刷新令牌:

OAuth2RefreshToken oauth2RefreshToken=tokenStore.readRefreshToken(refreshToken);
tokenStore.removeRefreshToken(oauth2RefreshToken);


现在,在查看JwtTokenStore时,我注意到它使用了ApprovalStore。因此,我继续向JwtTokenStore提供了InMemoryApprovalStore。我的JwtTokenStore实例化如下所示:

@Bean
protected JwtAccessTokenConverter jwtTokenEnhancer() {
    JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
    converter.setSigningKey("123456");
    return converter;
}

@Bean
public JwtTokenStore getTokenStore(){
    tokenStore= new JwtTokenStore(jwtTokenEnhancer());
    tokenStore.setApprovalStore(new InMemoryApprovalStore());
    tokenStore.setTokenEnhancer(jwtTokenEnhancer());
    return tokenStore;
};


结果:没有InMemoryApprovalStore,我可以验证用户身份并刷新令牌而不会出现问题。但是,一旦我将InMemoryApprovalStore添加到令牌存储中,我就会开始收到以下错误消息:

{"error":"invalid_grant","error_description":"Invalid refresh token: eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0NDUwMjQ2MTcsInVzZXJfbmFtZSI6IjYzZjIyYjZlLWU5MGUtNDFjYS1iYzJlLTBmZTgzNmY3MTQ2NyIsImF1dGhvcml0aWVzIjpbIlJPTEVfQURNSU4iLCJST0xFX1VTRVIiXSwianRpIjoiMjgwMDgwNWQtMjk1Zi00ZDQzLWI2NTYtMDNlZWYwMWFkMjg0IiwiY2xpZW50X2lkIjoid2ViLWNsaWVudCIsInNjb3BlIjpbInJlYWQiLCJ3cml0ZSIsInRydXN0Il19.BPC0HqLYjWGM0IFjvsUGGKQ9dyIXSXwMhraCVFIxD0U"}


因此,我的第二个问题是撤销刷新令牌的正确方法是什么?

编辑:我发现following thread暗示ApprovalStore确实是撤销JWT令牌的方法。我现在只需要找出如何正确使用它们。

最佳答案

首先:有人可以确认没有类似于/ oauth / token的API允许我撤销刷新令牌吗?


Confirmed

您不需要定义JwtTokenStore bean,spring将使用AuthorizationServerEndpointsConfigurer为您创建它

private TokenStore tokenStore() {
    if (tokenStore == null) {
        if (accessTokenConverter() instanceof JwtAccessTokenConverter) {
            this.tokenStore = new JwtTokenStore((JwtAccessTokenConverter) accessTokenConverter());
        }
        else {
            this.tokenStore = new InMemoryTokenStore();
        }
    }
    return this.tokenStore;
}

private ApprovalStore approvalStore() {
    if (approvalStore == null && tokenStore() != null && !isApprovalStoreDisabled()) {
        TokenApprovalStore tokenApprovalStore = new TokenApprovalStore();
        tokenApprovalStore.setTokenStore(tokenStore());
        this.approvalStore = tokenApprovalStore;
    }
    return this.approvalStore;
}



因此,我的第二个问题是撤销刷新令牌的正确方法是什么?


撤销对令牌的批准,由JwtTokenStore使用

private void remove(String token) {
    if (approvalStore != null) {
        OAuth2Authentication auth = readAuthentication(token);
        String clientId = auth.getOAuth2Request().getClientId();
        Authentication user = auth.getUserAuthentication();
        if (user != null) {
            Collection<Approval> approvals = new ArrayList<Approval>();
            for (String scope : auth.getOAuth2Request().getScope()) {
                approvals.add(new Approval(user.getName(), clientId, scope, new Date(), ApprovalStatus.APPROVED));
            }
            approvalStore.revokeApprovals(approvals);
        }
    }
}

关于spring-security - 撤销JWT Oauth2刷新 token ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32617645/

10-13 22:49