【问题标题】:How can we access oauth2 access token from springboot server using angular2我们如何使用angular2从springboot服务器访问oauth2访问令牌
【发布时间】:2018-05-10 22:31:09
【问题描述】:

我已将授权服务器配置为 公共无效配置(ClientDetailsS​​erviceConfigurer 客户端)抛出异常 {

    clients.inMemory()
            .withClient("clientID")
            .authorizedGrantTypes("client-credentials", "password","refresh_token")
            .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
            .scopes("read", "write", "trust")
            .resourceIds("oauth2-resource")
            .accessTokenValiditySeconds(120)
            .refreshTokenValiditySeconds(300)
            .secret("secret");

}

那么,基于这个配置,我如何从 angular2 应用中获取 access_token?

【问题讨论】:

    标签: spring-boot spring-security-oauth2 angular2-security


    【解决方案1】:

    使用client_credentials 模式从端点/oauth/token 获取access_token(如果您使用默认值)。

    HTTP POST 参数:

    • grant_type: client_credentials
    • client_id:clientID(来自您的配置)
    • client_secret:secret(来自您的配置)

    响应结果如下:

    {
        "access_token": "token...",
        "token_type": "bearer",
        "expires_in": 43199,
        "scope": "read write trust",
        "jti": "da2969c9-36e1-4177-b85e-71ed4217a036"
    }
    

    客户端配置(扩展AuthorizationServerConfigurerAdapter):

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security.allowFormAuthenticationForClients();
    }
    

    建议:

    如果要用户登录,请考虑password授权模式。

    另见 RFC6749 client_credentials

    【讨论】:

    • 感谢您的帮助@alex 我仍然遇到问题。我正在使用密码授予模式。我想我应该将 clientId 和 secret 放在带有 base64 编码的标头中。
    猜你喜欢
    • 2023-02-22
    • 2018-07-28
    • 2019-02-02
    • 2020-05-25
    • 2018-10-17
    • 2015-10-23
    • 1970-01-01
    • 2022-10-23
    • 2021-11-10
    相关资源
    最近更新 更多