【问题标题】:How to separate access token and refresh token endpoint in spring security oauth2如何在spring security oauth2中分离访问令牌和刷新令牌端点
【发布时间】:2020-04-19 16:33:47
【问题描述】:

在spring security oauth2中,获取访问令牌和刷新令牌使用同一个端点'/oauth/token',并通过参数grant_type'code'或'refresh_token'识别。

        if (isAuthCodeRequest(parameters)) {
            // The scope was requested or determined during the authorization step
            if (!tokenRequest.getScope().isEmpty()) {
                logger.debug("Clearing scope of incoming token request");
                tokenRequest.setScope(Collections.<String> emptySet());
            }
        }

        if (isRefreshTokenRequest(parameters)) {
            // A refresh token has its own default scopes, so we should ignore any added by the factory here.
            tokenRequest.setScope(OAuth2Utils.parseParameterList(parameters.get(OAuth2Utils.SCOPE)));
        }

但我想将此端点分成两部分,例如用于获取访问令牌的“oauth/access_token”和用于刷新访问令牌的“oauth/refresh_token”。我该怎么做 ?

我尝试编写自定义端点类,并注册 bean 以覆盖默认的 TokenEndpoint ,但似乎效果不佳。

【问题讨论】:

    标签: java spring oauth-2.0 spring-security-oauth2


    【解决方案1】:

    您可以为访问令牌和刷新令牌创建两个休息控制器方法,并使用休息模板在相关控制器方法内对 oauth/token 端点进行标准调用。

    @RestController
    public class TokenController {
    
        @RequestMapping("oauth/access_token")
        public TokenResponse getAccessToken() {
            //use rest template or httpclient to call to oauth/token and return converted TokenResponse
        }
    
        @RequestMapping("oauth/refresh_token")
        public TokenResponse getRefreshToken() {
            //use rest template or httpclient to call to oauth/token and return converted TokenResponse
        }
    }
    

    【讨论】:

    • 我试过这种方式,但是rest模板的响应是401,也许端点'oauth/token'受spring安全过滤器保护,我会尝试配置这个。谢谢。
    猜你喜欢
    • 2020-07-04
    • 2017-04-14
    • 2018-08-12
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    • 2016-05-31
    • 2021-07-08
    • 2018-12-01
    相关资源
    最近更新 更多