mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-07-19 08:43:30 +00:00
ClaimAccessor.getClaimAsString() checks null claim value
Fixes gh-5608
This commit is contained in:
parent
de640a10d6
commit
1bbae835a4
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2017 the original author or authors.
|
* Copyright 2002-2018 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -53,13 +53,17 @@ public interface ClaimAccessor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the claim value as a {@code String} or {@code null} if it does not exist.
|
* Returns the claim value as a {@code String} or {@code null} if it does not exist or is equal to {@code null}.
|
||||||
*
|
*
|
||||||
* @param claim the name of the claim
|
* @param claim the name of the claim
|
||||||
* @return the claim value or {@code null} if it does not exist
|
* @return the claim value or {@code null} if it does not exist or is equal to {@code null}
|
||||||
*/
|
*/
|
||||||
default String getClaimAsString(String claim) {
|
default String getClaimAsString(String claim) {
|
||||||
return (this.containsClaim(claim) ? this.getClaims().get(claim).toString() : null);
|
if (!this.containsClaim(claim)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Object claimValue = this.getClaims().get(claim);
|
||||||
|
return (claimValue != null ? claimValue.toString() : null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -70,4 +70,13 @@ 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-5608
|
||||||
|
@Test
|
||||||
|
public void getClaimAsStringWhenValueIsNullThenReturnNull() {
|
||||||
|
String claimName = "claim-with-null-value";
|
||||||
|
this.claims.put(claimName, null);
|
||||||
|
|
||||||
|
assertThat(this.claimAccessor.getClaimAsString(claimName)).isEqualTo(null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user