gpt4 book ai didi

Spring Security 5.2 密码流程

转载 作者:行者123 更新时间:2023-12-03 23:49:45 32 4
gpt4 key购买 nike

我正在尝试使用最新版本的 Spring Security - 5.2 中的密码流对用户进行身份验证。

docs seem to suggest怎么做。

@Bean
public OAuth2AuthorizedClientManager passwordFlowAuthorizedClientManager(
HttpClient httpClient,
ClientRegistrationRepository clientRegistrationRepository,
OAuth2AuthorizedClientRepository authorizedClientRepository) {

HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);

DefaultPasswordTokenResponseClient c = new DefaultPasswordTokenResponseClient();
RestTemplate client = new RestTemplate(requestFactory);
client.setMessageConverters(Arrays.asList(
new FormHttpMessageConverter(),
new OAuth2AccessTokenResponseHttpMessageConverter()));
client.setErrorHandler(new OAuth2ErrorResponseErrorHandler());
c.setRestOperations(client);

OAuth2AuthorizedClientProvider authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
.password(configurer -> configurer.accessTokenResponseClient(c))
.refreshToken()
.build();

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

authorizedClientManager.setContextAttributesMapper(authorizeRequest -> {
Map<String, Object> contextAttributes = new HashMap<>();
String username = authorizeRequest.getAttribute(OAuth2ParameterNames.USERNAME);
String password = authorizeRequest.getAttribute(OAuth2ParameterNames.PASSWORD);
contextAttributes.put(OAuth2AuthorizationContext.USERNAME_ATTRIBUTE_NAME, username);
contextAttributes.put(OAuth2AuthorizationContext.PASSWORD_ATTRIBUTE_NAME, password);
return contextAttributes;
});

return authorizedClientManager;
}




我执行请求,我可以看到在 HTTP header 中返回的访问 token ,但没有填充 SecurityContext 并且 session 用户保持匿名。
String username = "joe";
String password = "joe";
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
ClientRegistration r = clientRegistrationRepository.findByRegistrationId("keycloak");

OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId(r.getRegistrationId())
.principal(authentication)
.attributes(attrs -> {
attrs.put(OAuth2ParameterNames.USERNAME, username);
attrs.put(OAuth2ParameterNames.PASSWORD, password);
})
.build();
OAuth2AuthorizedClient authorizedClient = this.authorizedClientManager.authorize(authorizeRequest);

有任何想法吗?

最佳答案

在进一步阅读文档后,我认为 Spring Security 5.2 中的 Oauth 2 密码流与授权流的支持方式不同。 Spring Security 5.2 为 http 客户端提供密码流支持,它可以缓存授权请求并在 token 过期之前刷新 token - 但没有最终用户密码流支持,其中客户端将凭据代理到授权服务器。

当然,完全有可能通过获取凭据来对最终用户进行身份验证,实现一个自定义的 AuthenticationProvider,该提供者将凭据与授权服务器交换为 token ,并返回一个持久化到上下文的 OAuth2AuthenticationToken。

关于Spring Security 5.2 密码流程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59383562/

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