【问题标题】:How do you use a non-password grant type in Spring Security Oauth2?如何在 Spring Security Oauth2 中使用非密码授权类型?
【发布时间】:2019-01-07 00:50:00
【问题描述】:

我看到 Spring Security Oauth2 有不同的授权类型:

https://projects.spring.io/spring-security-oauth/docs/oauth2.html#grant-types

如何使用不是password 的授权类型?

我目前正在使用基于密码的身份验证。

$ curl -u client:123456 http://localhost:8080/oauth/token -d grant_type=password -d username=admin -d password=admin -d client_id=client -d client_secret=123456 -d scope=write
{"access_token":"40add879-3be3-4d94-b969-4dc17975c659","token_type":"bearer","refresh_token":"583eb284-3318-432b-a8a3-b3eee744c9b7","expires_in":40688,"scope":"write"}

我注释掉了

@EnableAuthorizationServer
public class OAuth2Configuration extends AuthorizationServerConfigurerAdapter {
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints
            .tokenStore(this.tokenStore).userDetailsService(userDetailsService);
//          .authenticationManager(new AuthenticationManager() {
//          @Override
//          public Authentication authenticate(Authentication authentication)
//                  throws AuthenticationException {
//              return authenticationManagerBuilder.getOrBuild().authenticate(authentication);
//          }
//      });

因此它将不再使用密码授权类型。 (“通过注入 AuthenticationManager 开启密码授权”)。

然后我在 API 中搜索“grant”

https://docs.spring.io/spring-security/oauth/apidocs/index.html

然后我尝试了

$ curl -u client:123456 http://localhost:8080/oauth/token -d grant_type=token -d client_id=client -d client_secret=123456 -d scope=write
{"error":"unsupported_grant_type","error_description":"Unsupported grant type: token"}
$ curl -u client:123456 http://localhost:8080/oauth/token -d grant_type=client -d client_id=client -d client_secret=123456 -d scope=write
{"error":"unsupported_grant_type","error_description":"Unsupported grant type: client"}
$ curl -u client:123456 http://localhost:8080/oauth/token -d grant_type=code -d client_id=client -d client_secret=123456 -d scope=write
{"error":"unsupported_grant_type","error_description":"Unsupported grant type: code"}

那么,如何在不使用数据库中的用户名和密码的情况下创建仅客户端/秘密授权类型来获取访问令牌?我在任何地方都找不到有效的grant_types 列表!

这似乎是定义允许的授权类型的方法,但它没有指定有效值。

https://docs.spring.io/spring-security/oauth/apidocs/org/springframework/security/oauth2/config/annotation/builders/ClientDetailsServiceBuilder.ClientBuilder.html#authorizedGrantTypes(java.lang.String...)

【问题讨论】:

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


    【解决方案1】:

    对于基于客户端凭据的令牌,使用 client_crendentials 作为 grant_type

    $ curl -u client:123456 http://localhost:8080/oauth/token -d grant_type=client_crendentials -d client_id=client -d client_secret=123456 -d scope=write
    

    没有grant_types 这样的列表,因为授权类型是使用任何人都可以实现的TokenGranter 接口定义的。然而,目前在 Spring Security OAuth2 框架中有四个开箱即用的grant_type,它们如下:

    1. implicit
    2. client_credentials
    3. password
    4. refresh_token

    您可以使用以上任何一种作为grant_type

    【讨论】:

    • 我收到错误 {"error":"invalid_client","error_description":"Unauthorized grant type: client_credentials"}。即使我添加了.authorizedGrantTypes("client_crendentials", ...。顺便说一句,我使用的是 Spring Security Oauth2 2.0.15 版。
    • 我不知道为什么,但是在我清理项目并重建后,它现在可以工作了。
    • client_credentials 不是 client_crendentials 抱歉,至少编辑了 6 个字符。
    猜你喜欢
    • 1970-01-01
    • 2015-06-25
    • 2018-09-07
    • 2018-05-03
    • 2014-05-10
    • 2013-12-01
    • 1970-01-01
    • 2021-04-24
    • 2015-05-22
    相关资源
    最近更新 更多