Apply ROLE_ prefix when needed in ReactiveSecurityContext isUserInRole
See gh-11869
This commit is contained in:
parent
20f104766b
commit
ae45b6730b
|
@ -46,6 +46,7 @@ import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.security.core.GrantedAuthority;
|
import org.springframework.security.core.GrantedAuthority;
|
||||||
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
|
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.util.ReflectionUtils;
|
import org.springframework.util.ReflectionUtils;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
@ -389,6 +390,8 @@ public abstract class AbstractWebFluxEndpointHandlerMapping
|
||||||
|
|
||||||
private static final class ReactiveSecurityContext implements SecurityContext {
|
private static final class ReactiveSecurityContext implements SecurityContext {
|
||||||
|
|
||||||
|
private static final String ROLE_PREFIX = "ROLE_";
|
||||||
|
|
||||||
private final Authentication authentication;
|
private final Authentication authentication;
|
||||||
|
|
||||||
ReactiveSecurityContext(Authentication authentication) {
|
ReactiveSecurityContext(Authentication authentication) {
|
||||||
|
@ -402,9 +405,13 @@ public abstract class AbstractWebFluxEndpointHandlerMapping
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isUserInRole(String role) {
|
public boolean isUserInRole(String role) {
|
||||||
if (this.authentication == null || !this.authentication.isAuthenticated()) {
|
if (this.authentication == null || !this.authentication.isAuthenticated()
|
||||||
|
|| CollectionUtils.isEmpty(this.authentication.getAuthorities())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (!role.startsWith(ROLE_PREFIX)) {
|
||||||
|
role = ROLE_PREFIX + role;
|
||||||
|
}
|
||||||
for (GrantedAuthority grantedAuthority : this.authentication
|
for (GrantedAuthority grantedAuthority : this.authentication
|
||||||
.getAuthorities()) {
|
.getAuthorities()) {
|
||||||
if (role.equals(grantedAuthority.getAuthority())) {
|
if (role.equals(grantedAuthority.getAuthority())) {
|
||||||
|
|
Loading…
Reference in New Issue