1616package org .springframework .graphql .data .method .annotation .support ;
1717
1818import java .lang .annotation .Annotation ;
19+ import java .util .Map ;
1920
2021import graphql .schema .DataFetchingEnvironment ;
2122import org .reactivestreams .Publisher ;
@@ -96,7 +97,7 @@ private static AuthenticationPrincipal findMethodAnnotation(MethodParameter para
9697
9798@ Override
9899public 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) {
106107return (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