1+ package com .baeldung .cfuaa .oauth2 .client ;
2+
3+ import org .springframework .beans .factory .annotation .Value ;
4+ import org .springframework .http .HttpEntity ;
5+ import org .springframework .http .HttpHeaders ;
6+ import org .springframework .http .HttpMethod ;
7+ import org .springframework .http .ResponseEntity ;
8+ import org .springframework .security .oauth2 .client .OAuth2AuthorizedClient ;
9+ import org .springframework .security .oauth2 .client .OAuth2AuthorizedClientService ;
10+ import org .springframework .security .oauth2 .client .authentication .OAuth2AuthenticationToken ;
11+ import org .springframework .security .oauth2 .core .OAuth2AccessToken ;
12+ import org .springframework .web .bind .annotation .RequestMapping ;
13+ import org .springframework .web .bind .annotation .RestController ;
14+ import org .springframework .web .client .HttpClientErrorException ;
15+ import org .springframework .web .client .RestTemplate ;
16+
17+ @ RestController
18+ public class CFUAAOAuth2ClientController {
19+
20+ @ Value ("${resource.server.url}" )
21+ private String remoteResourceServer ;
22+
23+ private RestTemplate restTemplate ;
24+
25+ private OAuth2AuthorizedClientService authorizedClientService ;
26+
27+ public CFUAAOAuth2ClientController (OAuth2AuthorizedClientService authorizedClientService ) {
28+ this .authorizedClientService = authorizedClientService ;
29+ this .restTemplate = new RestTemplate ();
30+ }
31+
32+ @ RequestMapping ("/" )
33+ public String index (OAuth2AuthenticationToken authenticationToken ) {
34+ OAuth2AuthorizedClient oAuth2AuthorizedClient = this .authorizedClientService .loadAuthorizedClient (authenticationToken .getAuthorizedClientRegistrationId (), authenticationToken .getName ());
35+ OAuth2AccessToken oAuth2AccessToken = oAuth2AuthorizedClient .getAccessToken ();
36+
37+ String response = "Hello, " + authenticationToken .getPrincipal ().getName ();
38+ response += "</br></br>" ;
39+ response += "Here is your accees token :</br>" + oAuth2AccessToken .getTokenValue ();
40+ response += "</br>" ;
41+ response += "</br>You can use it to call these Resource Server APIs:" ;
42+ response += "</br></br>" ;
43+ response += "<a href='/read'>Call Resource Server Read API</a>" ;
44+ response += "</br>" ;
45+ response += "<a href='/write'>Call Resource Server Write API</a>" ;
46+ return response ;
47+ }
48+
49+ @ RequestMapping ("/read" )
50+ public String read (OAuth2AuthenticationToken authenticationToken ) {
51+ String url = remoteResourceServer + "/read" ;
52+ return callResourceServer (authenticationToken , url );
53+ }
54+
55+ @ RequestMapping ("/write" )
56+ public String write (OAuth2AuthenticationToken authenticationToken ) {
57+ String url = remoteResourceServer + "/write" ;
58+ return callResourceServer (authenticationToken , url );
59+ }
60+
61+ private String callResourceServer (OAuth2AuthenticationToken authenticationToken , String url ) {
62+ OAuth2AuthorizedClient oAuth2AuthorizedClient = this .authorizedClientService .loadAuthorizedClient (authenticationToken .getAuthorizedClientRegistrationId (), authenticationToken .getName ());
63+ OAuth2AccessToken oAuth2AccessToken = oAuth2AuthorizedClient .getAccessToken ();
64+
65+ HttpHeaders headers = new HttpHeaders ();
66+ headers .add ("Authorization" , "Bearer " + oAuth2AccessToken .getTokenValue ());
67+
68+ HttpEntity <String > entity = new HttpEntity <>("parameters" , headers );
69+ ResponseEntity <String > responseEntity = null ;
70+
71+ String response = null ;
72+ try {
73+ responseEntity = restTemplate .exchange (url , HttpMethod .GET , entity , String .class );
74+ response = responseEntity .getBody ();
75+ } catch (HttpClientErrorException e ) {
76+ response = e .getMessage ();
77+ }
78+ return response ;
79+ }
80+ }
0 commit comments