Spring OAuth 2 调用 /oauth/token 结果为 401(未授权)
Spring OAuth 2 Call /oauth/token Resulted in 401 (Unauthorized)
大家好,我尝试通过 Spring Security OAuth 配置简单的授权代码流。
我通过以下方法测试了我的授权和资源服务器配置:
- 创建一个 Web 应用程序作为客户端并使用其页面触发对 /oauth/authorize 的 http post 调用。
- 获取代码后,我使用相同的页面
使用代码触发另一个 http post 并获取令牌。
- 最后,我用
curl -H 将令牌放在 header 中并从受保护的位置获取响应
资源。
但是当我尝试使用 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"/>
如果有人知道哪里出了问题,请告诉我。
我上面的配置有两个问题。
- 我注意到我的客户端使用了错误的密钥与授权服务器通信。
- 授权服务器上的令牌端点使用身份验证管理器
服务于用户认证。结果
在我为创建新的安全领域之前,客户端一直被拒绝
令牌端点并将其配置为使用专为
客户.
注意客户端不同于用户。客户端是第三方想要访问属于您的用户(也称为资源所有者)的资源。
我遇到了同样的问题。它有助于添加
org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService
到 spring 证券认证管理器,将 clientDetailsService 粘合到认证管理器。所以
<authentication-manager alias="authenticationManager">
...
<authentication-provider user-service-ref="clientDetailsUserDetailsService"/>
...
</authentication-manager>
几乎解决了我的问题。我还有一个问题:由于 ClientDetailsUserDetailsService 没有默认构造函数,spring 抛出了形式为
的异常
org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class
[class org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService]:
Common causes of this problem include using a final class or a non-visible class;
nested exception is java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given
如果不使用 class 的副本接收 clientDetailsService 作为 属性 而不是构造函数 arg,我无法解决这个问题。
大家好,我尝试通过 Spring Security OAuth 配置简单的授权代码流。
我通过以下方法测试了我的授权和资源服务器配置:
- 创建一个 Web 应用程序作为客户端并使用其页面触发对 /oauth/authorize 的 http post 调用。
- 获取代码后,我使用相同的页面 使用代码触发另一个 http post 并获取令牌。
- 最后,我用 curl -H 将令牌放在 header 中并从受保护的位置获取响应 资源。
但是当我尝试使用 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"/>
如果有人知道哪里出了问题,请告诉我。
我上面的配置有两个问题。
- 我注意到我的客户端使用了错误的密钥与授权服务器通信。
- 授权服务器上的令牌端点使用身份验证管理器 服务于用户认证。结果 在我为创建新的安全领域之前,客户端一直被拒绝 令牌端点并将其配置为使用专为 客户.
注意客户端不同于用户。客户端是第三方想要访问属于您的用户(也称为资源所有者)的资源。
我遇到了同样的问题。它有助于添加
org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService
到 spring 证券认证管理器,将 clientDetailsService 粘合到认证管理器。所以
<authentication-manager alias="authenticationManager">
...
<authentication-provider user-service-ref="clientDetailsUserDetailsService"/>
...
</authentication-manager>
几乎解决了我的问题。我还有一个问题:由于 ClientDetailsUserDetailsService 没有默认构造函数,spring 抛出了形式为
的异常 org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class
[class org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService]:
Common causes of this problem include using a final class or a non-visible class;
nested exception is java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given
如果不使用 class 的副本接收 clientDetailsService 作为 属性 而不是构造函数 arg,我无法解决这个问题。