【问题标题】:Spring security Oauth2 token error: "Missing grant type"Spring security Oauth2 令牌错误:“缺少授权类型”
【发布时间】:2018-05-03 16:38:25
【问题描述】:

我已经使用 OAuth2 和 JWT 保护了一个端点,但是在尝试进行身份验证时,我不断收到错误消息:“缺少授权类型”。我已尝试按照其他主题中的建议添加 Content-Type 标头,但它没有用。这是我的 curl 命令:

curl -vu aClient:aSecret -X POST --header "Content-Type:application/x-www-form-urlencoded" 'http://localhost:9000/oauth/token?username=mauricio.coder&password=123&grant_type=password'
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 9000 (#0)
* Server auth using Basic with user 'aClient'
> POST /oauth/token?username=mauricio.coder&password=123&grant_type=password HTTP/1.1
> Host: localhost:9000
> Authorization: Basic YUNsaWVudDphU2VjcmV0
> User-Agent: curl/7.52.1
> Accept: */*
> Content-Type:application/x-www-form-urlencoded
> 
< HTTP/1.1 400 
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< X-Frame-Options: DENY
< X-Application-Context: application:9000
< Cache-Control: no-store
< Pragma: no-cache
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Mon, 20 Nov 2017 12:34:10 GMT
< Connection: close
< 
* Curl_http_done: called premature == 0
* Closing connection 0
{"error":"invalid_request","error_description":"Missing grant type"}

有什么问题吗?

【问题讨论】:

    标签: java spring oauth jwt


    【解决方案1】:

    您需要将数据作为表单数据而不是 url 参数传递。你的命令应该是 ..

    curl -u aClient:aSecret --data "grant_type=password&username=mauricio.coder&password=123" -X POST -H "Content-Type:application/x-www-form-urlencoded" http://localhost:9000/oauth/token
    

    以下评论中的其他问题更新:

    根据 spring oauth2 文档

    通过注入 AuthenticationManager 来启用密码授予。

    Ref - http://projects.spring.io/spring-security-oauth/docs/oauth2.html 了解更多详情。

    因此,如果您的配置如下所示,它应该可以很好地用于密码授予流程。

    @Configuration
    @EnableAuthorizationServer
    protected static class OAuth2AuthorizationConfig extends
                AuthorizationServerConfigurerAdapter {
    
        @Autowired
        @Qualifier("authenticationManagerBean")
        private AuthenticationManager authenticationManager;
    
        @Override
        public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
                clients.inMemory()
                        .withClient("myclient")
                        .secret("myclientsecret")
                        .authorizedGrantTypes("authorization_code", "refresh_token", "password")
                ...
        }
    
    }
    

    【讨论】:

    • 感谢答案。但现在我收到以下错误: {"error":"unsupported_grant_type","error_description":"Unsupported grant type: password"}
    • 但是我已经配置了AuthorizationServer来支持它:clients.authorizedGrantTypes("password", "refresh_token")
    • 有人有解决办法吗?
    • @Vesko Vujovic - 请查看我的更新答案。让我知道它是否满足您的查询或您是否有更多查询。
    猜你喜欢
    • 2015-05-16
    • 1970-01-01
    • 2017-12-11
    • 2017-01-14
    • 2013-07-31
    • 2018-07-04
    • 2019-08-09
    • 2014-05-10
    • 2013-12-01
    相关资源
    最近更新 更多