浏览代码

Improve ClaimAccessor getClaimAsInstant

Fixes gh-5250
Joe Grandja 7 年之前
父节点
当前提交
fff64db0e2

+ 4 - 2
oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/ClaimAccessor.java

@@ -83,8 +83,10 @@ public interface ClaimAccessor {
 			return null;
 		}
 		Object claimValue = this.getClaims().get(claim);
-		if (Long.class.isAssignableFrom(claimValue.getClass())) {
-			return Instant.ofEpochSecond((Long) claimValue);
+		if (Long.class.isAssignableFrom(claimValue.getClass()) ||
+				Integer.class.isAssignableFrom(claimValue.getClass()) ||
+				Double.class.isAssignableFrom(claimValue.getClass())) {
+			return Instant.ofEpochSecond(((Number) claimValue).longValue());
 		}
 		if (Date.class.isAssignableFrom(claimValue.getClass())) {
 			return ((Date) claimValue).toInstant();

+ 22 - 0
oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/ClaimAccessorTests.java

@@ -70,4 +70,26 @@ public class ClaimAccessorTests {
 		assertThat(this.claimAccessor.getClaimAsInstant(claimName)).isBetween(
 				expectedClaimValue.minusSeconds(1), expectedClaimValue.plusSeconds(1));
 	}
+
+	// gh-5250
+	@Test
+	public void getClaimAsInstantWhenIntegerTypeSecondsThenReturnInstant() {
+		Instant expectedClaimValue = Instant.now();
+		String claimName = "integerSeconds";
+		this.claims.put(claimName, Long.valueOf(expectedClaimValue.getEpochSecond()).intValue());
+
+		assertThat(this.claimAccessor.getClaimAsInstant(claimName)).isBetween(
+				expectedClaimValue.minusSeconds(1), expectedClaimValue.plusSeconds(1));
+	}
+
+	// gh-5250
+	@Test
+	public void getClaimAsInstantWhenDoubleTypeSecondsThenReturnInstant() {
+		Instant expectedClaimValue = Instant.now();
+		String claimName = "doubleSeconds";
+		this.claims.put(claimName, Long.valueOf(expectedClaimValue.getEpochSecond()).doubleValue());
+
+		assertThat(this.claimAccessor.getClaimAsInstant(claimName)).isBetween(
+				expectedClaimValue.minusSeconds(1), expectedClaimValue.plusSeconds(1));
+	}
 }