mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-05-31 01:02:14 +00:00
Add ClaimAccessor#hasClaim
The new method is intended to replace ClaimAccessor#containsClaim, the return type of which was non-primitive Boolean. The existing containsClaim method is now deprecated. Closes gh-9201
This commit is contained in:
parent
050e4a98b4
commit
c002c6f9f3
@ -50,7 +50,7 @@ public interface ClaimAccessor {
|
|||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
default <T> T getClaim(String claim) {
|
default <T> T getClaim(String claim) {
|
||||||
return !containsClaim(claim) ? null : (T) getClaims().get(claim);
|
return !hasClaim(claim) ? null : (T) getClaims().get(claim);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -58,12 +58,26 @@ public interface ClaimAccessor {
|
|||||||
* {@code false}.
|
* {@code false}.
|
||||||
* @param claim the name of the claim
|
* @param claim the name of the claim
|
||||||
* @return {@code true} if the claim exists, otherwise {@code false}
|
* @return {@code true} if the claim exists, otherwise {@code false}
|
||||||
|
* @since 5.5
|
||||||
*/
|
*/
|
||||||
default Boolean containsClaim(String claim) {
|
default boolean hasClaim(String claim) {
|
||||||
Assert.notNull(claim, "claim cannot be null");
|
Assert.notNull(claim, "claim cannot be null");
|
||||||
return getClaims().containsKey(claim);
|
return getClaims().containsKey(claim);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns {@code true} if the claim exists in {@link #getClaims()}, otherwise
|
||||||
|
* {@code false}.
|
||||||
|
* @param claim the name of the claim
|
||||||
|
* @return {@code true} if the claim exists, otherwise {@code false}
|
||||||
|
* @deprecated Use
|
||||||
|
* {@link org.springframework.security.oauth2.core.ClaimAccessor#hasClaim} instead.
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
default Boolean containsClaim(String claim) {
|
||||||
|
return hasClaim(claim);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the claim value as a {@code String} or {@code null} if it does not exist or
|
* Returns the claim value as a {@code String} or {@code null} if it does not exist or
|
||||||
* is equal to {@code null}.
|
* is equal to {@code null}.
|
||||||
@ -72,7 +86,7 @@ public interface ClaimAccessor {
|
|||||||
* {@code null}
|
* {@code null}
|
||||||
*/
|
*/
|
||||||
default String getClaimAsString(String claim) {
|
default String getClaimAsString(String claim) {
|
||||||
return !containsClaim(claim) ? null
|
return !hasClaim(claim) ? null
|
||||||
: ClaimConversionService.getSharedInstance().convert(getClaims().get(claim), String.class);
|
: ClaimConversionService.getSharedInstance().convert(getClaims().get(claim), String.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,7 +96,7 @@ public interface ClaimAccessor {
|
|||||||
* @return the claim value or {@code null} if it does not exist
|
* @return the claim value or {@code null} if it does not exist
|
||||||
*/
|
*/
|
||||||
default Boolean getClaimAsBoolean(String claim) {
|
default Boolean getClaimAsBoolean(String claim) {
|
||||||
return !containsClaim(claim) ? null
|
return !hasClaim(claim) ? null
|
||||||
: ClaimConversionService.getSharedInstance().convert(getClaims().get(claim), Boolean.class);
|
: ClaimConversionService.getSharedInstance().convert(getClaims().get(claim), Boolean.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,7 +106,7 @@ public interface ClaimAccessor {
|
|||||||
* @return the claim value or {@code null} if it does not exist
|
* @return the claim value or {@code null} if it does not exist
|
||||||
*/
|
*/
|
||||||
default Instant getClaimAsInstant(String claim) {
|
default Instant getClaimAsInstant(String claim) {
|
||||||
if (!containsClaim(claim)) {
|
if (!hasClaim(claim)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
Object claimValue = getClaims().get(claim);
|
Object claimValue = getClaims().get(claim);
|
||||||
@ -108,7 +122,7 @@ public interface ClaimAccessor {
|
|||||||
* @return the claim value or {@code null} if it does not exist
|
* @return the claim value or {@code null} if it does not exist
|
||||||
*/
|
*/
|
||||||
default URL getClaimAsURL(String claim) {
|
default URL getClaimAsURL(String claim) {
|
||||||
if (!containsClaim(claim)) {
|
if (!hasClaim(claim)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
Object claimValue = getClaims().get(claim);
|
Object claimValue = getClaims().get(claim);
|
||||||
@ -127,7 +141,7 @@ public interface ClaimAccessor {
|
|||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
default Map<String, Object> getClaimAsMap(String claim) {
|
default Map<String, Object> getClaimAsMap(String claim) {
|
||||||
if (!containsClaim(claim)) {
|
if (!hasClaim(claim)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
final TypeDescriptor sourceDescriptor = TypeDescriptor.valueOf(Object.class);
|
final TypeDescriptor sourceDescriptor = TypeDescriptor.valueOf(Object.class);
|
||||||
@ -150,7 +164,7 @@ public interface ClaimAccessor {
|
|||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
default List<String> getClaimAsStringList(String claim) {
|
default List<String> getClaimAsStringList(String claim) {
|
||||||
if (!containsClaim(claim)) {
|
if (!hasClaim(claim)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
final TypeDescriptor sourceDescriptor = TypeDescriptor.valueOf(Object.class);
|
final TypeDescriptor sourceDescriptor = TypeDescriptor.valueOf(Object.class);
|
||||||
|
@ -261,7 +261,7 @@ public class NimbusJwtDecoderTests {
|
|||||||
public void decodeWhenSignedThenOk() {
|
public void decodeWhenSignedThenOk() {
|
||||||
NimbusJwtDecoder jwtDecoder = new NimbusJwtDecoder(withSigning(JWK_SET));
|
NimbusJwtDecoder jwtDecoder = new NimbusJwtDecoder(withSigning(JWK_SET));
|
||||||
Jwt jwt = jwtDecoder.decode(SIGNED_JWT);
|
Jwt jwt = jwtDecoder.decode(SIGNED_JWT);
|
||||||
assertThat(jwt.containsClaim(JwtClaimNames.EXP)).isNotNull();
|
assertThat(jwt.hasClaim(JwtClaimNames.EXP)).isNotNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -435,7 +435,7 @@ public class NimbusJwtDecoderTests {
|
|||||||
)
|
)
|
||||||
.build();
|
.build();
|
||||||
// @formatter:on
|
// @formatter:on
|
||||||
assertThat(decoder.decode(signedJwt.serialize()).containsClaim(JwtClaimNames.EXP)).isNotNull();
|
assertThat(decoder.decode(signedJwt.serialize()).hasClaim(JwtClaimNames.EXP)).isNotNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -553,7 +553,7 @@ public class NimbusJwtDecoderTests {
|
|||||||
)
|
)
|
||||||
.build();
|
.build();
|
||||||
// @formatter:on
|
// @formatter:on
|
||||||
assertThat(decoder.decode(signedJwt.serialize()).containsClaim(JwtClaimNames.EXP)).isNotNull();
|
assertThat(decoder.decode(signedJwt.serialize()).hasClaim(JwtClaimNames.EXP)).isNotNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -94,7 +94,7 @@ public final class JwtGrantedAuthoritiesConverter implements Converter<Jwt, Coll
|
|||||||
return this.authoritiesClaimName;
|
return this.authoritiesClaimName;
|
||||||
}
|
}
|
||||||
for (String claimName : WELL_KNOWN_AUTHORITIES_CLAIM_NAMES) {
|
for (String claimName : WELL_KNOWN_AUTHORITIES_CLAIM_NAMES) {
|
||||||
if (jwt.containsClaim(claimName)) {
|
if (jwt.hasClaim(claimName)) {
|
||||||
return claimName;
|
return claimName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user