首页 文章

了解OAuth2客户端凭据流

提问于
浏览
16

我'm trying to understand and implement a client credentials flow between our new REST server and our existing client app. I'已设置spring-security OAuth2,如this . 根据我的理解,到目前为止,我的服务器现在应该支持以下请求:

$ curl -X -v -d 'client_id=the_client&client_secret=secret&grant_type=client_credentials' -X POST "http://localhost:9090/oauth/token"

但我明白了

InsufficientAuthenticationException: There is no client authentication

Principal 引起 null 这里(spring-security code):

@FrameworkEndpoint
@RequestMapping(value = "/oauth/token")
public class TokenEndpoint extends AbstractEndpoint {

    @RequestMapping
    public ResponseEntity<OAuth2AccessToken> getAccessToken(Principal principal,
            @RequestParam("grant_type") String grantType, @RequestParam Map<String, String> parameters) {

        if (!(principal instanceof Authentication)) {
            throw new InsufficientAuthenticationException(

所以看来,我需要先 authenticate against the server . 但那是 not what I want to do . 我希望我的两台服务器使用共享密钥互相通信 . OAuth提供程序服务器应根据请求向(受信任)客户端服务器提供访问令牌,以便客户端服务器可以使用该令牌访问服务器上的所有REST资源 . 这应该保护REST资源免受外部访问 .

后来我想向第三方提供所选资源,并最终为服务器到服务器通信实现一些更细粒度的安全性 . 但是现在我需要保护REST服务器免受外部访问 .

看起来我可能对整个客户端凭证流程或 spring 安全的应用有一些误解,因此任何澄清将非常感激 .

1 回答

  • 19

    您没有向授权服务器验证您的客户端 .

    你需要做这样的事情:

    curl --user the_client:secret --data "grant_type=client_credentials" http://localhost:9090/oauth/token
    

    这是向授权服务器验证客户端,然后指定grant_type和其他参数 . 这将返回“bearer”类型的访问令牌,其范围由oauth客户端详细信息确定 . 获得令牌后,您可以通过设置Authorization标头来访问受保护的资源:

    curl -H "Authorization: Bearer <accessToken>" <resourceUrl>
    

相关问题