mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-11-10 19:48:50 +00:00
Mark GrantedAuthority#getAuthority as @Nullable
Closes: gh-17999 Signed-off-by: Andrey Litvitski <andrey1010102008@gmail.com>
This commit is contained in:
parent
eb43830260
commit
9b61533db2
@ -99,7 +99,7 @@ public final class AllRequiredFactorsAuthorizationManager<T> implements Authoriz
|
|||||||
private @Nullable RequiredFactorError requiredFactorError(RequiredFactor requiredFactor,
|
private @Nullable RequiredFactorError requiredFactorError(RequiredFactor requiredFactor,
|
||||||
List<GrantedAuthority> currentFactors) {
|
List<GrantedAuthority> currentFactors) {
|
||||||
Optional<GrantedAuthority> matchingAuthority = currentFactors.stream()
|
Optional<GrantedAuthority> matchingAuthority = currentFactors.stream()
|
||||||
.filter((authority) -> authority.getAuthority().equals(requiredFactor.getAuthority()))
|
.filter((authority) -> Objects.equals(authority.getAuthority(), requiredFactor.getAuthority()))
|
||||||
.findFirst();
|
.findFirst();
|
||||||
if (!matchingAuthority.isPresent()) {
|
if (!matchingAuthority.isPresent()) {
|
||||||
return RequiredFactorError.createMissing(requiredFactor);
|
return RequiredFactorError.createMissing(requiredFactor);
|
||||||
|
|||||||
@ -17,6 +17,7 @@
|
|||||||
package org.springframework.security.authorization;
|
package org.springframework.security.authorization;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
@ -47,8 +48,8 @@ public class AuthorityReactiveAuthorizationManager<T> implements ReactiveAuthori
|
|||||||
// @formatter:off
|
// @formatter:off
|
||||||
return authentication.filter(Authentication::isAuthenticated)
|
return authentication.filter(Authentication::isAuthenticated)
|
||||||
.flatMapIterable(Authentication::getAuthorities)
|
.flatMapIterable(Authentication::getAuthorities)
|
||||||
.map(GrantedAuthority::getAuthority)
|
.mapNotNull(GrantedAuthority::getAuthority)
|
||||||
.any((grantedAuthority) -> this.authorities.stream().anyMatch((authority) -> authority.getAuthority().equals(grantedAuthority)))
|
.any((grantedAuthority) -> this.authorities.stream().anyMatch((authority) -> Objects.equals(authority.getAuthority(), grantedAuthority)))
|
||||||
.map((granted) -> ((AuthorizationResult) new AuthorityAuthorizationDecision(granted, this.authorities)))
|
.map((granted) -> ((AuthorizationResult) new AuthorityAuthorizationDecision(granted, this.authorities)))
|
||||||
.defaultIfEmpty(new AuthorityAuthorizationDecision(false, this.authorities));
|
.defaultIfEmpty(new AuthorityAuthorizationDecision(false, this.authorities));
|
||||||
// @formatter:on
|
// @formatter:on
|
||||||
|
|||||||
@ -18,6 +18,8 @@ package org.springframework.security.core;
|
|||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
import org.jspecify.annotations.Nullable;
|
||||||
|
|
||||||
import org.springframework.security.authorization.AuthorizationManager;
|
import org.springframework.security.authorization.AuthorizationManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -46,6 +48,6 @@ public interface GrantedAuthority extends Serializable {
|
|||||||
* granted authority cannot be expressed as a <code>String</code> with sufficient
|
* granted authority cannot be expressed as a <code>String</code> with sufficient
|
||||||
* precision).
|
* precision).
|
||||||
*/
|
*/
|
||||||
String getAuthority();
|
@Nullable String getAuthority();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -64,7 +64,10 @@ public final class SimpleAuthorityMapper implements GrantedAuthoritiesMapper, In
|
|||||||
public Set<GrantedAuthority> mapAuthorities(Collection<? extends GrantedAuthority> authorities) {
|
public Set<GrantedAuthority> mapAuthorities(Collection<? extends GrantedAuthority> authorities) {
|
||||||
HashSet<GrantedAuthority> mapped = new HashSet<>(authorities.size());
|
HashSet<GrantedAuthority> mapped = new HashSet<>(authorities.size());
|
||||||
for (GrantedAuthority authority : authorities) {
|
for (GrantedAuthority authority : authorities) {
|
||||||
mapped.add(mapAuthority(authority.getAuthority()));
|
String authorityStr = authority.getAuthority();
|
||||||
|
if (authorityStr != null) {
|
||||||
|
mapped.add(mapAuthority(authorityStr));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (this.defaultAuthority != null) {
|
if (this.defaultAuthority != null) {
|
||||||
mapped.add(this.defaultAuthority);
|
mapped.add(this.defaultAuthority);
|
||||||
|
|||||||
@ -281,7 +281,8 @@ public final class SecurityMockMvcResultMatchers {
|
|||||||
for (String role : roles) {
|
for (String role : roles) {
|
||||||
withPrefix.add(new SimpleGrantedAuthority(rolePrefix + role));
|
withPrefix.add(new SimpleGrantedAuthority(rolePrefix + role));
|
||||||
}
|
}
|
||||||
this.ignoreAuthorities = (authority) -> !authority.getAuthority().startsWith(rolePrefix);
|
this.ignoreAuthorities = (authority) -> (authority.getAuthority() != null
|
||||||
|
&& !authority.getAuthority().startsWith(rolePrefix));
|
||||||
return withAuthorities(withPrefix);
|
return withAuthorities(withPrefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -162,7 +162,8 @@ public final class DelegatingMissingAuthorityAccessDeniedHandler implements Acce
|
|||||||
if (authorizationResult instanceof AuthorityAuthorizationDecision authorityDecision) {
|
if (authorizationResult instanceof AuthorityAuthorizationDecision authorityDecision) {
|
||||||
// @formatter:off
|
// @formatter:off
|
||||||
return authorityDecision.getAuthorities().stream()
|
return authorityDecision.getAuthorities().stream()
|
||||||
.map((grantedAuthority) -> {
|
.filter((ga) -> ga.getAuthority() != null)
|
||||||
|
.map((grantedAuthority) -> {
|
||||||
String authority = grantedAuthority.getAuthority();
|
String authority = grantedAuthority.getAuthority();
|
||||||
if (authority.startsWith("FACTOR_")) {
|
if (authority.startsWith("FACTOR_")) {
|
||||||
RequiredFactor required = RequiredFactor.withAuthority(authority).build();
|
RequiredFactor required = RequiredFactor.withAuthority(authority).build();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user