Skip to content

Commit 1f1ac3c

Browse files
committed
use single method to resolve authentication
1 parent 808de81 commit 1f1ac3c

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

spring-graphql/src/main/java/org/springframework/graphql/data/method/annotation/support/AuthenticationPrincipalArgumentResolver.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.graphql.data.method.annotation.support;
1717

1818
import java.lang.annotation.Annotation;
19+
import java.util.Map;
1920

2021
import graphql.schema.DataFetchingEnvironment;
2122
import org.reactivestreams.Publisher;
@@ -96,7 +97,7 @@ private static AuthenticationPrincipal findMethodAnnotation(MethodParameter para
9697

9798
@Override
9899
public Object resolveArgument(MethodParameter parameter, DataFetchingEnvironment environment) throws Exception {
99-
return getCurrentAuthentication()
100+
return getCurrentAuthentication(parameter.isOptional())
100101
.flatMap(auth -> Mono.justOrEmpty(resolvePrincipal(parameter, auth.getPrincipal())))
101102
.transform((argument) -> isParameterMonoAssignable(parameter) ? Mono.just(argument) : argument);
102103
}
@@ -106,9 +107,16 @@ private static boolean isParameterMonoAssignable(MethodParameter parameter) {
106107
return (Publisher.class.equals(type) || Mono.class.equals(type));
107108
}
108109

109-
private Mono<Authentication> getCurrentAuthentication() {
110-
return Mono.justOrEmpty(SecurityContextHolder.getContext().getAuthentication())
111-
.switchIfEmpty(ReactiveSecurityContextHolder.getContext().map(SecurityContext::getAuthentication));
110+
@SuppressWarnings("unchecked")
111+
private Mono<Authentication> getCurrentAuthentication(boolean optional) {
112+
Object principal = PrincipalMethodArgumentResolver.doResolve(optional);
113+
if (principal instanceof Authentication) {
114+
return Mono.just((Authentication) principal);
115+
}
116+
else if (principal instanceof Mono) {
117+
return (Mono<Authentication>) principal;
118+
}
119+
return Mono.error(new IllegalStateException("Unexpected return value: " + principal));
112120
}
113121

114122
@Nullable

0 commit comments

Comments
 (0)