【问题标题】:Oauth2 and Spring-Security: Fail on authentication using Oauth2 and spring-securityOauth2 和 Spring-Security:使用 Oauth2 和 spring-security 进行身份验证失败
【发布时间】:2014-09-15 20:07:21
【问题描述】:

我想将 Oauth2 和 Spring-security 与 rest web 服务一起使用。我是新手,我正在尝试在一些网站的帮助下创建配置。不过,我显然不明白 oauth 验证休息服务的方式和流程是什么。我正在使用最新的依赖项Click on the link for see my configurations。我正在使用数据库来存储客户信息和令牌。当我尝试访问令牌时,使用带有发布请求和发布正文的以下 url:

http://localhost:8080/empirecl-api/oauth/token
grant_type=password&username=a@a.com&password=password&client_id=my-client-with-secret&client_secret=ak_secret

将出现基本身份验证弹出框。我正在使用 chrome rest postman 客户端。我的数据库中有用户表,用于存储用户名和密码,当我输入usernamepassword 时,身份验证失败,如果我使用我的client_idclient_secret 作为username 和@ 987654328@,认证失败。当我在json 之后取消基本身份验证的弹出时返回:

{
"error": "unauthorized",
"error_description": "No client with requested id: a@a.com"
}

我不知道会发生什么。

当我尝试使用 get reqquest 访问以下网址时:

http://localhost:8080/empirecl-api/oauth/authorize?grant_type=password&username=a@a.com&password=password

以下error 将返回:

{
"error": {
    "error": "invalid_client",
    "error_description": "Bad client credentials"
}
}

当我在 Eclipse 的控制台中检查错误时,会显示以下错误:

AuthorizationEndpoint:436 - Handling ClientRegistrationException error: No client with requested id: null

我如何解决这个问题以及如何将 Oauth 与我的 rest api 一起使用。我还想用 Oauth 保护我所有的 url。

【问题讨论】:

    标签: exception spring-security oauth-2.0 basic-authentication


    【解决方案1】:

    您需要将客户端用户名和密码作为 Base64 编码的授权标头传递。

    尝试使用 curl。这是一个例子:

    curl -v -X POST \
    -H "Content-Type: application/json" \
    -H "Authorization: Basic MzUzYjMwMmM0NDU3NGY1NjUwNDU2ODdlNTM0ZTdkNmE6Mjg2OTI0Njk3ZTYxNWE2NzJhNjQ2YTQ5MzU0NTY0NmM=" \
    'http://localhost:8080/empirecl-api/oauth/token?grant_type=password&username=a@a.com&password=password'

    我有一个描述here的工作示例

    【讨论】:

    • 你好,@porterhead `Basic MzUzYjMwMmM0NDU3NGY1NjUwNDU2ODdlNTM0ZTdkNmE6Mjg2OTI0Njk3ZTYxNWE2NzJhNjQ2YTQ5MzU0NTY0NmM="`的实际值是多少?
    • String toEncode = clientId + ":" + clientSecret String encodedStr = Base64.encodeToString(toEncode.bytes())
    • @lain Porter 我正在使用my-client-with-secret:123456789 客户端信息并编码为base64,但发送请求仍然存在问题。
    【解决方案2】:

    这里有几个问题。

    1. 您使用了错误的端点 - 请求应该是令牌端点,而不是授权端点。
    2. 它应该是一个 POST 请求 - 您不应该在 URL 中传递参数
    3. 当用户必须进行身份验证而客户端进行身份验证时,您会感到困惑 - 服务器正在从客户端查找基本凭据,但您输入的是用户名。

    the oauth2 spec section on using the resource owner password grant

    client needs to authenticate,正如 porterhead 指出的那样,您可以使用基本身份验证来执行此操作。该规范还允许将客户端 ID 和机密作为发布参数传递,但更喜欢基本身份验证。

    您可能应该确保您对 OAuth2 的工作原理有更多了解,并可能扩展您的问题以解释您为什么要使用它以及为什么选择这种特定的授权类型。

    【讨论】:

    • 您好@Luke 我能够生成令牌,但仍然存在保护我的资源的问题,我需要保护。从休息客户端,对资源的调用成功进行,无需任何身份验证。以下是配置:<sec:http pattern="/api/**" access-decision-manager-ref="accessDecisionManager" create-session="never" > <sec:anonymous enabled="false" /> <sec:intercept-url pattern="/api/**" method="POST"/> <sec:custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER"/><sec:http-basic entry-point-ref="oauthAuthenticationEntryPoint" /></sec:http>
    猜你喜欢
    • 2017-06-14
    • 1970-01-01
    • 2015-07-30
    • 1970-01-01
    • 2019-01-28
    • 2015-04-27
    • 2018-08-09
    • 2014-07-03
    • 2016-01-05
    相关资源
    最近更新 更多