-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Improve authoritiesClaimName validation in JwtGrantedAuthoritiesConverter #17247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright 2002-2022 the original author or authors. | ||
* Copyright 2002-2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
@@ -53,7 +53,9 @@ public final class JwtGrantedAuthoritiesConverter implements Converter<Jwt, Coll | |
|
||
private String authoritiesClaimDelimiter = DEFAULT_AUTHORITIES_CLAIM_DELIMITER; | ||
|
||
private String authoritiesClaimName; | ||
private Collection<String> authoritiesClaimNames = WELL_KNOWN_AUTHORITIES_CLAIM_NAMES; | ||
|
||
private boolean isExplicitlySet = false; | ||
|
||
/** | ||
* Extract {@link GrantedAuthority}s from the given {@link Jwt}. | ||
|
@@ -102,14 +104,15 @@ public void setAuthoritiesClaimDelimiter(String authoritiesClaimDelimiter) { | |
*/ | ||
public void setAuthoritiesClaimName(String authoritiesClaimName) { | ||
Assert.hasText(authoritiesClaimName, "authoritiesClaimName cannot be empty"); | ||
this.authoritiesClaimName = authoritiesClaimName; | ||
this.authoritiesClaimNames = Collections.singletonList(authoritiesClaimName); | ||
this.isExplicitlySet = true; | ||
} | ||
|
||
private String getAuthoritiesClaimName(Jwt jwt) { | ||
if (this.authoritiesClaimName != null) { | ||
return this.authoritiesClaimName; | ||
if (this.isExplicitlySet) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @jzheaux, I've implemented the refactoring as suggested, but I noticed an important behavioral difference that I wanted to discuss: Original behavior: Initial refactoring attempt: My solution:
Would you prefer this approach, or would you like to intentionally change the behavior to always validate claim names against the JWT? I wanted to confirm the intended behavior before finalizing the implementation. The changes include: Let me know your thoughts on this approach! |
||
return this.authoritiesClaimNames.iterator().next(); | ||
} | ||
for (String claimName : WELL_KNOWN_AUTHORITIES_CLAIM_NAMES) { | ||
for (String claimName : this.authoritiesClaimNames) { | ||
if (jwt.hasClaim(claimName)) { | ||
return claimName; | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.