gpt4 book ai didi

java - Spring安全Oauth2 token 错误: "Missing grant type"

转载 作者:行者123 更新时间:2023-12-01 20:05:52 25 4
gpt4 key购买 nike

我已使用 OAuth2 和 JWT 保护端点,但在尝试进行身份验证时,我不断收到错误:“缺少授权类型”。我尝试按照其他主题中的建议添加 Content-Type header ,但没有用。这是我的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"}

有什么问题吗?

最佳答案

您需要将数据作为表单数据而不是 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 文档

password grants are switched on by injecting an AuthenticationManager.

引用 - 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")
...
}

}

关于java - Spring安全Oauth2 token 错误: "Missing grant type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47392372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com