gpt4 book ai didi

spring-security-oauth2 - Spring OAuth 2 调用/oauth/token 导致 401(未授权)

转载 作者:行者123 更新时间:2023-12-04 18:03:16 27 4
gpt4 key购买 nike

大家好,我尝试通过 Spring Security OAuth 配置简单的授权代码流。

我通过以下方法测试了我的授权和资源服务器配置:

  1. 创建一个 Web 应用程序作为客户端,并使用其页面触发对/oauth/authorize 的 http post 调用。
  2. 获得代码后,我使用同一页面使用代码触发另一个 http 帖子并获取 token 。
  3. 最后,我使用curl -H 将 token 放在 header 内并从 protected 位置获取响应资源。

但是当我尝试使用 rest 模板时。它抛出错误消息 401 未经授权的错误。

服务器端 - 安全配置:

<http auto-config="true" pattern="/protected/**"
authentication-manager-ref="authenticationManager">
<custom-filter ref="resourceFilter" before="PRE_AUTH_FILTER" />
<csrf disabled="true" />
</http>

<http auto-config="true">
<intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
<form-login default-target-url="/admin.html" />
<logout logout-success-url="/welcome.html" logout-url="/logout"/>
<csrf disabled="true" />
</http>

<authentication-manager alias="authenticationManager">
<authentication-provider>
<user-service>
<user name="admin" password="123456" authorities="ROLE_USER,ROLE_ADMIN" />
</user-service>
</authentication-provider>
</authentication-manager>

服务器端 - 授权和资源配置:

<oauth:authorization-server
client-details-service-ref="clientDetails" error-page="error">
<oauth:authorization-code />
</oauth:authorization-server>

<oauth:client-details-service id="clientDetails">
<oauth:client client-id="admin" secret="fooSecret" />
</oauth:client-details-service>

<oauth:resource-server id="resourceFilter" />

客户端:

<oauth:client id="oauth2ClientContextFilter" />
<oauth:resource id="sso" client-id="admin"
access-token-uri="http://localhost:8080/tough/oauth/token"
user-authorization-uri="http://localhost:8080/tough/oauth/authorize"
use-current-uri="true" client-secret="secret"
client-authentication-scheme="header" type="authorization_code"
scope="trust" />
<oauth:rest-template id="template" resource="sso"/>

如果有人知道哪里出了问题,请告诉我。

最佳答案

我上面的配置有两个问题。

  1. 我注意到我的客户端使用了错误的 key 与授权服务器通信。
  2. 授权服务器上的 token 端点使用身份验证管理器服务于用户认证。结果在我为创建新的安全领域之前,客户端一直被拒绝 token 端点并将其配置为使用专为客户。

注意客户端不同于用户。客户端是第三方想要访问属于您的用户(也称为资源所有者)的资源。

关于spring-security-oauth2 - Spring OAuth 2 调用/oauth/token 导致 401(未授权),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31588356/

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