gpt4 book ai didi

java - 没有 client_secret 的 Spring Security OAuth 2.0

转载 作者:行者123 更新时间:2023-11-29 09:28:48 27 4
gpt4 key购买 nike

我正在使用 Spring Boot 和 Spring Security OAuth 2.0 为我的 Android 应用程序开发登录系统。

我的起点是以下演示存储库:https://github.com/royclarkson/spring-rest-service-oauth。在演示中,您可以找到以下设置:

OAuth2 客户端:

clients
.inMemory()
.withClient("clientapp")
.authorizedGrantTypes("password", "refresh_token")
.authorities("USER")
.scopes("read", "write")
.resourceIds(RESOURCE_ID)
.secret("123456");

在其测试中获取访问 token 的方法:

private String getAccessToken(String username, String password) throws Exception {
String authorization = "Basic " + new String(Base64Utils.encode("clientapp:123456".getBytes()));

String content = mvc
.perform(
post("/oauth/token")
.header("Authorization", authorization)
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.param("username", username)
.param("password", password)
.param("grant_type", "password")
.param("scope", "read write")
.param("client_id", "clientapp")
.param("client_secret", "123456"))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();

return content.substring(17, 53);
}

项目中的每个测试都提供完美的工作,但我想以不同的方式做事,但我在这样做时遇到了麻烦。如您所见,演示客户端定义了一个 client_secret(也在测试中使用)但是 client_secret 在 Android 环境中真的毫无值(value),我不能保证它的 '隐私”。

参见 https://apigility.org/documentation/auth/authentication-oauth2:

If we are using a public client (by default, this is true when no secret is associated with the client) you can omit the client_secret value;

并查看 https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-31#section-2.1 :

Public:Clients incapable of maintaining the confidentiality of theircredentials (e.g. clients executing on the device used by theresource owner such as an installed native application or a webbrowser-based application), and incapable of secure clientauthentication via any other means.

所以我所做的就是删除客户端配置中的 secret :

clients
.inMemory()
.withClient("books_password_client")
.authorizedGrantTypes("password", "refresh_token")
.authorities("USER")
.scopes("read", "write")
.resourceIds(RESOURCE_ID);

还修改了getAccessToken(...)方法:

private String getAccessToken(String username, String password) throws Exception {
String authorization = "Basic " + new String(Base64Utils.encode("books_password_client:123456".getBytes()));

String content = mvc
.perform(
post("/oauth/token")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.param("username", username)
.param("password", password)
.param("grant_type", "password")
.param("client_id", "books_password_client"))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();

return content.substring(17, 53);}
}

但是当我使用这个新设置时,我的测试失败了,我无法获得访问 token ,我不断收到 HTTP 错误 401 Unauthorized。

最佳答案

这就是我的工作:

Response response =
given().
auth()
.preemptive().basic("clientapp", "")
.formParam("username", username)
.formParam("password", password)
.formParam("grant_type", "password")
.formParam("scope", "read%20write")
when()
.post("/oauth/token").
then()
.extract().response();

关于java - 没有 client_secret 的 Spring Security OAuth 2.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32890255/

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