我正在根据以下内容使用 JpaRegisteredClient 存储库配置 Spring Oauth2 授权服务器: https://docs.spring.io/spring-authorization-server/docs/current/reference/html/guides/how-to-jpa.html
要有一个客户,我只需将下一个值放入数据库:
INSERT INTO client(id, authorization_grant_types,
client_authentication_methods,
client_id,
client_id_issued_at,
client_name,
client_secret,
client_secret_expires_at,
client_settings,
redirect_uris,
scopes,
token_settings)
VALUES('abbc70f1-fb59-4b42-b1e4-c52fa0080bea',
'refresh_token,client_credentials,authorization_code',
'client_secret_basic',
'client',
null,
'abbc70f1-fb59-4b42-b1e4-c52fa0080bea',
'$2a$10$lcGI9Fp6GLfk7wjyOK0VqORQqMtsQRoC3J7i/V023SgQv9JZLZ01K',
null,
'{"@class":"java.util.Collections$UnmodifiableMap","settings.client.require-proof-key":true,"settings.client.require-authorization-consent":true}',
'http://localhost:9090/home,https://oauthdebugger.com/debug,https://oidcdebugger.com/debug',
'read,openid,profile,client.read,client.create',
'{"@class":"java.util.Collections$UnmodifiableMap","settings.token.reuse-refresh-tokens":true,
"settings.token.id-token-signature-algorithm":["org.springframework.security.oauth2.jose.jws.SignatureAlgorithm","RS256"],
"settings.token.access-token-time-to-live":["java.time.Duration",86400.000000000],
"settings.token.access-token-format":{"@class":"org.springframework.security.oauth2.server.authorization.settings.OAuth2TokenFormat",
"value":"self-contained"},"settings.token.refresh-token-time-to-live":["java.time.Duration",3600.000000000],
"settings.token.authorization-code-time-to-live":["java.time.Duration",300.000000000]}');
当我尝试使用 client_id: client and client secret: secret 获取 Postman 的访问令牌时,它工作得很好,当然范围只是 client.create 这意味着我可以使用此访问令牌来进行客户端注册终点一次。下一步我将使用数据库中的用户名和密码进行身份验证。我的数据库中有三个已声明的用户。作为回应,我得到了一个访问令牌。 最后一步是点击安全配置中声明的客户端注册端点:
.oidcClientRegistrationEndpoint("/clientinfo")
要注册一个新客户,我们需要发布一个 json,我的 json 在下面:
这很简单,我添加了一些范围和 grant_types 以供将来使用。 一切顺利,响应如下:
客户端成功放入数据库。 但是,当我尝试使用长度为 43 的 client_id (y_WektKwyeeJFYY2QP8r8VqEHOO0r9chrD3gTTEKaQI) 和长度为 6 的客户端密码 (XhPHxoq3yWpmgaRLEhb4BQanebh_Y2pohMpvDC4phMLvLenqrODxnz6oq60jUCbf) 获取访问令牌时4 我遇到了一个错误。
Encoded password does not look like BCrypt
我正在使用 BCryptPasswordEncoder。当然,我正在尝试实施 DelegatingPasswordEncoder 但它没有帮助。即使在尝试使用来自 SQL 代码的默认客户端(您可以在上面找到它)来获取具有 client.create 范围的访问令牌时,也会出现错误响应。我猜想在 BCryptPasswordEncoder.class 中声明的 matcher() 方法存在一些问题。当 BCrypt 只能编码/解码 60 个符号时,来自成功响应的客户端密码是 64 个符号。 当我使用调试器找出问题所在时,我意识到新创建的 client_secret 确实无法解码。 请给我一个解决方案。