Skip to content

Commit ea4d766

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

File tree

2 files changed

+4
-14
lines changed

2 files changed

+4
-14
lines changed

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

Lines changed: 1 addition & 6 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 PrincipalMethodArgumentResolver.doResolve(parameter.isOptional())
100100
.flatMap(auth -> Mono.justOrEmpty(resolvePrincipal(parameter, auth.getPrincipal())))
101101
.transform((argument) -> isParameterMonoAssignable(parameter) ? Mono.just(argument) : argument);
102102
}
@@ -106,11 +106,6 @@ 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));
112-
}
113-
114109
@Nullable
115110
private Object resolvePrincipal(MethodParameter parameter, Object principal) {
116111
AuthenticationPrincipal annotation = findMethodAnnotation(parameter);

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,9 @@ public Object resolveArgument(MethodParameter parameter, DataFetchingEnvironment
5757
return doResolve(parameter.isOptional());
5858
}
5959

60-
static Object doResolve(boolean optional) {
61-
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
62-
63-
if (authentication != null) {
64-
return authentication;
65-
}
66-
67-
return ReactiveSecurityContextHolder.getContext()
60+
static Mono<Authentication> doResolve(boolean optional) {
61+
return Mono.justOrEmpty(SecurityContextHolder.getContext())
62+
.switchIfEmpty(ReactiveSecurityContextHolder.getContext())
6863
.switchIfEmpty(optional ? Mono.empty() : Mono.error(new AuthenticationCredentialsNotFoundException("SecurityContext not available")))
6964
.handle((context, sink) -> {
7065
Authentication auth = context.getAuthentication();

0 commit comments

Comments
 (0)