39
39
import org .springframework .http .client .HttpComponentsClientHttpRequestFactory ;
40
40
import org .springframework .security .authentication .AuthenticationServiceException ;
41
41
import org .springframework .security .oauth2 .common .OAuth2AccessToken ;
42
+ import org .springframework .security .oauth2 .common .exceptions .InvalidClientException ;
43
+ import org .springframework .web .client .RestClientException ;
42
44
import org .springframework .web .client .RestTemplate ;
43
45
44
46
import com .google .common .cache .CacheBuilder ;
@@ -191,15 +193,18 @@ public RegisteredClient load(ServerConfiguration serverConfig) throws Exception
191
193
192
194
HttpEntity <String > entity = new HttpEntity <>(serializedClient , headers );
193
195
194
- String registered = restTemplate .postForObject (serverConfig .getRegistrationEndpointUri (), entity , String .class );
195
- // TODO: handle HTTP errors
196
-
197
- RegisteredClient client = ClientDetailsEntityJsonProcessor .parseRegistered (registered );
198
-
199
- // save this client for later
200
- registeredClientService .save (serverConfig .getIssuer (), client );
201
-
202
- return client ;
196
+ try {
197
+ String registered = restTemplate .postForObject (serverConfig .getRegistrationEndpointUri (), entity , String .class );
198
+
199
+ RegisteredClient client = ClientDetailsEntityJsonProcessor .parseRegistered (registered );
200
+
201
+ // save this client for later
202
+ registeredClientService .save (serverConfig .getIssuer (), client );
203
+
204
+ return client ;
205
+ } catch (RestClientException rce ) {
206
+ throw new InvalidClientException ("Error registering client with server" );
207
+ }
203
208
} else {
204
209
205
210
if (knownClient .getClientId () == null ) {
@@ -211,12 +216,16 @@ public RegisteredClient load(ServerConfiguration serverConfig) throws Exception
211
216
212
217
HttpEntity <String > entity = new HttpEntity <>(headers );
213
218
214
- String registered = restTemplate .exchange (knownClient .getRegistrationClientUri (), HttpMethod .GET , entity , String .class ).getBody ();
215
- // TODO: handle HTTP errors
216
-
217
- RegisteredClient client = ClientDetailsEntityJsonProcessor .parseRegistered (registered );
218
-
219
- return client ;
219
+ try {
220
+ String registered = restTemplate .exchange (knownClient .getRegistrationClientUri (), HttpMethod .GET , entity , String .class ).getBody ();
221
+ // TODO: handle HTTP errors
222
+
223
+ RegisteredClient client = ClientDetailsEntityJsonProcessor .parseRegistered (registered );
224
+
225
+ return client ;
226
+ } catch (RestClientException rce ) {
227
+ throw new InvalidClientException ("Error loading previously registered client information from server" );
228
+ }
220
229
} else {
221
230
// it's got a client ID from the store, don't bother trying to load it
222
231
return knownClient ;
0 commit comments