gpt4 book ai didi

java - 了解 OAuth2 客户端凭据流

转载 作者:IT老高 更新时间:2023-10-28 13:52:49 26 4
gpt4 key购买 nike

我正在尝试了解并实现我们的新 REST 服务器和现有客户端应用程序之间的客户端凭据流。我已经设置了 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 代码):

@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(

看来,我需要先对服务器进行身份验证。但这不是我想做的。我希望我的两台服务器使用共享 key 相互通信。 OAuth 提供者服务器应根据请求向(受信任的)客户端服务器提供访问 token ,以便客​​户端服务器随后可以使用该 token 访问服务器上的所有 REST 资源。这应该保护 REST 资源免受外部访问。

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

看起来我可能对整个客户端凭据流程或那里的 spring-security 应用存在一些误解,因此将不胜感激。

最佳答案

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

你需要做这样的事情:

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

这是向授权服务器验证客户端,然后指定 grant_type 和其他参数。这将返回类型为“承载”的访问 token ,其范围由 oauth 客户端详细信息确定。获得 token 后,您可以通过设置 Authorization header 来访问 protected 资源:

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

关于java - 了解 OAuth2 客户端凭据流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14140020/

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