Add AuthenticationPrincipalArgumentResolver
Closes gh-272
This commit is contained in:
@@ -36,6 +36,8 @@ import graphql.schema.idl.RuntimeWiring;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.dataloader.DataLoader;
|
||||
import org.springframework.context.expression.BeanFactoryResolver;
|
||||
import org.springframework.expression.BeanResolver;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@@ -166,6 +168,8 @@ public class AnnotatedControllerConfigurer
|
||||
this.argumentResolvers.addResolver(new DataLoaderMethodArgumentResolver());
|
||||
if (springSecurityPresent) {
|
||||
this.argumentResolvers.addResolver(new PrincipalMethodArgumentResolver());
|
||||
BeanResolver beanResolver = new BeanFactoryResolver(obtainApplicationContext());
|
||||
this.argumentResolvers.addResolver(new AuthenticationPrincipalArgumentResolver(beanResolver));
|
||||
}
|
||||
if (KotlinDetector.isKotlinPresent()) {
|
||||
this.argumentResolvers.addResolver(new ContinuationHandlerMethodArgumentResolver());
|
||||
|
||||
@@ -0,0 +1,153 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.graphql.data.method.annotation.support;
|
||||
|
||||
import graphql.schema.DataFetchingEnvironment;
|
||||
import org.reactivestreams.Publisher;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.expression.BeanResolver;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.ExpressionParser;
|
||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
import org.springframework.graphql.data.method.HandlerMethodArgumentResolver;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
|
||||
import org.springframework.security.core.context.SecurityContext;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
|
||||
/**
|
||||
* Resolver to obtain {@link Authentication#getPrincipal()} from Spring Security context via
|
||||
* {@link SecurityContext#getAuthentication()} for parameters annotated with {@link AuthenticationPrincipal}.
|
||||
*
|
||||
* <p>The resolver checks both ThreadLocal context via {@link SecurityContextHolder}
|
||||
* for Spring MVC applications, and {@link ReactiveSecurityContextHolder} for
|
||||
* Spring WebFlux applications.
|
||||
*
|
||||
* @author Rob Winch
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class AuthenticationPrincipalArgumentResolver implements HandlerMethodArgumentResolver {
|
||||
|
||||
private ExpressionParser parser = new SpelExpressionParser();
|
||||
|
||||
private final BeanResolver beanResolver;
|
||||
|
||||
/**
|
||||
* Creates a new instance.
|
||||
* @param beanResolver the {@link BeanResolver} used for resolving beans in SpEL expressions. Cannot be null.
|
||||
*/
|
||||
public AuthenticationPrincipalArgumentResolver(BeanResolver beanResolver) {
|
||||
Assert.notNull(beanResolver, "BeanResolver is required");
|
||||
this.beanResolver = beanResolver;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsParameter(MethodParameter parameter) {
|
||||
return findMethodAnnotation(AuthenticationPrincipal.class, parameter) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object resolveArgument(MethodParameter parameter, DataFetchingEnvironment environment) throws Exception {
|
||||
return getCurrentAuthentication().map(Authentication::getPrincipal)
|
||||
.flatMap((principal) -> Mono.justOrEmpty(resolvePrincipal(parameter, principal)))
|
||||
.transform((argument) -> {
|
||||
boolean isParameterPublisher = isParameterMonoAssignable(parameter);
|
||||
return isParameterPublisher ? Mono.just(argument) : argument;
|
||||
});
|
||||
}
|
||||
|
||||
private Mono<Authentication> getCurrentAuthentication() {
|
||||
SecurityContext securityContext = SecurityContextHolder.getContext();
|
||||
return Mono.justOrEmpty(securityContext.getAuthentication())
|
||||
.switchIfEmpty(ReactiveSecurityContextHolder.getContext().map(SecurityContext::getAuthentication));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private Object resolvePrincipal(MethodParameter parameter, Object principal) {
|
||||
AuthenticationPrincipal annotation = findMethodAnnotation(AuthenticationPrincipal.class, parameter);
|
||||
String expressionToParse = annotation.expression();
|
||||
if (StringUtils.hasLength(expressionToParse)) {
|
||||
StandardEvaluationContext context = new StandardEvaluationContext();
|
||||
context.setRootObject(principal);
|
||||
context.setVariable("this", principal);
|
||||
context.setBeanResolver(this.beanResolver);
|
||||
Expression expression = this.parser.parseExpression(expressionToParse);
|
||||
principal = expression.getValue(context);
|
||||
}
|
||||
if (isInvalidType(parameter, principal)) {
|
||||
if (annotation.errorOnInvalidType()) {
|
||||
throw new ClassCastException(principal + " is not assignable to " + parameter.getParameterType());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return principal;
|
||||
}
|
||||
|
||||
private boolean isInvalidType(MethodParameter parameter, @Nullable Object principal) {
|
||||
if (principal == null) {
|
||||
return false;
|
||||
}
|
||||
Class<?> typeToCheck = parameter.getParameterType();
|
||||
if (isParameterMonoAssignable(parameter)) {
|
||||
ResolvableType resolvableType = ResolvableType.forMethodParameter(parameter);
|
||||
Class<?> genericType = resolvableType.resolveGeneric(0);
|
||||
if (genericType == null) {
|
||||
return false;
|
||||
}
|
||||
typeToCheck = genericType;
|
||||
}
|
||||
return !ClassUtils.isAssignable(typeToCheck, principal.getClass());
|
||||
}
|
||||
|
||||
private boolean isParameterMonoAssignable(MethodParameter parameter) {
|
||||
return Publisher.class.equals(parameter.getParameterType()) ||
|
||||
Mono.class.equals(parameter.getParameterType());
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains the specified {@link Annotation} on the specified {@link MethodParameter}.
|
||||
* @param annotationClass the class of the {@link Annotation} to find on the
|
||||
* {@link MethodParameter}
|
||||
* @param parameter the {@link MethodParameter} to search for an {@link Annotation}
|
||||
* @return the {@link Annotation} that was found or null.
|
||||
*/
|
||||
@Nullable
|
||||
private <T extends Annotation> T findMethodAnnotation(Class<T> annotationClass, MethodParameter parameter) {
|
||||
T annotation = parameter.getParameterAnnotation(annotationClass);
|
||||
if (annotation != null) {
|
||||
return annotation;
|
||||
}
|
||||
Annotation[] annotationsToSearch = parameter.getParameterAnnotations();
|
||||
for (Annotation toSearch : annotationsToSearch) {
|
||||
annotation = AnnotationUtils.findAnnotation(toSearch.annotationType(), annotationClass);
|
||||
if (annotation != null) {
|
||||
return annotation;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,405 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.graphql.data.method.annotation.support;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.reactivestreams.Publisher;
|
||||
import org.springframework.core.DefaultParameterNameDiscoverer;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.annotation.SynthesizingMethodParameter;
|
||||
import org.springframework.graphql.data.method.annotation.Argument;
|
||||
import org.springframework.graphql.data.method.annotation.QueryMapping;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||
import org.springframework.security.core.authority.AuthorityUtils;
|
||||
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
|
||||
import org.springframework.security.core.context.SecurityContext;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.publisher.TestPublisher;
|
||||
import reactor.util.context.Context;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link AuthenticationPrincipalArgumentResolver}.
|
||||
*
|
||||
* @author Rob Winch
|
||||
*/
|
||||
class AuthenticationPrincipalArgumentResolverTests {
|
||||
|
||||
private final static Class<?> MONO_USER_DETAILS_CLASS = ResolvableType.forClassWithGenerics(Mono.class, UserDetails.class).getRawClass();
|
||||
|
||||
private final static Class<UserDetails> USER_DETAILS_CLASS = UserDetails.class;
|
||||
|
||||
private final static Class<String> STRING_CLASS = String.class;
|
||||
|
||||
private final static Class<?> MONO_STRING_CLASS = ResolvableType.forClassWithGenerics(Mono.class, String.class).getRawClass();
|
||||
|
||||
private final static Class<?> PUBLISHER_USER_DETAILS_CLASS = ResolvableType.forClassWithGenerics(Publisher.class, UserDetails.class).getRawClass();
|
||||
|
||||
private final static Class<?> TESTPUBLISHER_USER_DETAILS_CLASS = ResolvableType.forClassWithGenerics(TestPublisher.class, UserDetails.class).getRawClass();
|
||||
|
||||
private AuthenticationPrincipalArgumentResolver resolver;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
this.resolver = new AuthenticationPrincipalArgumentResolver((beanName, context) -> new PrincipalConverter());
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void cleanup() {
|
||||
SecurityContextHolder.clearContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
void supportsParameterWhenNoAnnotation() throws Exception {
|
||||
MethodParameter methodParameter = firstMethodParameter(UserController.class, "noParameter", USER_DETAILS_CLASS);
|
||||
assertThat(this.resolver.supportsParameter(methodParameter)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void supportsParameterWhenWrongAnnotation() throws Exception {
|
||||
MethodParameter methodParameter = firstMethodParameter(UserController.class, "argument", USER_DETAILS_CLASS);
|
||||
assertThat(this.resolver.supportsParameter(methodParameter)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void supportsParameterWhenCurrentUser() throws Exception {
|
||||
MethodParameter methodParameter = firstMethodParameter(UserController.class, "currentUser", USER_DETAILS_CLASS);
|
||||
assertThat(this.resolver.supportsParameter(methodParameter)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void supportsParameterWhenAuthenticationPrincipal() throws Exception {
|
||||
MethodParameter methodParameter = firstMethodParameter(UserController.class, "userDetails", USER_DETAILS_CLASS);
|
||||
assertThat(this.resolver.supportsParameter(methodParameter)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveArgumentWhenAuthenticationPrincipal() throws Exception {
|
||||
MethodParameter methodParameter = firstMethodParameter(UserController.class, "userDetails", USER_DETAILS_CLASS);
|
||||
Mono<UserDetails> userDetails = (Mono<UserDetails>) this.resolver.resolveArgument(methodParameter, null);
|
||||
assertThat(userDetails.contextWrite(authenticationContext()).block().getUsername()).isEqualTo("user");
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveArgumentWhenCurrentUser() throws Exception {
|
||||
MethodParameter methodParameter = firstMethodParameter(UserController.class, "currentUser", USER_DETAILS_CLASS);
|
||||
Mono<UserDetails> userDetails = (Mono<UserDetails>) this.resolver.resolveArgument(methodParameter, null);
|
||||
assertThat(userDetails.contextWrite(authenticationContext()).block().getUsername()).isEqualTo("user");
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveArgumentWhenNoSecurityContext() throws Exception {
|
||||
MethodParameter methodParameter = firstMethodParameter(UserController.class, "currentUser", USER_DETAILS_CLASS);
|
||||
Mono<UserDetails> userDetails = (Mono<UserDetails>) this.resolver.resolveArgument(methodParameter, null);
|
||||
assertThat(userDetails.block()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveArgumentWhenSecurityContextHolder() throws Exception {
|
||||
SecurityContext context = SecurityContextHolder.createEmptyContext();
|
||||
context.setAuthentication(usernamePasswordAuthentication());
|
||||
SecurityContextHolder.setContext(context);
|
||||
MethodParameter methodParameter = firstMethodParameter(UserController.class, "currentUser", USER_DETAILS_CLASS);
|
||||
Mono<UserDetails> userDetails = (Mono<UserDetails>) this.resolver.resolveArgument(methodParameter, null);
|
||||
assertThat(userDetails.contextWrite(authenticationContext()).block().getUsername()).isEqualTo("user");
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveArgumentWhenAuthenticationPrincipalUsername() throws Exception {
|
||||
MethodParameter methodParameter = firstMethodParameter(UserController.class, "username", STRING_CLASS);
|
||||
Mono<String> userDetails = (Mono<String>) this.resolver.resolveArgument(methodParameter, null);
|
||||
assertThat(userDetails.contextWrite(authenticationContext()).block()).isEqualTo("user");
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveArgumentWhenBeanName() throws Exception {
|
||||
MethodParameter methodParameter = firstMethodParameter(UserController.class, "beanName", USER_DETAILS_CLASS);
|
||||
Mono<UserDetails> userDetails = (Mono<UserDetails>) this.resolver.resolveArgument(methodParameter, null);
|
||||
assertThat(userDetails.contextWrite(authenticationContext()).block().getUsername()).isEqualTo("user");
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveArgumentWhenErrorOnInvalidType() throws Exception {
|
||||
MethodParameter methodParameter = firstMethodParameter(UserController.class, "errorOnInvalidType", String.class);
|
||||
Mono<Object> userDetails = (Mono<Object>) this.resolver.resolveArgument(methodParameter, null);
|
||||
assertThatExceptionOfType(ClassCastException.class)
|
||||
.isThrownBy(() -> userDetails.contextWrite(authenticationContext()).block());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveArgumentWhenInvalidType() throws Exception {
|
||||
MethodParameter methodParameter = firstMethodParameter(UserController.class, "invalidType", STRING_CLASS);
|
||||
Mono<Mono<Object>> userDetails = (Mono<Mono<Object>>) this.resolver.resolveArgument(methodParameter, null);
|
||||
assertThat(userDetails.flatMap(u -> u).contextWrite(authenticationContext()).block()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void supportsParameterWhenMonoNoAnnotation() throws Exception {
|
||||
MethodParameter methodParameter = firstMethodParameter(UserController.class, "noParameter", MONO_USER_DETAILS_CLASS);
|
||||
assertThat(this.resolver.supportsParameter(methodParameter)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void supportsParameterWhenMonoWrongAnnotation() throws Exception {
|
||||
MethodParameter methodParameter = firstMethodParameter(UserController.class, "argument", MONO_USER_DETAILS_CLASS);
|
||||
assertThat(this.resolver.supportsParameter(methodParameter)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void supportsParameterWhenMonoCurrentUser() throws Exception {
|
||||
MethodParameter methodParameter = firstMethodParameter(UserController.class, "currentUser", MONO_USER_DETAILS_CLASS);
|
||||
assertThat(this.resolver.supportsParameter(methodParameter)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void supportsParameterWhenMonoAuthenticationPrincipal() throws Exception {
|
||||
MethodParameter methodParameter = firstMethodParameter(UserController.class, "userDetails", MONO_USER_DETAILS_CLASS);
|
||||
assertThat(this.resolver.supportsParameter(methodParameter)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveArgumentWhenMonoAuthenticationPrincipal() throws Exception {
|
||||
MethodParameter methodParameter = firstMethodParameter(UserController.class, "userDetails", MONO_USER_DETAILS_CLASS);
|
||||
Mono<Mono<UserDetails>> userDetails = (Mono<Mono<UserDetails>>) this.resolver.resolveArgument(methodParameter, null);
|
||||
assertThat(userDetails.block().contextWrite(authenticationContext()).block().getUsername()).isEqualTo("user");
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveArgumentWhenMonoCurrentUser() throws Exception {
|
||||
MethodParameter methodParameter = firstMethodParameter(UserController.class, "currentUser", MONO_USER_DETAILS_CLASS);
|
||||
Mono<Mono<UserDetails>> userDetails = (Mono<Mono<UserDetails>>) this.resolver.resolveArgument(methodParameter, null);
|
||||
assertThat(userDetails.block().contextWrite(authenticationContext()).block().getUsername()).isEqualTo("user");
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveArgumentWhenMonoNoSecurityContext() throws Exception {
|
||||
MethodParameter methodParameter = firstMethodParameter(UserController.class, "currentUser", MONO_USER_DETAILS_CLASS);
|
||||
Mono<Mono<UserDetails>> userDetails = (Mono<Mono<UserDetails>>) this.resolver.resolveArgument(methodParameter, null);
|
||||
assertThat(userDetails.block().block()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveArgumentWhenMonoSecurityContextHolder() throws Exception {
|
||||
SecurityContext context = SecurityContextHolder.createEmptyContext();
|
||||
context.setAuthentication(usernamePasswordAuthentication());
|
||||
SecurityContextHolder.setContext(context);
|
||||
MethodParameter methodParameter = firstMethodParameter(UserController.class, "currentUser", MONO_USER_DETAILS_CLASS);
|
||||
Mono<Mono<UserDetails>> userDetails = (Mono<Mono<UserDetails>>) this.resolver.resolveArgument(methodParameter, null);
|
||||
assertThat(userDetails.block().contextWrite(authenticationContext()).block().getUsername()).isEqualTo("user");
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveArgumentWhenMonoAuthenticationPrincipalUsername() throws Exception {
|
||||
MethodParameter methodParameter = firstMethodParameter(UserController.class, "username", MONO_STRING_CLASS);
|
||||
Mono<Mono<String>> userDetails = (Mono<Mono<String>>) this.resolver.resolveArgument(methodParameter, null);
|
||||
assertThat(userDetails.block().contextWrite(authenticationContext()).block()).isEqualTo("user");
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveArgumentWhenMonoBeanName() throws Exception {
|
||||
MethodParameter methodParameter = firstMethodParameter(UserController.class, "beanName", MONO_USER_DETAILS_CLASS);
|
||||
Mono<Mono<UserDetails>> userDetails = (Mono<Mono<UserDetails>>) this.resolver.resolveArgument(methodParameter, null);
|
||||
assertThat(userDetails.flatMap(u -> u).contextWrite(authenticationContext()).block().getUsername()).isEqualTo("user");
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveArgumentWhenMonoErrorOnInvalidType() throws Exception {
|
||||
MethodParameter methodParameter = firstMethodParameter(UserController.class, "errorOnInvalidType", MONO_STRING_CLASS);
|
||||
Mono<Mono<Object>> userDetails = (Mono<Mono<Object>>) this.resolver.resolveArgument(methodParameter, null);
|
||||
assertThatExceptionOfType(ClassCastException.class)
|
||||
.isThrownBy(() -> userDetails.flatMap(u -> u).contextWrite(authenticationContext()).block());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveArgumentWhenMonoInvalidType() throws Exception {
|
||||
MethodParameter methodParameter = firstMethodParameter(UserController.class, "invalidType", MONO_STRING_CLASS);
|
||||
Mono<Mono<Object>> userDetails = (Mono<Mono<Object>>) this.resolver.resolveArgument(methodParameter, null);
|
||||
assertThat(userDetails.flatMap(u -> u).contextWrite(authenticationContext()).block()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveArgumentWhenPublisher() throws Exception {
|
||||
MethodParameter methodParameter = firstMethodParameter(UserController.class, "publisher", PUBLISHER_USER_DETAILS_CLASS);
|
||||
Mono<Mono<UserDetails>> userDetails = (Mono<Mono<UserDetails>>) this.resolver.resolveArgument(methodParameter, null);
|
||||
assertThat(userDetails.block().contextWrite(authenticationContext()).block().getUsername()).isEqualTo("user");
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveArgumentWhenTestPublisherThenEmptyMono() throws Exception {
|
||||
MethodParameter methodParameter = firstMethodParameter(UserController.class, "publisher", TESTPUBLISHER_USER_DETAILS_CLASS);
|
||||
Mono<TestPublisher<UserDetails>> userDetails = (Mono<TestPublisher<UserDetails>>) this.resolver.resolveArgument(methodParameter, null);
|
||||
assertThat(userDetails.contextWrite(authenticationContext()).block()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveArgumentWhenTestPublisherAndErrorOnInvalidType() throws Exception {
|
||||
MethodParameter methodParameter = firstMethodParameter(UserController.class, "errorOnInvalidType", TESTPUBLISHER_USER_DETAILS_CLASS);
|
||||
Mono<Mono<UserDetails>> userDetails = (Mono<Mono<UserDetails>>) this.resolver.resolveArgument(methodParameter, null);
|
||||
assertThatExceptionOfType(ClassCastException.class)
|
||||
.isThrownBy(() -> userDetails.flatMap(u -> u).contextWrite(authenticationContext()).block());
|
||||
}
|
||||
|
||||
private MethodParameter firstMethodParameter(Class<?> clazz, String methodName, Class<?>... paramTypes) {
|
||||
Method method = ClassUtils.getMethod(clazz, methodName, paramTypes);
|
||||
return methodParam(method, 0);
|
||||
}
|
||||
|
||||
private MethodParameter methodParam(Method method, int index) {
|
||||
MethodParameter methodParameter = new SynthesizingMethodParameter(method, index);
|
||||
methodParameter.initParameterNameDiscovery(new DefaultParameterNameDiscoverer());
|
||||
return methodParameter;
|
||||
}
|
||||
|
||||
private static Function<Context, Context> authenticationContext() {
|
||||
return (context) -> ReactiveSecurityContextHolder.withAuthentication(usernamePasswordAuthentication());
|
||||
}
|
||||
|
||||
private static Authentication usernamePasswordAuthentication() {
|
||||
UserDetails userDetails = userDetails();
|
||||
return new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(), userDetails.getAuthorities());
|
||||
}
|
||||
|
||||
private static UserDetails userDetails() {
|
||||
return new User("user", "password", AuthorityUtils.createAuthorityList("ROLE_USER"));
|
||||
}
|
||||
|
||||
static class PrincipalConverter {
|
||||
public UserDetails convert(UserDetails userDetails) {
|
||||
return userDetails;
|
||||
}
|
||||
}
|
||||
|
||||
@Controller
|
||||
static class UserController {
|
||||
|
||||
@QueryMapping
|
||||
public UserDetails noParameter(UserDetails userDetails) {
|
||||
return userDetails;
|
||||
}
|
||||
|
||||
@QueryMapping
|
||||
public UserDetails argument(@Argument UserDetails userDetails) {
|
||||
return userDetails;
|
||||
}
|
||||
|
||||
@QueryMapping
|
||||
public UserDetails userDetails(@AuthenticationPrincipal UserDetails userDetails) {
|
||||
return userDetails;
|
||||
}
|
||||
|
||||
@QueryMapping
|
||||
public UserDetails currentUser(@CurrentUser UserDetails userDetails) {
|
||||
return userDetails;
|
||||
}
|
||||
|
||||
@QueryMapping
|
||||
public String username(@AuthenticationPrincipal(expression = "username") String username) {
|
||||
return username;
|
||||
}
|
||||
|
||||
@QueryMapping
|
||||
public UserDetails beanName(@AuthenticationPrincipal(expression = "@bean.convert(#this)") UserDetails userDetails) {
|
||||
return userDetails;
|
||||
}
|
||||
|
||||
@QueryMapping
|
||||
public String errorOnInvalidType(@AuthenticationPrincipal(errorOnInvalidType = true) String userDetails) {
|
||||
return userDetails;
|
||||
}
|
||||
|
||||
@QueryMapping
|
||||
public String invalidType(@AuthenticationPrincipal String userDetails) {
|
||||
return userDetails;
|
||||
}
|
||||
|
||||
@QueryMapping
|
||||
public Mono<UserDetails> noParameter(Mono<UserDetails> userDetails) {
|
||||
return userDetails;
|
||||
}
|
||||
|
||||
@QueryMapping
|
||||
public Mono<UserDetails> argument(@Argument Mono<UserDetails> userDetails) {
|
||||
return userDetails;
|
||||
}
|
||||
|
||||
@QueryMapping
|
||||
public Mono<UserDetails> userDetails(@AuthenticationPrincipal Mono<UserDetails> userDetails) {
|
||||
return userDetails;
|
||||
}
|
||||
|
||||
@QueryMapping
|
||||
public Mono<UserDetails> currentUser(@CurrentUser Mono<UserDetails> userDetails) {
|
||||
return userDetails;
|
||||
}
|
||||
|
||||
@QueryMapping
|
||||
public Mono<String> username(@AuthenticationPrincipal(expression = "username") Mono<String> username) {
|
||||
return username;
|
||||
}
|
||||
|
||||
@QueryMapping
|
||||
public Mono<UserDetails> beanName(@AuthenticationPrincipal(expression = "@bean.convert(#this)") Mono<UserDetails> userDetails) {
|
||||
return userDetails;
|
||||
}
|
||||
|
||||
@QueryMapping
|
||||
public Mono<String> errorOnInvalidType(@AuthenticationPrincipal(errorOnInvalidType = true) Mono<String> userDetails) {
|
||||
return userDetails;
|
||||
}
|
||||
|
||||
@QueryMapping
|
||||
public Publisher<UserDetails> errorOnInvalidType(@AuthenticationPrincipal(errorOnInvalidType = true) TestPublisher<UserDetails> userDetails) {
|
||||
return userDetails;
|
||||
}
|
||||
|
||||
@QueryMapping
|
||||
public Mono<String> invalidType(@AuthenticationPrincipal Mono<String> userDetails) {
|
||||
return userDetails;
|
||||
}
|
||||
|
||||
@QueryMapping
|
||||
public Publisher<UserDetails> publisher(@AuthenticationPrincipal Publisher<UserDetails> userDetails) {
|
||||
return userDetails;
|
||||
}
|
||||
|
||||
@QueryMapping
|
||||
public Publisher<UserDetails> publisher(@AuthenticationPrincipal TestPublisher<UserDetails> userDetails) {
|
||||
return userDetails;
|
||||
}
|
||||
}
|
||||
|
||||
@AuthenticationPrincipal
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface CurrentUser {
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user