From 208af98ad0c39d66b4cc620c3c4b77bbffe3e898 Mon Sep 17 00:00:00 2001 From: Cherepanova Ekaterina Date: Mon, 9 Sep 2019 16:20:55 +0300 Subject: [PATCH 1/2] Create OptionalPrincipalMethodArgumentResolver.java Add method argument resolver to Optional @MessageMapping can use Optional as a method argument, which allows to work with or without authentication and to avoid error message org.springframework.messaging.simp.annotation.support.MissingSessionUserException: No "user" header in message --- ...tionalPrincipalMethodArgumentResolver.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/OptionalPrincipalMethodArgumentResolver.java diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/OptionalPrincipalMethodArgumentResolver.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/OptionalPrincipalMethodArgumentResolver.java new file mode 100644 index 000000000000..ab5f920ebe2b --- /dev/null +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/OptionalPrincipalMethodArgumentResolver.java @@ -0,0 +1,50 @@ +/* + * Copyright 2002-2019 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.messaging.simp.annotation.support; + +import java.lang.reflect.Type; +import java.security.Principal; +import java.util.Optional; + +import org.springframework.core.MethodParameter; +import org.springframework.core.ResolvableType; +import org.springframework.messaging.Message; +import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver; +import org.springframework.messaging.simp.SimpMessageHeaderAccessor; + +/** + * {@link HandlerMethodArgumentResolver} to a {@link Optional} of type {@link Principal}. + * + * @author Ekaterina Cherepanova + * @since 5.x + */ +public class OptionalPrincipalMethodArgumentResolver implements HandlerMethodArgumentResolver { + + @Override + public boolean supportsParameter(MethodParameter parameter) { + Type genericParamType = parameter.getGenericParameterType(); + ResolvableType resolvableType = ResolvableType.forType(genericParamType).as(Optional.class); + Class genericClass = resolvableType.getGeneric().resolve(Object.class); + return Principal.class.isAssignableFrom(genericClass); + } + + @Override + public Object resolveArgument(MethodParameter parameter, Message message) { + Principal user = SimpMessageHeaderAccessor.getUser(message.getHeaders()); + return Optional.ofNullable(user); + } +} From 6d1c6132e3b7ff44798a18f2ea4779dae32af28f Mon Sep 17 00:00:00 2001 From: KateVasovski Date: Mon, 14 Oct 2019 12:31:16 +0300 Subject: [PATCH 2/2] New resolver is deleted and in PrincipalMethodArgumentResolver added possibility to resolve Optional --- ...tionalPrincipalMethodArgumentResolver.java | 50 ------------------- .../PrincipalMethodArgumentResolver.java | 15 +++--- 2 files changed, 9 insertions(+), 56 deletions(-) delete mode 100644 spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/OptionalPrincipalMethodArgumentResolver.java diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/OptionalPrincipalMethodArgumentResolver.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/OptionalPrincipalMethodArgumentResolver.java deleted file mode 100644 index ab5f920ebe2b..000000000000 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/OptionalPrincipalMethodArgumentResolver.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright 2002-2019 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.messaging.simp.annotation.support; - -import java.lang.reflect.Type; -import java.security.Principal; -import java.util.Optional; - -import org.springframework.core.MethodParameter; -import org.springframework.core.ResolvableType; -import org.springframework.messaging.Message; -import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver; -import org.springframework.messaging.simp.SimpMessageHeaderAccessor; - -/** - * {@link HandlerMethodArgumentResolver} to a {@link Optional} of type {@link Principal}. - * - * @author Ekaterina Cherepanova - * @since 5.x - */ -public class OptionalPrincipalMethodArgumentResolver implements HandlerMethodArgumentResolver { - - @Override - public boolean supportsParameter(MethodParameter parameter) { - Type genericParamType = parameter.getGenericParameterType(); - ResolvableType resolvableType = ResolvableType.forType(genericParamType).as(Optional.class); - Class genericClass = resolvableType.getGeneric().resolve(Object.class); - return Principal.class.isAssignableFrom(genericClass); - } - - @Override - public Object resolveArgument(MethodParameter parameter, Message message) { - Principal user = SimpMessageHeaderAccessor.getUser(message.getHeaders()); - return Optional.ofNullable(user); - } -} diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/PrincipalMethodArgumentResolver.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/PrincipalMethodArgumentResolver.java index 4c7d9824ad45..859eaef433ac 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/PrincipalMethodArgumentResolver.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/PrincipalMethodArgumentResolver.java @@ -17,6 +17,7 @@ package org.springframework.messaging.simp.annotation.support; import java.security.Principal; +import java.util.Optional; import org.springframework.core.MethodParameter; import org.springframework.messaging.Message; @@ -24,7 +25,7 @@ import org.springframework.messaging.simp.SimpMessageHeaderAccessor; /** - * {@link HandlerMethodArgumentResolver} to a {@link Principal}. + * {@link HandlerMethodArgumentResolver} to a {@link Principal} or {@link Optional} of {@link Principal}. * * @author Rossen Stoyanchev * @since 4.0 @@ -33,17 +34,19 @@ public class PrincipalMethodArgumentResolver implements HandlerMethodArgumentRes @Override public boolean supportsParameter(MethodParameter parameter) { - Class paramType = parameter.getParameterType(); + MethodParameter nestedParameter = parameter.nestedIfOptional(); + Class paramType = nestedParameter.getNestedParameterType(); return Principal.class.isAssignableFrom(paramType); } @Override - public Object resolveArgument(MethodParameter parameter, Message message) throws Exception { + public Object resolveArgument(MethodParameter parameter, Message message){ Principal user = SimpMessageHeaderAccessor.getUser(message.getHeaders()); - if (user == null) { - throw new MissingSessionUserException(message); + if (parameter.isOptional()) { + return Optional.ofNullable(user); + } else { + return user; } - return user; } }