Merge pull request #23612 from KateVasovski/add-principal-method-argument-resolver
This commit is contained in:
commit
1b43b09e55
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2018 the original author or authors.
|
* Copyright 2002-2019 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -17,6 +17,7 @@
|
||||||
package org.springframework.messaging.simp.annotation.support;
|
package org.springframework.messaging.simp.annotation.support;
|
||||||
|
|
||||||
import java.security.Principal;
|
import java.security.Principal;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.springframework.core.MethodParameter;
|
import org.springframework.core.MethodParameter;
|
||||||
import org.springframework.messaging.Message;
|
import org.springframework.messaging.Message;
|
||||||
|
@ -24,7 +25,7 @@ import org.springframework.messaging.handler.invocation.HandlerMethodArgumentRes
|
||||||
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
|
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link HandlerMethodArgumentResolver} to a {@link Principal}.
|
* Resolver for arguments of type {@link Principal}, including {@code Optional<Principal>}.
|
||||||
*
|
*
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
* @since 4.0
|
* @since 4.0
|
||||||
|
@ -33,17 +34,15 @@ public class PrincipalMethodArgumentResolver implements HandlerMethodArgumentRes
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean supportsParameter(MethodParameter parameter) {
|
public boolean supportsParameter(MethodParameter parameter) {
|
||||||
Class<?> paramType = parameter.getParameterType();
|
MethodParameter nestedParameter = parameter.nestedIfOptional();
|
||||||
|
Class<?> paramType = nestedParameter.getNestedParameterType();
|
||||||
return Principal.class.isAssignableFrom(paramType);
|
return Principal.class.isAssignableFrom(paramType);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception {
|
public Object resolveArgument(MethodParameter parameter, Message<?> message){
|
||||||
Principal user = SimpMessageHeaderAccessor.getUser(message.getHeaders());
|
Principal user = SimpMessageHeaderAccessor.getUser(message.getHeaders());
|
||||||
if (user == null) {
|
return parameter.isOptional() ? Optional.ofNullable(user) : user;
|
||||||
throw new MissingSessionUserException(message);
|
|
||||||
}
|
|
||||||
return user;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue