diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/UserInfoTokenServices.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/UserInfoTokenServices.java index fb7a584a78..f0798cd3f3 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/UserInfoTokenServices.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/UserInfoTokenServices.java @@ -16,6 +16,7 @@ package org.springframework.boot.autoconfigure.security.oauth2.resource; +import java.util.Collections; import java.util.List; import java.util.Map; @@ -109,16 +110,23 @@ public class UserInfoTokenServices implements ResourceServerTokenServices { @SuppressWarnings({ "unchecked" }) private Map getMap(String path, String accessToken) { this.logger.info("Getting user info from: " + path); - OAuth2RestOperations restTemplate = this.restTemplate; - if (restTemplate == null) { - BaseOAuth2ProtectedResourceDetails resource = new BaseOAuth2ProtectedResourceDetails(); - resource.setClientId(this.clientId); - restTemplate = new OAuth2RestTemplate(resource); + try { + OAuth2RestOperations restTemplate = this.restTemplate; + if (restTemplate == null) { + BaseOAuth2ProtectedResourceDetails resource = new BaseOAuth2ProtectedResourceDetails(); + resource.setClientId(this.clientId); + restTemplate = new OAuth2RestTemplate(resource); + } + DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(accessToken); + token.setTokenType(this.tokenType); + restTemplate.getOAuth2ClientContext().setAccessToken(token); + return restTemplate.getForEntity(path, Map.class).getBody(); + } + catch (Exception e) { + this.logger.info("Could not fetch user details: " + e.getClass() + ", " + + e.getMessage()); + return Collections. singletonMap("error", + "Could not fetch user details"); } - DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(accessToken); - token.setTokenType(this.tokenType); - restTemplate.getOAuth2ClientContext().setAccessToken(token); - return restTemplate.getForEntity(path, Map.class).getBody(); } - } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/resource/UserInfoTokenServicesTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/resource/UserInfoTokenServicesTests.java index 84a183ceb2..9c2fe854b0 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/resource/UserInfoTokenServicesTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/resource/UserInfoTokenServicesTests.java @@ -15,17 +15,22 @@ */ package org.springframework.boot.autoconfigure.security.oauth2.resource; +import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.oauth2.client.OAuth2ClientContext; import org.springframework.security.oauth2.client.OAuth2RestOperations; import org.springframework.security.oauth2.client.resource.BaseOAuth2ProtectedResourceDetails; +import org.springframework.security.oauth2.client.resource.UserRedirectRequiredException; import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; +import org.springframework.security.oauth2.common.exceptions.InvalidTokenException; import static org.junit.Assert.assertEquals; import static org.mockito.BDDMockito.given; @@ -39,6 +44,9 @@ import static org.mockito.Mockito.mock; */ public class UserInfoTokenServicesTests { + @Rule + public ExpectedException expected = ExpectedException.none(); + private UserInfoTokenServices services = new UserInfoTokenServices( "http://example.com", "foo"); @@ -67,6 +75,17 @@ public class UserInfoTokenServicesTests { assertEquals("unknown", this.services.loadAuthentication("FOO").getName()); } + @SuppressWarnings("unchecked") + @Test + public void badToken() { + this.services.setRestTemplate(this.template); + given(this.template.getForEntity(any(String.class), any(Class.class))).willThrow( + new UserRedirectRequiredException("foo:bar", Collections + . emptyMap())); + this.expected.expect(InvalidTokenException.class); + assertEquals("unknown", this.services.loadAuthentication("FOO").getName()); + } + @Test public void userId() { this.map.put("userid", "spencer");