gpt4 book ai didi

java - Spring Security 5.3.2 OAuth 2,资源所有者密码凭证流程 - 如何向授权服务器 uri 添加额外的 HEADER 参数

转载 作者:行者123 更新时间:2023-12-01 16:42:58 25 4
gpt4 key购买 nike

我正在尝试使用资源所有者密码凭据流程从自定义公司 oauth 2 授权服务器生成访问 token 。

参见https://www.rfc-editor.org/rfc/rfc6749#section-4.3

该服务器仅在收到以下参数时生成访问 token :

POST https://custom_corporate_server/auth/oauth/v2/token

Header
idp: 99

Body
grant_type: password
scope: my_scope
client_id: 00******-****-****-****-**********99
client_secret: 00******-****-****-****-**********99
username: my_user
password: my_password

它们的配置需要额外的 header 自定义参数:idp - 应该是数字。

我正在使用 Spring Boot 2.3.0 和 Spring Security 5.3.2。

我按照下面的链接构建了我的测试示例: https://docs.spring.io/spring-security/site/docs/5.3.2.RELEASE/reference/html5/#using-the-access-token-2

@Bean
public OAuth2AuthorizedClientManager authorizedClientManager(
ClientRegistrationRepository clientRegistrationRepository,
OAuth2AuthorizedClientRepository authorizedClientRepository) {

OAuth2AuthorizedClientProvider authorizedClientProvider =
OAuth2AuthorizedClientProviderBuilder.builder()
.password()
.refreshToken()
.build();

DefaultOAuth2AuthorizedClientManager authorizedClientManager =
new DefaultOAuth2AuthorizedClientManager(
clientRegistrationRepository, authorizedClientRepository);
authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);

// Assuming the `username` and `password` are supplied as `HttpServletRequest` parameters,
// map the `HttpServletRequest` parameters to `OAuth2AuthorizationContext.getAttributes()`
authorizedClientManager.setContextAttributesMapper(contextAttributesMapper());

return authorizedClientManager;
}

private Function<OAuth2AuthorizeRequest, Map<String, Object>> contextAttributesMapper() {
return authorizeRequest -> {
Map<String, Object> contextAttributes = Collections.emptyMap();
HttpServletRequest servletRequest = authorizeRequest.getAttribute(HttpServletRequest.class.getName());
String username = servletRequest.getParameter(OAuth2ParameterNames.USERNAME);
String password = servletRequest.getParameter(OAuth2ParameterNames.PASSWORD);
if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
contextAttributes = new HashMap<>();

// `PasswordOAuth2AuthorizedClientProvider` requires both attributes
contextAttributes.put(OAuth2AuthorizationContext.USERNAME_ATTRIBUTE_NAME, username);
contextAttributes.put(OAuth2AuthorizationContext.PASSWORD_ATTRIBUTE_NAME, password);
}
return contextAttributes;
};
}

我无法将此 header 中的参数传递到授权服务器。如何做到这一点是我今天面临的主要难题。

最佳答案

看看this文章,它解释了授权和 token 请求的各种自定义。在您的情况下,有关 token request extra parameters 的部分,似乎准确地描述了您所需要的。

你可以这样做:

public class CustomRequestEntityConverter implements Converter<OAuth2PasswordGrantRequest, RequestEntity<?>> {

private OAuth2PasswordGrantRequestEntityConverter defaultConverter;

public CustomRequestEntityConverter() {
defaultConverter = new OAuth2PasswordGrantRequestEntityConverter();
}

@Override
public RequestEntity<?> convert(OAuth2PasswordGrantRequest req) {
RequestEntity<?> entity = defaultConverter.convert(req);
MultiValueMap<String, String> params = entity.getHeaders();
params.add("idp", "99");
return new RequestEntity<>(params, entity.getHeaders(), entity.getMethod(), entity.getUrl());
}
}

关于java - Spring Security 5.3.2 OAuth 2,资源所有者密码凭证流程 - 如何向授权服务器 uri 添加额外的 HEADER 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61823358/

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