Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在SpringSecurityOAuth2中使用非密码授权类型?_Spring_Spring Security_Oauth 2.0_Spring Security Oauth2_Spring Oauth2 - Fatal编程技术网

如何在SpringSecurityOAuth2中使用非密码授权类型?

如何在SpringSecurityOAuth2中使用非密码授权类型?,spring,spring-security,oauth-2.0,spring-security-oauth2,spring-oauth2,Spring,Spring Security,Oauth 2.0,Spring Security Oauth2,Spring Oauth2,我看到Spring Security Oauth2有不同的授权类型: 如何使用不是密码的授权类型 我目前有基于密码的身份验证工作 $ 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

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

如何使用不是密码的授权类型

我目前有基于密码的身份验证工作

$ 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”

然后我试着

$ 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\u类型的列表

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


对于基于客户端凭据的令牌,请使用
客户端凭据
作为
授权类型

$ 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\u类型的列表,因为grant类型是使用任何人都可以实现的接口定义的。然而,目前在Spring Security OAuth2框架中,有四个主要的
grant_type
是开箱即用实现的,它们是:


  • 您可以将上述任何一项用作
    授权类型

    我收到错误
    {“错误”:“无效的客户端”,“错误描述”:“未经授权的授权类型:客户端凭据”}
    。尽管我添加了
    .authorizedGrantTypes(“客户端凭据”…
    。顺便说一句,我使用的是Spring Security Oauth2 2.0.15版。我不知道为什么,但在我清理并重建了项目后,它现在可以正常工作了。
    客户端凭据
    不是
    客户端凭据
    对不起,至少编辑了6个字符。