Skip to content

Commit cb7b8dc

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

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ private static AuthenticationPrincipal findMethodAnnotation(MethodParameter para
9696

9797
@Override
9898
public Object resolveArgument(MethodParameter parameter, DataFetchingEnvironment environment) throws Exception {
99-
return getCurrentAuthentication()
99+
return getCurrentAuthentication(parameter.isOptional())
100100
.flatMap(auth -> Mono.justOrEmpty(resolvePrincipal(parameter, auth.getPrincipal())))
101101
.transform((argument) -> isParameterMonoAssignable(parameter) ? Mono.just(argument) : argument);
102102
}
@@ -106,9 +106,16 @@ private static boolean isParameterMonoAssignable(MethodParameter parameter) {
106106
return (Publisher.class.equals(type) || Mono.class.equals(type));
107107
}
108108

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

114121
@Nullable

0 commit comments

Comments
 (0)