Expected Behavior
when using http.oauth2Client() api i am aware that i should implement actual user(resource owner) authentication by myself
@GetMapping("/client")
public String client(HttpServletRequest request, Model model){
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String clientRegistrationId = "keycloak";
OAuth2AuthorizedClient oAuth2AuthorizedClient = authorizedClientRepository.loadAuthorizedClient(clientRegistrationId, authentication, request);
OAuth2AuthorizedClient oAuth2AuthorizedClient1 = authorizedClientService.loadAuthorizedClient(clientRegistrationId, authentication.getName());
System.out.println("oAuth2AuthorizedClient = " + oAuth2AuthorizedClient);
System.out.println("oAuth2AuthorizedClient1 = " + oAuth2AuthorizedClient1);
OAuth2AccessToken accessToken = oAuth2AuthorizedClient.getAccessToken();
OAuth2UserService oAuth2UserService = new DefaultOAuth2UserService();
OAuth2User oauth2User = oAuth2UserService.loadUser(new OAuth2UserRequest(oAuth2AuthorizedClient.getClientRegistration(), accessToken));
OAuth2AuthenticationToken authenticationToken = new OAuth2AuthenticationToken
(oauth2User, Arrays.asList(new SimpleGrantedAuthority("ROLE_USER")),clientRegistrationId);
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
model.addAttribute("accessToken", oAuth2AuthorizedClient.getAccessToken().getTokenValue());
model.addAttribute("refreshToken", oAuth2AuthorizedClient.getRefreshToken().getTokenValue());
model.addAttribute("principalName", oauth2User.getName());
model.addAttribute("clientName", oAuth2AuthorizedClient.getClientRegistration().getClientName());
return "client";
}
so i configured auth-server(keycloak) to redirect to /client
but then i realized authorizedClientService.loadAuthorizedClient() won't give AuthorizedClient because User was not authenticated and still anonymous and it was saved into AuthenticatedPrincipalOAuth2AuthorizedClientRepository.anonymousAuthorizedClientRepository so it was never saved into InMemoryOAuth2AuthorizedClientService.authorizedClients
since it is service layer and looking at the method name and infer its return shouldn't it return AuthorizedClient properly?
Current Behavior
authorizedClientService.loadAuthorizedClient() method doesn't return AuthorizedClients if actual User isn't authenticated (authenticated as anonymous)
Context
i can workaround by using authorizedClientRepository.loadAuthorizedClient() method
Comment From: sjohnr
Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. We prefer to use GitHub issues only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add a minimal sample that reproduces this issue if you feel this is a genuine bug.
A few resources for you to look at:
http.oauth2Login()- OAuth 2.0 Login@RegisteredOAuth2AuthorizedClient- Resolving an Authorized Client
I believe these resources should help you. If you feel I have misunderstood your request or feel you can clarify the enhancement you're proposing, please let me know and we can re-open if necessary.