我已经使用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"}
有什么不对吗?
答案 0 :(得分:2)
您需要将数据作为表单数据而不是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来打开密码授予。
参考 - 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")
...
}
}