mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-05-31 01:02:14 +00:00
Improve ClaimAccessor getClaimAsInstant
Fixes gh-5250
This commit is contained in:
parent
2356749cc3
commit
fff64db0e2
@ -83,8 +83,10 @@ public interface ClaimAccessor {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
Object claimValue = this.getClaims().get(claim);
|
Object claimValue = this.getClaims().get(claim);
|
||||||
if (Long.class.isAssignableFrom(claimValue.getClass())) {
|
if (Long.class.isAssignableFrom(claimValue.getClass()) ||
|
||||||
return Instant.ofEpochSecond((Long) claimValue);
|
Integer.class.isAssignableFrom(claimValue.getClass()) ||
|
||||||
|
Double.class.isAssignableFrom(claimValue.getClass())) {
|
||||||
|
return Instant.ofEpochSecond(((Number) claimValue).longValue());
|
||||||
}
|
}
|
||||||
if (Date.class.isAssignableFrom(claimValue.getClass())) {
|
if (Date.class.isAssignableFrom(claimValue.getClass())) {
|
||||||
return ((Date) claimValue).toInstant();
|
return ((Date) claimValue).toInstant();
|
||||||
|
@ -70,4 +70,26 @@ public class ClaimAccessorTests {
|
|||||||
assertThat(this.claimAccessor.getClaimAsInstant(claimName)).isBetween(
|
assertThat(this.claimAccessor.getClaimAsInstant(claimName)).isBetween(
|
||||||
expectedClaimValue.minusSeconds(1), expectedClaimValue.plusSeconds(1));
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user