1
0
mirror of synced 2026-08-05 09:47:05 +00:00

Add OAuth2 Client HandlerMethodArgumentResolver

Fixes gh-4651
This commit is contained in:
Joe Grandja
2018-03-08 06:09:50 -05:00
parent 982fc360b2
commit 526e0fdd4f
16 changed files with 850 additions and 71 deletions
@@ -15,12 +15,9 @@
*/
package sample.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.client.ClientAuthorizationRequiredException;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.annotation.OAuth2Client;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@@ -35,9 +32,7 @@ import java.util.List;
* @author Joe Grandja
*/
@Controller
public class MainController {
@Autowired
private OAuth2AuthorizedClientService authorizedClientService;
public class GitHubReposController {
@GetMapping("/")
public String index() {
@@ -45,16 +40,7 @@ public class MainController {
}
@GetMapping("/repos")
public String gitHubRepos(Model model, Authentication authentication) {
String registrationId = "github";
OAuth2AuthorizedClient authorizedClient =
this.authorizedClientService.loadAuthorizedClient(
registrationId, authentication.getName());
if (authorizedClient == null) {
throw new ClientAuthorizationRequiredException(registrationId);
}
public String gitHubRepos(Model model, @OAuth2Client("github") OAuth2AuthorizedClient authorizedClient) {
String endpointUri = "https://api.github.com/user/repos";
List repos = WebClient.builder()
.filter(oauth2Credentials(authorizedClient))
@@ -145,7 +145,7 @@ public class OAuth2LoginApplicationTests {
}
@Test
public void requestAuthorizeClientWhenInvalidClientThenStatusBadRequest() throws Exception {
public void requestAuthorizeClientWhenInvalidClientThenStatusInternalServerError() throws Exception {
HtmlPage page = this.webClient.getPage("/");
ClientRegistration clientRegistration = this.clientRegistrationRepository.findByRegistrationId("google");
@@ -161,7 +161,7 @@ public class OAuth2LoginApplicationTests {
response = ex.getResponse();
}
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST.value());
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR.value());
}
@Test
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,15 +15,13 @@
*/
package sample.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.security.oauth2.client.annotation.OAuth2Client;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.reactive.function.client.ClientRequest;
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
import org.springframework.web.reactive.function.client.WebClient;
@@ -36,22 +34,17 @@ import java.util.Map;
* @author Joe Grandja
*/
@Controller
public class MainController {
public class OAuth2LoginController {
@Autowired
private OAuth2AuthorizedClientService authorizedClientService;
@RequestMapping("/")
public String index(Model model, OAuth2AuthenticationToken authentication) {
OAuth2AuthorizedClient authorizedClient = this.getAuthorizedClient(authentication);
model.addAttribute("userName", authentication.getName());
@GetMapping("/")
public String index(Model model, @OAuth2Client OAuth2AuthorizedClient authorizedClient) {
model.addAttribute("userName", authorizedClient.getPrincipalName());
model.addAttribute("clientName", authorizedClient.getClientRegistration().getClientName());
return "index";
}
@RequestMapping("/userinfo")
public String userinfo(Model model, OAuth2AuthenticationToken authentication) {
OAuth2AuthorizedClient authorizedClient = this.getAuthorizedClient(authentication);
@GetMapping("/userinfo")
public String userinfo(Model model, @OAuth2Client OAuth2AuthorizedClient authorizedClient) {
Map userAttributes = Collections.emptyMap();
String userInfoEndpointUri = authorizedClient.getClientRegistration()
.getProviderDetails().getUserInfoEndpoint().getUri();
@@ -69,11 +62,6 @@ public class MainController {
return "userinfo";
}
private OAuth2AuthorizedClient getAuthorizedClient(OAuth2AuthenticationToken authentication) {
return this.authorizedClientService.loadAuthorizedClient(
authentication.getAuthorizedClientRegistrationId(), authentication.getName());
}
private ExchangeFilterFunction oauth2Credentials(OAuth2AuthorizedClient authorizedClient) {
return ExchangeFilterFunction.ofRequestProcessor(
clientRequest -> {