Reformat code using spring-javaformat
Run `./gradlew format` to reformat all java files. Issue gh-8945
This commit is contained in:
+5
-7
@@ -29,20 +29,17 @@ import org.springframework.util.Assert;
|
||||
* {@link MessageSecurityExpressionRoot}.
|
||||
*
|
||||
* @param <T> the type for the body of the Message
|
||||
*
|
||||
* @since 4.0
|
||||
* @author Rob Winch
|
||||
*/
|
||||
public class DefaultMessageSecurityExpressionHandler<T> extends
|
||||
AbstractSecurityExpressionHandler<Message<T>> {
|
||||
public class DefaultMessageSecurityExpressionHandler<T> extends AbstractSecurityExpressionHandler<Message<T>> {
|
||||
|
||||
private AuthenticationTrustResolver trustResolver = new AuthenticationTrustResolverImpl();
|
||||
|
||||
@Override
|
||||
protected SecurityExpressionOperations createSecurityExpressionRoot(
|
||||
Authentication authentication, Message<T> invocation) {
|
||||
MessageSecurityExpressionRoot root = new MessageSecurityExpressionRoot(
|
||||
authentication, invocation);
|
||||
protected SecurityExpressionOperations createSecurityExpressionRoot(Authentication authentication,
|
||||
Message<T> invocation) {
|
||||
MessageSecurityExpressionRoot root = new MessageSecurityExpressionRoot(authentication, invocation);
|
||||
root.setPermissionEvaluator(getPermissionEvaluator());
|
||||
root.setTrustResolver(trustResolver);
|
||||
root.setRoleHierarchy(getRoleHierarchy());
|
||||
@@ -53,4 +50,5 @@ public class DefaultMessageSecurityExpressionHandler<T> extends
|
||||
Assert.notNull(trustResolver, "trustResolver cannot be null");
|
||||
this.trustResolver = trustResolver;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+7
-10
@@ -19,8 +19,7 @@ import org.springframework.expression.EvaluationContext;
|
||||
|
||||
/**
|
||||
*
|
||||
/**
|
||||
* Allows post processing the {@link EvaluationContext}
|
||||
* /** Allows post processing the {@link EvaluationContext}
|
||||
*
|
||||
* <p>
|
||||
* This API is intentionally kept package scope as it may evolve over time.
|
||||
@@ -32,15 +31,13 @@ import org.springframework.expression.EvaluationContext;
|
||||
interface EvaluationContextPostProcessor<I> {
|
||||
|
||||
/**
|
||||
* Allows post processing of the {@link EvaluationContext}. Implementations
|
||||
* may return a new instance of {@link EvaluationContext} or modify the
|
||||
* {@link EvaluationContext} that was passed in.
|
||||
*
|
||||
* @param context
|
||||
* the original {@link EvaluationContext}
|
||||
* @param invocation
|
||||
* the security invocation object (i.e. Message)
|
||||
* Allows post processing of the {@link EvaluationContext}. Implementations may return
|
||||
* a new instance of {@link EvaluationContext} or modify the {@link EvaluationContext}
|
||||
* that was passed in.
|
||||
* @param context the original {@link EvaluationContext}
|
||||
* @param invocation the security invocation object (i.e. Message)
|
||||
* @return the upated context.
|
||||
*/
|
||||
EvaluationContext postProcess(EvaluationContext context, I invocation);
|
||||
|
||||
}
|
||||
|
||||
+6
-6
@@ -61,7 +61,6 @@ public final class ExpressionBasedMessageSecurityMetadataSourceFactory {
|
||||
*
|
||||
* <p>
|
||||
* For a complete listing of expressions see {@link MessageSecurityExpressionRoot}
|
||||
*
|
||||
* @param matcherToExpression an ordered mapping of {@link MessageMatcher} to Strings
|
||||
* that are turned into an Expression using
|
||||
* {@link DefaultMessageSecurityExpressionHandler#getExpressionParser()}
|
||||
@@ -69,7 +68,8 @@ public final class ExpressionBasedMessageSecurityMetadataSourceFactory {
|
||||
*/
|
||||
public static MessageSecurityMetadataSource createExpressionMessageMetadataSource(
|
||||
LinkedHashMap<MessageMatcher<?>, String> matcherToExpression) {
|
||||
return createExpressionMessageMetadataSource(matcherToExpression, new DefaultMessageSecurityExpressionHandler<>());
|
||||
return createExpressionMessageMetadataSource(matcherToExpression,
|
||||
new DefaultMessageSecurityExpressionHandler<>());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -98,7 +98,6 @@ public final class ExpressionBasedMessageSecurityMetadataSourceFactory {
|
||||
* <p>
|
||||
* For a complete listing of expressions see {@link MessageSecurityExpressionRoot}
|
||||
* </p>
|
||||
*
|
||||
* @param matcherToExpression an ordered mapping of {@link MessageMatcher} to Strings
|
||||
* that are turned into an Expression using
|
||||
* {@link DefaultMessageSecurityExpressionHandler#getExpressionParser()}
|
||||
@@ -106,15 +105,15 @@ public final class ExpressionBasedMessageSecurityMetadataSourceFactory {
|
||||
* @return the {@link MessageSecurityMetadataSource} to use. Cannot be null.
|
||||
*/
|
||||
public static MessageSecurityMetadataSource createExpressionMessageMetadataSource(
|
||||
LinkedHashMap<MessageMatcher<?>, String> matcherToExpression, SecurityExpressionHandler<Message<Object>> handler) {
|
||||
LinkedHashMap<MessageMatcher<?>, String> matcherToExpression,
|
||||
SecurityExpressionHandler<Message<Object>> handler) {
|
||||
|
||||
LinkedHashMap<MessageMatcher<?>, Collection<ConfigAttribute>> matcherToAttrs = new LinkedHashMap<>();
|
||||
|
||||
for (Map.Entry<MessageMatcher<?>, String> entry : matcherToExpression.entrySet()) {
|
||||
MessageMatcher<?> matcher = entry.getKey();
|
||||
String rawExpression = entry.getValue();
|
||||
Expression expression = handler.getExpressionParser().parseExpression(
|
||||
rawExpression);
|
||||
Expression expression = handler.getExpressionParser().parseExpression(rawExpression);
|
||||
ConfigAttribute attribute = new MessageExpressionConfigAttribute(expression, matcher);
|
||||
matcherToAttrs.put(matcher, Arrays.asList(attribute));
|
||||
}
|
||||
@@ -123,4 +122,5 @@ public final class ExpressionBasedMessageSecurityMetadataSourceFactory {
|
||||
|
||||
private ExpressionBasedMessageSecurityMetadataSourceFactory() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+7
-6
@@ -34,13 +34,13 @@ import java.util.Map;
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
class MessageExpressionConfigAttribute implements ConfigAttribute, EvaluationContextPostProcessor<Message<?>> {
|
||||
private final Expression authorizeExpression;
|
||||
private final MessageMatcher<?> matcher;
|
||||
|
||||
private final Expression authorizeExpression;
|
||||
|
||||
private final MessageMatcher<?> matcher;
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
*
|
||||
* @param authorizeExpression the {@link Expression} to use. Cannot be null
|
||||
* @param matcher the {@link MessageMatcher} used to match the messages.
|
||||
*/
|
||||
@@ -51,7 +51,6 @@ class MessageExpressionConfigAttribute implements ConfigAttribute, EvaluationCon
|
||||
this.matcher = matcher;
|
||||
}
|
||||
|
||||
|
||||
Expression getAuthorizeExpression() {
|
||||
return authorizeExpression;
|
||||
}
|
||||
@@ -68,11 +67,13 @@ class MessageExpressionConfigAttribute implements ConfigAttribute, EvaluationCon
|
||||
@Override
|
||||
public EvaluationContext postProcess(EvaluationContext ctx, Message<?> message) {
|
||||
if (matcher instanceof SimpDestinationMessageMatcher) {
|
||||
final Map<String, String> variables = ((SimpDestinationMessageMatcher) matcher).extractPathVariables(message);
|
||||
for (Map.Entry<String, String> entry : variables.entrySet()){
|
||||
final Map<String, String> variables = ((SimpDestinationMessageMatcher) matcher)
|
||||
.extractPathVariables(message);
|
||||
for (Map.Entry<String, String> entry : variables.entrySet()) {
|
||||
ctx.setVariable(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
return ctx;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+7
-10
@@ -38,10 +38,10 @@ import java.util.Collection;
|
||||
* @author Daniel Bustamante Ospina
|
||||
*/
|
||||
public class MessageExpressionVoter<T> implements AccessDecisionVoter<Message<T>> {
|
||||
|
||||
private SecurityExpressionHandler<Message<T>> expressionHandler = new DefaultMessageSecurityExpressionHandler<>();
|
||||
|
||||
public int vote(Authentication authentication, Message<T> message,
|
||||
Collection<ConfigAttribute> attributes) {
|
||||
public int vote(Authentication authentication, Message<T> message, Collection<ConfigAttribute> attributes) {
|
||||
assert authentication != null;
|
||||
assert message != null;
|
||||
assert attributes != null;
|
||||
@@ -52,16 +52,13 @@ public class MessageExpressionVoter<T> implements AccessDecisionVoter<Message<T>
|
||||
return ACCESS_ABSTAIN;
|
||||
}
|
||||
|
||||
EvaluationContext ctx = expressionHandler.createEvaluationContext(authentication,
|
||||
message);
|
||||
EvaluationContext ctx = expressionHandler.createEvaluationContext(authentication, message);
|
||||
ctx = attr.postProcess(ctx, message);
|
||||
|
||||
return ExpressionUtils.evaluateAsBoolean(attr.getAuthorizeExpression(), ctx) ? ACCESS_GRANTED
|
||||
: ACCESS_DENIED;
|
||||
return ExpressionUtils.evaluateAsBoolean(attr.getAuthorizeExpression(), ctx) ? ACCESS_GRANTED : ACCESS_DENIED;
|
||||
}
|
||||
|
||||
private MessageExpressionConfigAttribute findConfigAttribute(
|
||||
Collection<ConfigAttribute> attributes) {
|
||||
private MessageExpressionConfigAttribute findConfigAttribute(Collection<ConfigAttribute> attributes) {
|
||||
for (ConfigAttribute attribute : attributes) {
|
||||
if (attribute instanceof MessageExpressionConfigAttribute) {
|
||||
return (MessageExpressionConfigAttribute) attribute;
|
||||
@@ -78,9 +75,9 @@ public class MessageExpressionVoter<T> implements AccessDecisionVoter<Message<T>
|
||||
return Message.class.isAssignableFrom(clazz);
|
||||
}
|
||||
|
||||
public void setExpressionHandler(
|
||||
SecurityExpressionHandler<Message<T>> expressionHandler) {
|
||||
public void setExpressionHandler(SecurityExpressionHandler<Message<T>> expressionHandler) {
|
||||
Assert.notNull(expressionHandler, "expressionHandler cannot be null");
|
||||
this.expressionHandler = expressionHandler;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
@@ -33,4 +33,5 @@ public class MessageSecurityExpressionRoot extends SecurityExpressionRoot {
|
||||
super(authentication);
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+5
-7
@@ -36,15 +36,14 @@ import org.springframework.util.Assert;
|
||||
* @since 4.0
|
||||
* @author Rob Winch
|
||||
*/
|
||||
public final class ChannelSecurityInterceptor extends AbstractSecurityInterceptor
|
||||
implements ChannelInterceptor {
|
||||
public final class ChannelSecurityInterceptor extends AbstractSecurityInterceptor implements ChannelInterceptor {
|
||||
|
||||
private static final ThreadLocal<InterceptorStatusToken> tokenHolder = new ThreadLocal<>();
|
||||
|
||||
private final MessageSecurityMetadataSource metadataSource;
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
*
|
||||
* @param metadataSource the MessageSecurityMetadataSource to use. Cannot be null.
|
||||
*
|
||||
* @see DefaultMessageSecurityMetadataSource
|
||||
@@ -78,8 +77,7 @@ public final class ChannelSecurityInterceptor extends AbstractSecurityIntercepto
|
||||
afterInvocation(token, null);
|
||||
}
|
||||
|
||||
public void afterSendCompletion(Message<?> message, MessageChannel channel,
|
||||
boolean sent, Exception ex) {
|
||||
public void afterSendCompletion(Message<?> message, MessageChannel channel, boolean sent, Exception ex) {
|
||||
InterceptorStatusToken token = clearToken();
|
||||
finallyInvocation(token);
|
||||
}
|
||||
@@ -92,8 +90,7 @@ public final class ChannelSecurityInterceptor extends AbstractSecurityIntercepto
|
||||
return message;
|
||||
}
|
||||
|
||||
public void afterReceiveCompletion(Message<?> message, MessageChannel channel,
|
||||
Exception ex) {
|
||||
public void afterReceiveCompletion(Message<?> message, MessageChannel channel, Exception ex) {
|
||||
}
|
||||
|
||||
private InterceptorStatusToken clearToken() {
|
||||
@@ -101,4 +98,5 @@ public final class ChannelSecurityInterceptor extends AbstractSecurityIntercepto
|
||||
tokenHolder.remove();
|
||||
return token;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+5
-7
@@ -33,12 +33,11 @@ import java.util.*;
|
||||
*
|
||||
* @see ChannelSecurityInterceptor
|
||||
* @see ExpressionBasedMessageSecurityMetadataSourceFactory
|
||||
*
|
||||
* @since 4.0
|
||||
* @author Rob Winch
|
||||
*/
|
||||
public final class DefaultMessageSecurityMetadataSource implements
|
||||
MessageSecurityMetadataSource {
|
||||
public final class DefaultMessageSecurityMetadataSource implements MessageSecurityMetadataSource {
|
||||
|
||||
private final Map<MessageMatcher<?>, Collection<ConfigAttribute>> messageMap;
|
||||
|
||||
public DefaultMessageSecurityMetadataSource(
|
||||
@@ -47,11 +46,9 @@ public final class DefaultMessageSecurityMetadataSource implements
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public Collection<ConfigAttribute> getAttributes(Object object)
|
||||
throws IllegalArgumentException {
|
||||
public Collection<ConfigAttribute> getAttributes(Object object) throws IllegalArgumentException {
|
||||
final Message message = (Message) object;
|
||||
for (Map.Entry<MessageMatcher<?>, Collection<ConfigAttribute>> entry : messageMap
|
||||
.entrySet()) {
|
||||
for (Map.Entry<MessageMatcher<?>, Collection<ConfigAttribute>> entry : messageMap.entrySet()) {
|
||||
if (entry.getKey().matches(message)) {
|
||||
return entry.getValue();
|
||||
}
|
||||
@@ -72,4 +69,5 @@ public final class DefaultMessageSecurityMetadataSource implements
|
||||
public boolean supports(Class<?> clazz) {
|
||||
return Message.class.isAssignableFrom(clazz);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -23,9 +23,9 @@ import org.springframework.security.access.SecurityMetadataSource;
|
||||
*
|
||||
* @see ChannelSecurityInterceptor
|
||||
* @see DefaultMessageSecurityMetadataSource
|
||||
*
|
||||
* @since 4.0
|
||||
* @author Rob Winch
|
||||
*/
|
||||
public interface MessageSecurityMetadataSource extends SecurityMetadataSource {
|
||||
|
||||
}
|
||||
|
||||
+8
-15
@@ -81,8 +81,7 @@ import org.springframework.util.StringUtils;
|
||||
* @author Rob Winch
|
||||
* @since 4.0
|
||||
*/
|
||||
public final class AuthenticationPrincipalArgumentResolver
|
||||
implements HandlerMethodArgumentResolver {
|
||||
public final class AuthenticationPrincipalArgumentResolver implements HandlerMethodArgumentResolver {
|
||||
|
||||
private ExpressionParser parser = new SpelExpressionParser();
|
||||
|
||||
@@ -106,15 +105,13 @@ public final class AuthenticationPrincipalArgumentResolver
|
||||
* org.springframework.messaging.Message)
|
||||
*/
|
||||
public Object resolveArgument(MethodParameter parameter, Message<?> message) {
|
||||
Authentication authentication = SecurityContextHolder.getContext()
|
||||
.getAuthentication();
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication == null) {
|
||||
return null;
|
||||
}
|
||||
Object principal = authentication.getPrincipal();
|
||||
|
||||
AuthenticationPrincipal authPrincipal = findMethodAnnotation(
|
||||
AuthenticationPrincipal.class, parameter);
|
||||
AuthenticationPrincipal authPrincipal = findMethodAnnotation(AuthenticationPrincipal.class, parameter);
|
||||
|
||||
String expressionToParse = authPrincipal.expression();
|
||||
if (StringUtils.hasLength(expressionToParse)) {
|
||||
@@ -126,11 +123,9 @@ public final class AuthenticationPrincipalArgumentResolver
|
||||
principal = expression.getValue(context);
|
||||
}
|
||||
|
||||
if (principal != null
|
||||
&& !parameter.getParameterType().isAssignableFrom(principal.getClass())) {
|
||||
if (principal != null && !parameter.getParameterType().isAssignableFrom(principal.getClass())) {
|
||||
if (authPrincipal.errorOnInvalidType()) {
|
||||
throw new ClassCastException(principal + " is not assignable to "
|
||||
+ parameter.getParameterType());
|
||||
throw new ClassCastException(principal + " is not assignable to " + parameter.getParameterType());
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
@@ -141,26 +136,24 @@ public final class AuthenticationPrincipalArgumentResolver
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
private <T extends Annotation> T findMethodAnnotation(Class<T> annotationClass,
|
||||
MethodParameter parameter) {
|
||||
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);
|
||||
annotation = AnnotationUtils.findAnnotation(toSearch.annotationType(), annotationClass);
|
||||
if (annotation != null) {
|
||||
return annotation;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+10
-14
@@ -41,14 +41,15 @@ import org.springframework.util.Assert;
|
||||
*/
|
||||
public final class SecurityContextChannelInterceptor extends ChannelInterceptorAdapter
|
||||
implements ExecutorChannelInterceptor {
|
||||
private final SecurityContext EMPTY_CONTEXT = SecurityContextHolder
|
||||
.createEmptyContext();
|
||||
|
||||
private final SecurityContext EMPTY_CONTEXT = SecurityContextHolder.createEmptyContext();
|
||||
|
||||
private static final ThreadLocal<Stack<SecurityContext>> ORIGINAL_CONTEXT = new ThreadLocal<>();
|
||||
|
||||
private final String authenticationHeaderName;
|
||||
|
||||
private Authentication anonymous = new AnonymousAuthenticationToken("key",
|
||||
"anonymous", AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS"));
|
||||
private Authentication anonymous = new AnonymousAuthenticationToken("key", "anonymous",
|
||||
AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS"));
|
||||
|
||||
/**
|
||||
* Creates a new instance using the header of the name
|
||||
@@ -61,13 +62,11 @@ public final class SecurityContextChannelInterceptor extends ChannelInterceptorA
|
||||
/**
|
||||
* Creates a new instance that uses the specified header to obtain the
|
||||
* {@link Authentication}.
|
||||
*
|
||||
* @param authenticationHeaderName the header name to obtain the
|
||||
* {@link Authentication}. Cannot be null.
|
||||
*/
|
||||
public SecurityContextChannelInterceptor(String authenticationHeaderName) {
|
||||
Assert.notNull(authenticationHeaderName,
|
||||
"authenticationHeaderName cannot be null");
|
||||
Assert.notNull(authenticationHeaderName, "authenticationHeaderName cannot be null");
|
||||
this.authenticationHeaderName = authenticationHeaderName;
|
||||
}
|
||||
|
||||
@@ -78,7 +77,6 @@ public final class SecurityContextChannelInterceptor extends ChannelInterceptorA
|
||||
* new AnonymousAuthenticationToken("key", "anonymous",
|
||||
* AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS"));
|
||||
* </pre>
|
||||
*
|
||||
* @param authentication the Authentication used for anonymous authentication. Cannot
|
||||
* be null.
|
||||
*/
|
||||
@@ -94,19 +92,16 @@ public final class SecurityContextChannelInterceptor extends ChannelInterceptorA
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterSendCompletion(Message<?> message, MessageChannel channel,
|
||||
boolean sent, Exception ex) {
|
||||
public void afterSendCompletion(Message<?> message, MessageChannel channel, boolean sent, Exception ex) {
|
||||
cleanup();
|
||||
}
|
||||
|
||||
public Message<?> beforeHandle(Message<?> message, MessageChannel channel,
|
||||
MessageHandler handler) {
|
||||
public Message<?> beforeHandle(Message<?> message, MessageChannel channel, MessageHandler handler) {
|
||||
setup(message);
|
||||
return message;
|
||||
}
|
||||
|
||||
public void afterMessageHandled(Message<?> message, MessageChannel channel,
|
||||
MessageHandler handler, Exception ex) {
|
||||
public void afterMessageHandled(Message<?> message, MessageChannel channel, MessageHandler handler, Exception ex) {
|
||||
cleanup();
|
||||
}
|
||||
|
||||
@@ -158,4 +153,5 @@ public final class SecurityContextChannelInterceptor extends ChannelInterceptorA
|
||||
SecurityContextHolder.clearContext();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+19
-30
@@ -86,18 +86,17 @@ import java.lang.annotation.Annotation;
|
||||
* }
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* @author Rob Winch
|
||||
* @since 5.2
|
||||
*/
|
||||
public class AuthenticationPrincipalArgumentResolver
|
||||
implements HandlerMethodArgumentResolver {
|
||||
public class AuthenticationPrincipalArgumentResolver implements HandlerMethodArgumentResolver {
|
||||
|
||||
private ExpressionParser parser = new SpelExpressionParser();
|
||||
|
||||
private BeanResolver beanResolver;
|
||||
|
||||
private ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry
|
||||
.getSharedInstance();
|
||||
private ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
|
||||
|
||||
/**
|
||||
* Sets the {@link BeanResolver} to be used on the expressions
|
||||
@@ -109,8 +108,8 @@ public class AuthenticationPrincipalArgumentResolver
|
||||
|
||||
/**
|
||||
* Sets the {@link ReactiveAdapterRegistry} to be used.
|
||||
* @param adapterRegistry the {@link ReactiveAdapterRegistry} to use. Cannot be null. Default is
|
||||
* {@link ReactiveAdapterRegistry#getSharedInstance()}
|
||||
* @param adapterRegistry the {@link ReactiveAdapterRegistry} to use. Cannot be null.
|
||||
* Default is {@link ReactiveAdapterRegistry#getSharedInstance()}
|
||||
*/
|
||||
public void setAdapterRegistry(ReactiveAdapterRegistry adapterRegistry) {
|
||||
Assert.notNull(adapterRegistry, "adapterRegistry cannot be null");
|
||||
@@ -123,21 +122,16 @@ public class AuthenticationPrincipalArgumentResolver
|
||||
}
|
||||
|
||||
public Mono<Object> resolveArgument(MethodParameter parameter, Message<?> message) {
|
||||
ReactiveAdapter adapter = this.adapterRegistry
|
||||
.getAdapter(parameter.getParameterType());
|
||||
return ReactiveSecurityContextHolder.getContext()
|
||||
.map(SecurityContext::getAuthentication).flatMap(a -> {
|
||||
Object p = resolvePrincipal(parameter, a.getPrincipal());
|
||||
Mono<Object> principal = Mono.justOrEmpty(p);
|
||||
return adapter == null ?
|
||||
principal :
|
||||
Mono.just(adapter.fromPublisher(principal));
|
||||
});
|
||||
ReactiveAdapter adapter = this.adapterRegistry.getAdapter(parameter.getParameterType());
|
||||
return ReactiveSecurityContextHolder.getContext().map(SecurityContext::getAuthentication).flatMap(a -> {
|
||||
Object p = resolvePrincipal(parameter, a.getPrincipal());
|
||||
Mono<Object> principal = Mono.justOrEmpty(p);
|
||||
return adapter == null ? principal : Mono.just(adapter.fromPublisher(principal));
|
||||
});
|
||||
}
|
||||
|
||||
private Object resolvePrincipal(MethodParameter parameter, Object principal) {
|
||||
AuthenticationPrincipal authPrincipal = findMethodAnnotation(
|
||||
AuthenticationPrincipal.class, parameter);
|
||||
AuthenticationPrincipal authPrincipal = findMethodAnnotation(AuthenticationPrincipal.class, parameter);
|
||||
|
||||
String expressionToParse = authPrincipal.expression();
|
||||
if (StringUtils.hasLength(expressionToParse)) {
|
||||
@@ -153,9 +147,7 @@ public class AuthenticationPrincipalArgumentResolver
|
||||
if (isInvalidType(parameter, principal)) {
|
||||
|
||||
if (authPrincipal.errorOnInvalidType()) {
|
||||
throw new ClassCastException(
|
||||
principal + " is not assignable to " + parameter
|
||||
.getParameterType());
|
||||
throw new ClassCastException(principal + " is not assignable to " + parameter.getParameterType());
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
@@ -170,8 +162,7 @@ public class AuthenticationPrincipalArgumentResolver
|
||||
return false;
|
||||
}
|
||||
Class<?> typeToCheck = parameter.getParameterType();
|
||||
boolean isParameterPublisher = Publisher.class
|
||||
.isAssignableFrom(parameter.getParameterType());
|
||||
boolean isParameterPublisher = Publisher.class.isAssignableFrom(parameter.getParameterType());
|
||||
if (isParameterPublisher) {
|
||||
ResolvableType resolvableType = ResolvableType.forMethodParameter(parameter);
|
||||
Class<?> genericType = resolvableType.resolveGeneric(0);
|
||||
@@ -185,26 +176,24 @@ public class AuthenticationPrincipalArgumentResolver
|
||||
|
||||
/**
|
||||
* 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}
|
||||
* {@link MethodParameter}
|
||||
* @param parameter the {@link MethodParameter} to search for an {@link Annotation}
|
||||
* @return the {@link Annotation} that was found or null.
|
||||
*/
|
||||
private <T extends Annotation> T findMethodAnnotation(Class<T> annotationClass,
|
||||
MethodParameter parameter) {
|
||||
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);
|
||||
annotation = AnnotationUtils.findAnnotation(toSearch.annotationType(), annotationClass);
|
||||
if (annotation != null) {
|
||||
return annotation;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+22
-33
@@ -56,9 +56,9 @@ import java.lang.annotation.Annotation;
|
||||
* </pre>
|
||||
*
|
||||
* <p>
|
||||
* Will resolve the SecurityContext argument using the {@link ReactiveSecurityContextHolder}.
|
||||
* If the {@link SecurityContext} is empty, it will return null. If the types do not
|
||||
* match, null will be returned unless
|
||||
* Will resolve the SecurityContext argument using the
|
||||
* {@link ReactiveSecurityContextHolder}. If the {@link SecurityContext} is empty, it will
|
||||
* return null. If the types do not match, null will be returned unless
|
||||
* {@link CurrentSecurityContext#errorOnInvalidType()} is true in which case a
|
||||
* {@link ClassCastException} will be thrown.
|
||||
*
|
||||
@@ -85,18 +85,17 @@ import java.lang.annotation.Annotation;
|
||||
* }
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* @author Rob Winch
|
||||
* @since 5.2
|
||||
*/
|
||||
public class CurrentSecurityContextArgumentResolver
|
||||
implements HandlerMethodArgumentResolver {
|
||||
public class CurrentSecurityContextArgumentResolver implements HandlerMethodArgumentResolver {
|
||||
|
||||
private ExpressionParser parser = new SpelExpressionParser();
|
||||
|
||||
private BeanResolver beanResolver;
|
||||
|
||||
private ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry
|
||||
.getSharedInstance();
|
||||
private ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
|
||||
|
||||
/**
|
||||
* Sets the {@link BeanResolver} to be used on the expressions
|
||||
@@ -108,8 +107,8 @@ public class CurrentSecurityContextArgumentResolver
|
||||
|
||||
/**
|
||||
* Sets the {@link ReactiveAdapterRegistry} to be used.
|
||||
* @param adapterRegistry the {@link ReactiveAdapterRegistry} to use. Cannot be null. Default is
|
||||
* {@link ReactiveAdapterRegistry#getSharedInstance()}
|
||||
* @param adapterRegistry the {@link ReactiveAdapterRegistry} to use. Cannot be null.
|
||||
* Default is {@link ReactiveAdapterRegistry#getSharedInstance()}
|
||||
*/
|
||||
public void setAdapterRegistry(ReactiveAdapterRegistry adapterRegistry) {
|
||||
Assert.notNull(adapterRegistry, "adapterRegistry cannot be null");
|
||||
@@ -122,21 +121,16 @@ public class CurrentSecurityContextArgumentResolver
|
||||
}
|
||||
|
||||
public Mono<Object> resolveArgument(MethodParameter parameter, Message<?> message) {
|
||||
ReactiveAdapter adapter = this.adapterRegistry
|
||||
.getAdapter(parameter.getParameterType());
|
||||
return ReactiveSecurityContextHolder.getContext()
|
||||
.flatMap(securityContext -> {
|
||||
Object sc = resolveSecurityContext(parameter, securityContext);
|
||||
Mono<Object> result = Mono.justOrEmpty(sc);
|
||||
return adapter == null ?
|
||||
result :
|
||||
Mono.just(adapter.fromPublisher(result));
|
||||
});
|
||||
ReactiveAdapter adapter = this.adapterRegistry.getAdapter(parameter.getParameterType());
|
||||
return ReactiveSecurityContextHolder.getContext().flatMap(securityContext -> {
|
||||
Object sc = resolveSecurityContext(parameter, securityContext);
|
||||
Mono<Object> result = Mono.justOrEmpty(sc);
|
||||
return adapter == null ? result : Mono.just(adapter.fromPublisher(result));
|
||||
});
|
||||
}
|
||||
|
||||
private Object resolveSecurityContext(MethodParameter parameter, Object securityContext) {
|
||||
CurrentSecurityContext contextAnno = findMethodAnnotation(
|
||||
CurrentSecurityContext.class, parameter);
|
||||
CurrentSecurityContext contextAnno = findMethodAnnotation(CurrentSecurityContext.class, parameter);
|
||||
|
||||
String expressionToParse = contextAnno.expression();
|
||||
if (StringUtils.hasLength(expressionToParse)) {
|
||||
@@ -152,9 +146,7 @@ public class CurrentSecurityContextArgumentResolver
|
||||
if (isInvalidType(parameter, securityContext)) {
|
||||
|
||||
if (contextAnno.errorOnInvalidType()) {
|
||||
throw new ClassCastException(
|
||||
securityContext + " is not assignable to " + parameter
|
||||
.getParameterType());
|
||||
throw new ClassCastException(securityContext + " is not assignable to " + parameter.getParameterType());
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
@@ -169,8 +161,7 @@ public class CurrentSecurityContextArgumentResolver
|
||||
return false;
|
||||
}
|
||||
Class<?> typeToCheck = parameter.getParameterType();
|
||||
boolean isParameterPublisher = Publisher.class
|
||||
.isAssignableFrom(parameter.getParameterType());
|
||||
boolean isParameterPublisher = Publisher.class.isAssignableFrom(parameter.getParameterType());
|
||||
if (isParameterPublisher) {
|
||||
ResolvableType resolvableType = ResolvableType.forMethodParameter(parameter);
|
||||
Class<?> genericType = resolvableType.resolveGeneric(0);
|
||||
@@ -184,26 +175,24 @@ public class CurrentSecurityContextArgumentResolver
|
||||
|
||||
/**
|
||||
* 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}
|
||||
* {@link MethodParameter}
|
||||
* @param parameter the {@link MethodParameter} to search for an {@link Annotation}
|
||||
* @return the {@link Annotation} that was found or null.
|
||||
*/
|
||||
private <T extends Annotation> T findMethodAnnotation(Class<T> annotationClass,
|
||||
MethodParameter parameter) {
|
||||
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);
|
||||
annotation = AnnotationUtils.findAnnotation(toSearch.annotationType(), annotationClass);
|
||||
if (annotation != null) {
|
||||
return annotation;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+3
-4
@@ -29,20 +29,19 @@ import org.apache.commons.logging.Log;
|
||||
* @since 4.0
|
||||
*/
|
||||
abstract class AbstractMessageMatcherComposite<T> implements MessageMatcher<T> {
|
||||
|
||||
protected final Log LOGGER = getLog(getClass());
|
||||
|
||||
private final List<MessageMatcher<T>> messageMatchers;
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
*
|
||||
* @param messageMatchers the {@link MessageMatcher} instances to try
|
||||
*/
|
||||
AbstractMessageMatcherComposite(List<MessageMatcher<T>> messageMatchers) {
|
||||
notEmpty(messageMatchers, "messageMatchers must contain a value");
|
||||
if (messageMatchers.contains(null)) {
|
||||
throw new IllegalArgumentException(
|
||||
"messageMatchers cannot contain null values");
|
||||
throw new IllegalArgumentException("messageMatchers cannot contain null values");
|
||||
}
|
||||
this.messageMatchers = messageMatchers;
|
||||
|
||||
@@ -50,7 +49,6 @@ abstract class AbstractMessageMatcherComposite<T> implements MessageMatcher<T> {
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
*
|
||||
* @param messageMatchers the {@link MessageMatcher} instances to try
|
||||
*/
|
||||
@SafeVarargs
|
||||
@@ -66,4 +64,5 @@ abstract class AbstractMessageMatcherComposite<T> implements MessageMatcher<T> {
|
||||
public String toString() {
|
||||
return getClass().getSimpleName() + "[messageMatchers=" + messageMatchers + "]";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+2
-2
@@ -26,9 +26,9 @@ import org.springframework.messaging.Message;
|
||||
* @since 4.0
|
||||
*/
|
||||
public final class AndMessageMatcher<T> extends AbstractMessageMatcherComposite<T> {
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
*
|
||||
* @param messageMatchers the {@link MessageMatcher} instances to try
|
||||
*/
|
||||
public AndMessageMatcher(List<MessageMatcher<T>> messageMatchers) {
|
||||
@@ -37,7 +37,6 @@ public final class AndMessageMatcher<T> extends AbstractMessageMatcherComposite<
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
*
|
||||
* @param messageMatchers the {@link MessageMatcher} instances to try
|
||||
*/
|
||||
@SafeVarargs
|
||||
@@ -59,4 +58,5 @@ public final class AndMessageMatcher<T> extends AbstractMessageMatcherComposite<
|
||||
LOGGER.debug("All messageMatchers returned true");
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
+1
@@ -46,4 +46,5 @@ public interface MessageMatcher<T> {
|
||||
return "ANY_MESSAGE";
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
+2
-2
@@ -26,9 +26,9 @@ import org.springframework.messaging.Message;
|
||||
* @since 4.0
|
||||
*/
|
||||
public final class OrMessageMatcher<T> extends AbstractMessageMatcherComposite<T> {
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
*
|
||||
* @param messageMatchers the {@link MessageMatcher} instances to try
|
||||
*/
|
||||
public OrMessageMatcher(List<MessageMatcher<T>> messageMatchers) {
|
||||
@@ -37,7 +37,6 @@ public final class OrMessageMatcher<T> extends AbstractMessageMatcherComposite<T
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
*
|
||||
* @param messageMatchers the {@link MessageMatcher} instances to try
|
||||
*/
|
||||
@SafeVarargs
|
||||
@@ -59,4 +58,5 @@ public final class OrMessageMatcher<T> extends AbstractMessageMatcherComposite<T
|
||||
LOGGER.debug("No matches found");
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+19
-34
@@ -36,9 +36,9 @@ import java.util.Map;
|
||||
* @author Rob Winch
|
||||
*/
|
||||
public final class SimpDestinationMessageMatcher implements MessageMatcher<Object> {
|
||||
|
||||
public static final MessageMatcher<Object> NULL_DESTINATION_MATCHER = message -> {
|
||||
String destination = SimpMessageHeaderAccessor.getDestination(message
|
||||
.getHeaders());
|
||||
String destination = SimpMessageHeaderAccessor.getDestination(message.getHeaders());
|
||||
return destination == null;
|
||||
};
|
||||
|
||||
@@ -49,6 +49,7 @@ public final class SimpDestinationMessageMatcher implements MessageMatcher<Objec
|
||||
* null, this matcher will match every Message.
|
||||
*/
|
||||
private final MessageMatcher<Object> messageTypeMatcher;
|
||||
|
||||
private final String pattern;
|
||||
|
||||
/**
|
||||
@@ -77,7 +78,6 @@ public final class SimpDestinationMessageMatcher implements MessageMatcher<Objec
|
||||
* <li>{@code com/**/test} - matches all destinations ending with {@code test}
|
||||
* underneath the {@code com} path</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param pattern the pattern to use
|
||||
*/
|
||||
public SimpDestinationMessageMatcher(String pattern) {
|
||||
@@ -87,7 +87,6 @@ public final class SimpDestinationMessageMatcher implements MessageMatcher<Objec
|
||||
/**
|
||||
* <p>
|
||||
* Creates a new instance with the specified pattern and {@link PathMatcher}.
|
||||
*
|
||||
* @param pattern the pattern to use
|
||||
* @param pathMatcher the {@link PathMatcher} to use.
|
||||
*/
|
||||
@@ -99,24 +98,21 @@ public final class SimpDestinationMessageMatcher implements MessageMatcher<Objec
|
||||
* <p>
|
||||
* Creates a new instance with the specified pattern, {@link SimpMessageType}, and
|
||||
* {@link PathMatcher}.
|
||||
*
|
||||
* @param pattern the pattern to use
|
||||
* @param type the {@link SimpMessageType} to match on or null if any
|
||||
* {@link SimpMessageType} should be matched.
|
||||
* @param pathMatcher the {@link PathMatcher} to use.
|
||||
*/
|
||||
private SimpDestinationMessageMatcher(String pattern, SimpMessageType type,
|
||||
PathMatcher pathMatcher) {
|
||||
private SimpDestinationMessageMatcher(String pattern, SimpMessageType type, PathMatcher pathMatcher) {
|
||||
Assert.notNull(pattern, "pattern cannot be null");
|
||||
Assert.notNull(pathMatcher, "pathMatcher cannot be null");
|
||||
if (!isTypeWithDestination(type)) {
|
||||
throw new IllegalArgumentException("SimpMessageType " + type
|
||||
+ " does not contain a destination and so cannot be matched on.");
|
||||
throw new IllegalArgumentException(
|
||||
"SimpMessageType " + type + " does not contain a destination and so cannot be matched on.");
|
||||
}
|
||||
|
||||
this.matcher = pathMatcher;
|
||||
this.messageTypeMatcher = type == null ? ANY_MESSAGE
|
||||
: new SimpMessageTypeMatcher(type);
|
||||
this.messageTypeMatcher = type == null ? ANY_MESSAGE : new SimpMessageTypeMatcher(type);
|
||||
this.pattern = pattern;
|
||||
}
|
||||
|
||||
@@ -125,17 +121,13 @@ public final class SimpDestinationMessageMatcher implements MessageMatcher<Objec
|
||||
return false;
|
||||
}
|
||||
|
||||
String destination = SimpMessageHeaderAccessor.getDestination(message
|
||||
.getHeaders());
|
||||
String destination = SimpMessageHeaderAccessor.getDestination(message.getHeaders());
|
||||
return destination != null && matcher.match(pattern, destination);
|
||||
}
|
||||
|
||||
|
||||
public Map<String, String> extractPathVariables(Message<?> message){
|
||||
final String destination = SimpMessageHeaderAccessor.getDestination(message
|
||||
.getHeaders());
|
||||
return destination != null ? matcher.extractUriTemplateVariables(pattern, destination)
|
||||
: Collections.emptyMap();
|
||||
public Map<String, String> extractPathVariables(Message<?> message) {
|
||||
final String destination = SimpMessageHeaderAccessor.getDestination(message.getHeaders());
|
||||
return destination != null ? matcher.extractUriTemplateVariables(pattern, destination) : Collections.emptyMap();
|
||||
}
|
||||
|
||||
public MessageMatcher<Object> getMessageTypeMatcher() {
|
||||
@@ -144,44 +136,37 @@ public final class SimpDestinationMessageMatcher implements MessageMatcher<Objec
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SimpDestinationMessageMatcher [matcher=" + matcher
|
||||
+ ", messageTypeMatcher=" + messageTypeMatcher + ", pattern=" + pattern
|
||||
+ "]";
|
||||
return "SimpDestinationMessageMatcher [matcher=" + matcher + ", messageTypeMatcher=" + messageTypeMatcher
|
||||
+ ", pattern=" + pattern + "]";
|
||||
}
|
||||
|
||||
private boolean isTypeWithDestination(SimpMessageType type) {
|
||||
if (type == null) {
|
||||
return true;
|
||||
}
|
||||
return SimpMessageType.MESSAGE.equals(type)
|
||||
|| SimpMessageType.SUBSCRIBE.equals(type);
|
||||
return SimpMessageType.MESSAGE.equals(type) || SimpMessageType.SUBSCRIBE.equals(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Creates a new instance with the specified pattern,
|
||||
* {@code SimpMessageType.SUBSCRIBE}, and {@link PathMatcher}.
|
||||
*
|
||||
* @param pattern the pattern to use
|
||||
* @param matcher the {@link PathMatcher} to use.
|
||||
*/
|
||||
public static SimpDestinationMessageMatcher createSubscribeMatcher(String pattern,
|
||||
PathMatcher matcher) {
|
||||
return new SimpDestinationMessageMatcher(pattern, SimpMessageType.SUBSCRIBE,
|
||||
matcher);
|
||||
public static SimpDestinationMessageMatcher createSubscribeMatcher(String pattern, PathMatcher matcher) {
|
||||
return new SimpDestinationMessageMatcher(pattern, SimpMessageType.SUBSCRIBE, matcher);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Creates a new instance with the specified pattern, {@code SimpMessageType.MESSAGE},
|
||||
* and {@link PathMatcher}.
|
||||
*
|
||||
* @param pattern the pattern to use
|
||||
* @param matcher the {@link PathMatcher} to use.
|
||||
*/
|
||||
public static SimpDestinationMessageMatcher createMessageMatcher(String pattern,
|
||||
PathMatcher matcher) {
|
||||
return new SimpDestinationMessageMatcher(pattern, SimpMessageType.MESSAGE,
|
||||
matcher);
|
||||
public static SimpDestinationMessageMatcher createMessageMatcher(String pattern, PathMatcher matcher) {
|
||||
return new SimpDestinationMessageMatcher(pattern, SimpMessageType.MESSAGE, matcher);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+2
-1
@@ -31,11 +31,11 @@ import org.springframework.util.ObjectUtils;
|
||||
*
|
||||
*/
|
||||
public class SimpMessageTypeMatcher implements MessageMatcher<Object> {
|
||||
|
||||
private final SimpMessageType typeToMatch;
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
*
|
||||
* @param typeToMatch the {@link SimpMessageType} that will result in a match. Cannot
|
||||
* be null.
|
||||
*/
|
||||
@@ -75,4 +75,5 @@ public class SimpMessageTypeMatcher implements MessageMatcher<Object> {
|
||||
public String toString() {
|
||||
return "SimpMessageTypeMatcher [typeToMatch=" + typeToMatch + "]";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+4
-4
@@ -37,8 +37,8 @@ import org.springframework.security.web.csrf.MissingCsrfTokenException;
|
||||
* @since 4.0
|
||||
*/
|
||||
public final class CsrfChannelInterceptor extends ChannelInterceptorAdapter {
|
||||
private final MessageMatcher<Object> matcher = new SimpMessageTypeMatcher(
|
||||
SimpMessageType.CONNECT);
|
||||
|
||||
private final MessageMatcher<Object> matcher = new SimpMessageTypeMatcher(SimpMessageType.CONNECT);
|
||||
|
||||
@Override
|
||||
public Message<?> preSend(Message<?> message, MessageChannel channel) {
|
||||
@@ -46,8 +46,7 @@ public final class CsrfChannelInterceptor extends ChannelInterceptorAdapter {
|
||||
return message;
|
||||
}
|
||||
|
||||
Map<String, Object> sessionAttributes = SimpMessageHeaderAccessor
|
||||
.getSessionAttributes(message.getHeaders());
|
||||
Map<String, Object> sessionAttributes = SimpMessageHeaderAccessor.getSessionAttributes(message.getHeaders());
|
||||
CsrfToken expectedToken = sessionAttributes == null ? null
|
||||
: (CsrfToken) sessionAttributes.get(CsrfToken.class.getName());
|
||||
|
||||
@@ -64,4 +63,5 @@ public final class CsrfChannelInterceptor extends ChannelInterceptorAdapter {
|
||||
}
|
||||
throw new InvalidCsrfTokenException(expectedToken, actualTokenValue);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+5
-6
@@ -36,11 +36,9 @@ import org.springframework.web.socket.server.HandshakeInterceptor;
|
||||
*/
|
||||
public final class CsrfTokenHandshakeInterceptor implements HandshakeInterceptor {
|
||||
|
||||
public boolean beforeHandshake(ServerHttpRequest request,
|
||||
ServerHttpResponse response, WebSocketHandler wsHandler,
|
||||
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
|
||||
Map<String, Object> attributes) {
|
||||
HttpServletRequest httpRequest = ((ServletServerHttpRequest) request)
|
||||
.getServletRequest();
|
||||
HttpServletRequest httpRequest = ((ServletServerHttpRequest) request).getServletRequest();
|
||||
CsrfToken token = (CsrfToken) httpRequest.getAttribute(CsrfToken.class.getName());
|
||||
if (token == null) {
|
||||
return true;
|
||||
@@ -49,7 +47,8 @@ public final class CsrfTokenHandshakeInterceptor implements HandshakeInterceptor
|
||||
return true;
|
||||
}
|
||||
|
||||
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response,
|
||||
WebSocketHandler wsHandler, Exception exception) {
|
||||
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
|
||||
Exception exception) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+12
-18
@@ -38,8 +38,10 @@ import org.springframework.security.core.authority.AuthorityUtils;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class DefaultMessageSecurityExpressionHandlerTests {
|
||||
|
||||
@Mock
|
||||
AuthenticationTrustResolver trustResolver;
|
||||
|
||||
@Mock
|
||||
PermissionEvaluator permissionEvaluator;
|
||||
|
||||
@@ -61,10 +63,8 @@ public class DefaultMessageSecurityExpressionHandlerTests {
|
||||
// SEC-2705
|
||||
@Test
|
||||
public void trustResolverPopulated() {
|
||||
EvaluationContext context = handler.createEvaluationContext(authentication,
|
||||
message);
|
||||
Expression expression = handler.getExpressionParser().parseExpression(
|
||||
"authenticated");
|
||||
EvaluationContext context = handler.createEvaluationContext(authentication, message);
|
||||
Expression expression = handler.getExpressionParser().parseExpression("authenticated");
|
||||
|
||||
assertThat(ExpressionUtils.evaluateAsBoolean(expression, context)).isFalse();
|
||||
}
|
||||
@@ -77,10 +77,8 @@ public class DefaultMessageSecurityExpressionHandlerTests {
|
||||
@Test
|
||||
public void trustResolverCustom() {
|
||||
handler.setTrustResolver(trustResolver);
|
||||
EvaluationContext context = handler.createEvaluationContext(authentication,
|
||||
message);
|
||||
Expression expression = handler.getExpressionParser().parseExpression(
|
||||
"authenticated");
|
||||
EvaluationContext context = handler.createEvaluationContext(authentication, message);
|
||||
Expression expression = handler.getExpressionParser().parseExpression("authenticated");
|
||||
when(trustResolver.isAnonymous(authentication)).thenReturn(false);
|
||||
|
||||
assertThat(ExpressionUtils.evaluateAsBoolean(expression, context)).isTrue();
|
||||
@@ -92,10 +90,8 @@ public class DefaultMessageSecurityExpressionHandlerTests {
|
||||
RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl();
|
||||
roleHierarchy.setHierarchy("ROLE_ADMIN > ROLE_USER");
|
||||
handler.setRoleHierarchy(roleHierarchy);
|
||||
EvaluationContext context = handler.createEvaluationContext(authentication,
|
||||
message);
|
||||
Expression expression = handler.getExpressionParser().parseExpression(
|
||||
"hasRole('ROLE_USER')");
|
||||
EvaluationContext context = handler.createEvaluationContext(authentication, message);
|
||||
Expression expression = handler.getExpressionParser().parseExpression("hasRole('ROLE_USER')");
|
||||
|
||||
assertThat(ExpressionUtils.evaluateAsBoolean(expression, context)).isTrue();
|
||||
}
|
||||
@@ -103,13 +99,11 @@ public class DefaultMessageSecurityExpressionHandlerTests {
|
||||
@Test
|
||||
public void permissionEvaluator() {
|
||||
handler.setPermissionEvaluator(permissionEvaluator);
|
||||
EvaluationContext context = handler.createEvaluationContext(authentication,
|
||||
message);
|
||||
Expression expression = handler.getExpressionParser().parseExpression(
|
||||
"hasPermission(message, 'read')");
|
||||
when(permissionEvaluator.hasPermission(authentication, message, "read"))
|
||||
.thenReturn(true);
|
||||
EvaluationContext context = handler.createEvaluationContext(authentication, message);
|
||||
Expression expression = handler.getExpressionParser().parseExpression("hasPermission(message, 'read')");
|
||||
when(permissionEvaluator.hasPermission(authentication, message, "read")).thenReturn(true);
|
||||
|
||||
assertThat(ExpressionUtils.evaluateAsBoolean(expression, context)).isTrue();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+9
-6
@@ -35,12 +35,16 @@ import java.util.LinkedHashMap;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ExpressionBasedMessageSecurityMetadataSourceFactoryTests {
|
||||
|
||||
@Mock
|
||||
MessageMatcher<Object> matcher1;
|
||||
|
||||
@Mock
|
||||
MessageMatcher<Object> matcher2;
|
||||
|
||||
@Mock
|
||||
Message<Object> message;
|
||||
|
||||
@Mock
|
||||
Authentication authentication;
|
||||
|
||||
@@ -83,9 +87,8 @@ public class ExpressionBasedMessageSecurityMetadataSourceFactoryTests {
|
||||
assertThat(attrs).hasSize(1);
|
||||
ConfigAttribute attr = attrs.iterator().next();
|
||||
assertThat(attr).isInstanceOf(MessageExpressionConfigAttribute.class);
|
||||
assertThat(
|
||||
((MessageExpressionConfigAttribute) attr).getAuthorizeExpression()
|
||||
.getValue(rootObject)).isEqualTo(true);
|
||||
assertThat(((MessageExpressionConfigAttribute) attr).getAuthorizeExpression().getValue(rootObject))
|
||||
.isEqualTo(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -97,8 +100,8 @@ public class ExpressionBasedMessageSecurityMetadataSourceFactoryTests {
|
||||
assertThat(attrs).hasSize(1);
|
||||
ConfigAttribute attr = attrs.iterator().next();
|
||||
assertThat(attr).isInstanceOf(MessageExpressionConfigAttribute.class);
|
||||
assertThat(
|
||||
((MessageExpressionConfigAttribute) attr).getAuthorizeExpression()
|
||||
.getValue(rootObject)).isEqualTo(false);
|
||||
assertThat(((MessageExpressionConfigAttribute) attr).getAuthorizeExpression().getValue(rootObject))
|
||||
.isEqualTo(false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+4
-1
@@ -33,6 +33,7 @@ import static org.mockito.Mockito.*;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class MessageExpressionConfigAttributeTests {
|
||||
|
||||
@Mock
|
||||
Expression expression;
|
||||
|
||||
@@ -76,7 +77,8 @@ public class MessageExpressionConfigAttributeTests {
|
||||
@Test
|
||||
public void postProcessContext() {
|
||||
SimpDestinationMessageMatcher matcher = new SimpDestinationMessageMatcher("/topics/{topic}/**");
|
||||
Message<?> message = MessageBuilder.withPayload("M").setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/topics/someTopic/sub1").build();
|
||||
Message<?> message = MessageBuilder.withPayload("M")
|
||||
.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/topics/someTopic/sub1").build();
|
||||
EvaluationContext context = mock(EvaluationContext.class);
|
||||
|
||||
attribute = new MessageExpressionConfigAttribute(expression, matcher);
|
||||
@@ -84,4 +86,5 @@ public class MessageExpressionConfigAttributeTests {
|
||||
|
||||
verify(context).setVariable("topic", "someTopic");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+22
-25
@@ -38,17 +38,24 @@ import static org.springframework.security.access.AccessDecisionVoter.*;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class MessageExpressionVoterTests {
|
||||
|
||||
@Mock
|
||||
Authentication authentication;
|
||||
|
||||
@Mock
|
||||
Message<Object> message;
|
||||
|
||||
Collection<ConfigAttribute> attributes;
|
||||
|
||||
@Mock
|
||||
Expression expression;
|
||||
|
||||
@Mock
|
||||
MessageMatcher<?> matcher;
|
||||
|
||||
@Mock
|
||||
SecurityExpressionHandler<Message> expressionHandler;
|
||||
|
||||
@Mock
|
||||
EvaluationContext evaluationContext;
|
||||
|
||||
@@ -56,33 +63,27 @@ public class MessageExpressionVoterTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
attributes = Arrays
|
||||
.<ConfigAttribute> asList(new MessageExpressionConfigAttribute(expression, matcher));
|
||||
attributes = Arrays.<ConfigAttribute>asList(new MessageExpressionConfigAttribute(expression, matcher));
|
||||
|
||||
voter = new MessageExpressionVoter();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void voteGranted() {
|
||||
when(expression.getValue(any(EvaluationContext.class), eq(Boolean.class)))
|
||||
.thenReturn(true);
|
||||
assertThat(voter.vote(authentication, message, attributes)).isEqualTo(
|
||||
ACCESS_GRANTED);
|
||||
when(expression.getValue(any(EvaluationContext.class), eq(Boolean.class))).thenReturn(true);
|
||||
assertThat(voter.vote(authentication, message, attributes)).isEqualTo(ACCESS_GRANTED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void voteDenied() {
|
||||
when(expression.getValue(any(EvaluationContext.class), eq(Boolean.class)))
|
||||
.thenReturn(false);
|
||||
assertThat(voter.vote(authentication, message, attributes)).isEqualTo(
|
||||
ACCESS_DENIED);
|
||||
when(expression.getValue(any(EvaluationContext.class), eq(Boolean.class))).thenReturn(false);
|
||||
assertThat(voter.vote(authentication, message, attributes)).isEqualTo(ACCESS_DENIED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void voteAbstain() {
|
||||
attributes = Arrays.<ConfigAttribute> asList(new SecurityConfig("ROLE_USER"));
|
||||
assertThat(voter.vote(authentication, message, attributes)).isEqualTo(
|
||||
ACCESS_ABSTAIN);
|
||||
attributes = Arrays.<ConfigAttribute>asList(new SecurityConfig("ROLE_USER"));
|
||||
assertThat(voter.vote(authentication, message, attributes)).isEqualTo(ACCESS_ABSTAIN);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -102,8 +103,7 @@ public class MessageExpressionVoterTests {
|
||||
|
||||
@Test
|
||||
public void supportsMessageExpressionConfigAttributeTrue() {
|
||||
assertThat(voter.supports(new MessageExpressionConfigAttribute(expression, matcher)))
|
||||
.isTrue();
|
||||
assertThat(voter.supports(new MessageExpressionConfigAttribute(expression, matcher))).isTrue();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@@ -114,29 +114,26 @@ public class MessageExpressionVoterTests {
|
||||
@Test
|
||||
public void customExpressionHandler() {
|
||||
voter.setExpressionHandler(expressionHandler);
|
||||
when(expressionHandler.createEvaluationContext(authentication, message))
|
||||
.thenReturn(evaluationContext);
|
||||
when(expressionHandler.createEvaluationContext(authentication, message)).thenReturn(evaluationContext);
|
||||
when(expression.getValue(evaluationContext, Boolean.class)).thenReturn(true);
|
||||
|
||||
assertThat(voter.vote(authentication, message, attributes)).isEqualTo(
|
||||
ACCESS_GRANTED);
|
||||
assertThat(voter.vote(authentication, message, attributes)).isEqualTo(ACCESS_GRANTED);
|
||||
|
||||
verify(expressionHandler).createEvaluationContext(authentication, message);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postProcessEvaluationContext(){
|
||||
public void postProcessEvaluationContext() {
|
||||
final MessageExpressionConfigAttribute configAttribute = mock(MessageExpressionConfigAttribute.class);
|
||||
voter.setExpressionHandler(expressionHandler);
|
||||
when(expressionHandler.createEvaluationContext(authentication, message)).thenReturn(evaluationContext);
|
||||
when(configAttribute.getAuthorizeExpression()).thenReturn(expression);
|
||||
attributes = Arrays.<ConfigAttribute> asList(configAttribute);
|
||||
attributes = Arrays.<ConfigAttribute>asList(configAttribute);
|
||||
when(configAttribute.postProcess(evaluationContext, message)).thenReturn(evaluationContext);
|
||||
when(expression.getValue(any(EvaluationContext.class), eq(Boolean.class)))
|
||||
.thenReturn(true);
|
||||
when(expression.getValue(any(EvaluationContext.class), eq(Boolean.class))).thenReturn(true);
|
||||
|
||||
assertThat(voter.vote(authentication, message, attributes)).isEqualTo(
|
||||
ACCESS_GRANTED);
|
||||
assertThat(voter.vote(authentication, message, attributes)).isEqualTo(ACCESS_GRANTED);
|
||||
verify(configAttribute).postProcess(evaluationContext, message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+16
-17
@@ -44,16 +44,22 @@ import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ChannelSecurityInterceptorTests {
|
||||
|
||||
@Mock
|
||||
Message<Object> message;
|
||||
|
||||
@Mock
|
||||
MessageChannel channel;
|
||||
|
||||
@Mock
|
||||
MessageSecurityMetadataSource source;
|
||||
|
||||
@Mock
|
||||
AccessDecisionManager accessDecisionManager;
|
||||
|
||||
@Mock
|
||||
RunAsManager runAsManager;
|
||||
|
||||
@Mock
|
||||
Authentication runAs;
|
||||
|
||||
@@ -65,7 +71,7 @@ public class ChannelSecurityInterceptorTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
attrs = Arrays.<ConfigAttribute> asList(new SecurityConfig("ROLE_USER"));
|
||||
attrs = Arrays.<ConfigAttribute>asList(new SecurityConfig("ROLE_USER"));
|
||||
interceptor = new ChannelSecurityInterceptor(source);
|
||||
interceptor.setAccessDecisionManager(accessDecisionManager);
|
||||
interceptor.setRunAsManager(runAsManager);
|
||||
@@ -111,8 +117,8 @@ public class ChannelSecurityInterceptorTests {
|
||||
@Test(expected = AccessDeniedException.class)
|
||||
public void preSendDeny() {
|
||||
when(source.getAttributes(message)).thenReturn(attrs);
|
||||
doThrow(new AccessDeniedException("")).when(accessDecisionManager).decide(
|
||||
any(Authentication.class), eq(message), eq(attrs));
|
||||
doThrow(new AccessDeniedException("")).when(accessDecisionManager).decide(any(Authentication.class),
|
||||
eq(message), eq(attrs));
|
||||
|
||||
interceptor.preSend(message, channel);
|
||||
}
|
||||
@@ -121,19 +127,15 @@ public class ChannelSecurityInterceptorTests {
|
||||
@Test
|
||||
public void preSendPostSendRunAs() {
|
||||
when(source.getAttributes(message)).thenReturn(attrs);
|
||||
when(
|
||||
runAsManager.buildRunAs(any(Authentication.class), any(),
|
||||
any(Collection.class))).thenReturn(runAs);
|
||||
when(runAsManager.buildRunAs(any(Authentication.class), any(), any(Collection.class))).thenReturn(runAs);
|
||||
|
||||
Message<?> preSend = interceptor.preSend(message, channel);
|
||||
|
||||
assertThat(SecurityContextHolder.getContext().getAuthentication())
|
||||
.isSameAs(runAs);
|
||||
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(runAs);
|
||||
|
||||
interceptor.postSend(preSend, channel, true);
|
||||
|
||||
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(
|
||||
originalAuth);
|
||||
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(originalAuth);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -145,19 +147,15 @@ public class ChannelSecurityInterceptorTests {
|
||||
@Test
|
||||
public void preSendFinallySendRunAs() {
|
||||
when(source.getAttributes(message)).thenReturn(attrs);
|
||||
when(
|
||||
runAsManager.buildRunAs(any(Authentication.class), any(),
|
||||
any(Collection.class))).thenReturn(runAs);
|
||||
when(runAsManager.buildRunAs(any(Authentication.class), any(), any(Collection.class))).thenReturn(runAs);
|
||||
|
||||
Message<?> preSend = interceptor.preSend(message, channel);
|
||||
|
||||
assertThat(SecurityContextHolder.getContext().getAuthentication())
|
||||
.isSameAs(runAs);
|
||||
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(runAs);
|
||||
|
||||
interceptor.afterSendCompletion(preSend, channel, true, new RuntimeException());
|
||||
|
||||
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(
|
||||
originalAuth);
|
||||
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(originalAuth);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -174,4 +172,5 @@ public class ChannelSecurityInterceptorTests {
|
||||
public void afterReceiveCompletionNullExceptionNoExceptionThrown() {
|
||||
interceptor.afterReceiveCompletion(message, channel, null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+7
-2
@@ -35,12 +35,16 @@ import static org.powermock.api.mockito.PowerMockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class DefaultMessageSecurityMetadataSourceTests {
|
||||
|
||||
@Mock
|
||||
MessageMatcher<Object> matcher1;
|
||||
|
||||
@Mock
|
||||
MessageMatcher<Object> matcher2;
|
||||
|
||||
@Mock
|
||||
Message<?> message;
|
||||
|
||||
@Mock
|
||||
Authentication authentication;
|
||||
|
||||
@@ -55,8 +59,8 @@ public class DefaultMessageSecurityMetadataSourceTests {
|
||||
@Before
|
||||
public void setup() {
|
||||
messageMap = new LinkedHashMap<>();
|
||||
messageMap.put(matcher1, Arrays.<ConfigAttribute> asList(config1));
|
||||
messageMap.put(matcher2, Arrays.<ConfigAttribute> asList(config2));
|
||||
messageMap.put(matcher1, Arrays.<ConfigAttribute>asList(config1));
|
||||
messageMap.put(matcher2, Arrays.<ConfigAttribute>asList(config2));
|
||||
|
||||
source = new DefaultMessageSecurityMetadataSource(messageMap);
|
||||
}
|
||||
@@ -94,4 +98,5 @@ public class DefaultMessageSecurityMetadataSourceTests {
|
||||
public void supportsTrue() {
|
||||
assertThat(source.supports(Message.class)).isTrue();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+27
-31
@@ -40,7 +40,9 @@ import org.springframework.util.ReflectionUtils;
|
||||
*
|
||||
*/
|
||||
public class AuthenticationPrincipalArgumentResolverTests {
|
||||
|
||||
private Object expectedPrincipal;
|
||||
|
||||
private AuthenticationPrincipalArgumentResolver resolver;
|
||||
|
||||
@Before
|
||||
@@ -82,38 +84,32 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
||||
@Test
|
||||
public void resolveArgumentString() throws Exception {
|
||||
setAuthenticationPrincipal("john");
|
||||
assertThat(resolver.resolveArgument(showUserAnnotationString(), null)).isEqualTo(
|
||||
expectedPrincipal);
|
||||
assertThat(resolver.resolveArgument(showUserAnnotationString(), null)).isEqualTo(expectedPrincipal);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveArgumentPrincipalStringOnObject() throws Exception {
|
||||
setAuthenticationPrincipal("john");
|
||||
assertThat(resolver.resolveArgument(showUserAnnotationObject(), null)).isEqualTo(
|
||||
expectedPrincipal);
|
||||
assertThat(resolver.resolveArgument(showUserAnnotationObject(), null)).isEqualTo(expectedPrincipal);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveArgumentUserDetails() throws Exception {
|
||||
setAuthenticationPrincipal(new User("user", "password",
|
||||
AuthorityUtils.createAuthorityList("ROLE_USER")));
|
||||
assertThat(resolver.resolveArgument(showUserAnnotationUserDetails(), null))
|
||||
.isEqualTo(expectedPrincipal);
|
||||
setAuthenticationPrincipal(new User("user", "password", AuthorityUtils.createAuthorityList("ROLE_USER")));
|
||||
assertThat(resolver.resolveArgument(showUserAnnotationUserDetails(), null)).isEqualTo(expectedPrincipal);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveArgumentCustomUserPrincipal() throws Exception {
|
||||
setAuthenticationPrincipal(new CustomUserPrincipal());
|
||||
assertThat(
|
||||
resolver.resolveArgument(showUserAnnotationCustomUserPrincipal(), null))
|
||||
assertThat(resolver.resolveArgument(showUserAnnotationCustomUserPrincipal(), null))
|
||||
.isEqualTo(expectedPrincipal);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveArgumentCustomAnnotation() throws Exception {
|
||||
setAuthenticationPrincipal(new CustomUserPrincipal());
|
||||
assertThat(resolver.resolveArgument(showUserCustomAnnotation(), null)).isEqualTo(
|
||||
expectedPrincipal);
|
||||
assertThat(resolver.resolveArgument(showUserCustomAnnotation(), null)).isEqualTo(expectedPrincipal);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -121,8 +117,7 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
||||
CustomUserPrincipal principal = new CustomUserPrincipal();
|
||||
setAuthenticationPrincipal(principal);
|
||||
this.expectedPrincipal = principal.property;
|
||||
assertThat(this.resolver.resolveArgument(showUserSpel(), null))
|
||||
.isEqualTo(this.expectedPrincipal);
|
||||
assertThat(this.resolver.resolveArgument(showUserSpel(), null)).isEqualTo(this.expectedPrincipal);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -155,8 +150,7 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
||||
@Test
|
||||
public void resolveArgumentObject() throws Exception {
|
||||
setAuthenticationPrincipal(new Object());
|
||||
assertThat(resolver.resolveArgument(showUserAnnotationObject(), null)).isEqualTo(
|
||||
expectedPrincipal);
|
||||
assertThat(resolver.resolveArgument(showUserAnnotationObject(), null)).isEqualTo(expectedPrincipal);
|
||||
}
|
||||
|
||||
private MethodParameter showUserNoAnnotation() {
|
||||
@@ -172,8 +166,7 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
||||
}
|
||||
|
||||
private MethodParameter showUserAnnotationCurrentUserErrorOnInvalidType() {
|
||||
return getMethodParameter("showUserAnnotationCurrentUserErrorOnInvalidType",
|
||||
String.class);
|
||||
return getMethodParameter("showUserAnnotationCurrentUserErrorOnInvalidType", String.class);
|
||||
}
|
||||
|
||||
private MethodParameter showUserAnnotationUserDetails() {
|
||||
@@ -201,8 +194,7 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
||||
}
|
||||
|
||||
private MethodParameter getMethodParameter(String methodName, Class<?>... paramTypes) {
|
||||
Method method = ReflectionUtils.findMethod(TestController.class, methodName,
|
||||
paramTypes);
|
||||
Method method = ReflectionUtils.findMethod(TestController.class, methodName, paramTypes);
|
||||
return new MethodParameter(method, 0);
|
||||
}
|
||||
|
||||
@@ -210,15 +202,18 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@AuthenticationPrincipal
|
||||
static @interface CurrentUser {
|
||||
|
||||
}
|
||||
|
||||
@Target({ ElementType.PARAMETER })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@AuthenticationPrincipal(errorOnInvalidType = true)
|
||||
static @interface CurrentUserErrorOnInvalidType {
|
||||
|
||||
}
|
||||
|
||||
public static class TestController {
|
||||
|
||||
public void showUserNoAnnotation(String user) {
|
||||
}
|
||||
|
||||
@@ -229,8 +224,7 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
||||
@AuthenticationPrincipal(errorOnInvalidType = true) String user) {
|
||||
}
|
||||
|
||||
public void showUserAnnotationCurrentUserErrorOnInvalidType(
|
||||
@CurrentUserErrorOnInvalidType String user) {
|
||||
public void showUserAnnotationCurrentUserErrorOnInvalidType(@CurrentUserErrorOnInvalidType String user) {
|
||||
}
|
||||
|
||||
public void showUserAnnotation(@AuthenticationPrincipal UserDetails user) {
|
||||
@@ -245,20 +239,23 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
||||
public void showUserAnnotation(@AuthenticationPrincipal Object user) {
|
||||
}
|
||||
|
||||
public void showUserSpel(
|
||||
@AuthenticationPrincipal(expression = "property") String user) {
|
||||
public void showUserSpel(@AuthenticationPrincipal(expression = "property") String user) {
|
||||
}
|
||||
|
||||
public void showUserSpelCopy(
|
||||
@AuthenticationPrincipal(expression = "new org.springframework.security.messaging.context.AuthenticationPrincipalArgumentResolverTests$CopyUserPrincipal(#this)") CopyUserPrincipal user) {
|
||||
public void showUserSpelCopy(@AuthenticationPrincipal(
|
||||
expression = "new org.springframework.security.messaging.context.AuthenticationPrincipalArgumentResolverTests$CopyUserPrincipal(#this)") CopyUserPrincipal user) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class CustomUserPrincipal {
|
||||
|
||||
public final String property = "property";
|
||||
|
||||
}
|
||||
|
||||
public static class CopyUserPrincipal {
|
||||
|
||||
public final String property;
|
||||
|
||||
public CopyUserPrincipal(String property) {
|
||||
@@ -273,8 +270,7 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result
|
||||
+ ((this.property == null) ? 0 : this.property.hashCode());
|
||||
result = prime * result + ((this.property == null) ? 0 : this.property.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -300,13 +296,13 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void setAuthenticationPrincipal(Object principal) {
|
||||
this.expectedPrincipal = principal;
|
||||
SecurityContextHolder.getContext()
|
||||
.setAuthentication(
|
||||
new TestingAuthenticationToken(expectedPrincipal, "password",
|
||||
"ROLE_USER"));
|
||||
.setAuthentication(new TestingAuthenticationToken(expectedPrincipal, "password", "ROLE_USER"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+22
-32
@@ -39,10 +39,13 @@ import static org.springframework.security.core.context.SecurityContextHolder.*;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class SecurityContextChannelInterceptorTests {
|
||||
|
||||
@Mock
|
||||
MessageChannel channel;
|
||||
|
||||
@Mock
|
||||
MessageHandler handler;
|
||||
|
||||
@Mock
|
||||
Principal principal;
|
||||
|
||||
@@ -82,8 +85,7 @@ public class SecurityContextChannelInterceptorTests {
|
||||
|
||||
interceptor.preSend(messageBuilder.build(), channel);
|
||||
|
||||
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(
|
||||
authentication);
|
||||
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(authentication);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -92,8 +94,7 @@ public class SecurityContextChannelInterceptorTests {
|
||||
|
||||
interceptor.preSend(messageBuilder.build(), channel);
|
||||
|
||||
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(
|
||||
authentication);
|
||||
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(authentication);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@@ -103,8 +104,8 @@ public class SecurityContextChannelInterceptorTests {
|
||||
|
||||
@Test
|
||||
public void preSendUsesCustomAnonymous() {
|
||||
expectedAnonymous = new AnonymousAuthenticationToken("customKey",
|
||||
"customAnonymous", AuthorityUtils.createAuthorityList("ROLE_CUSTOM"));
|
||||
expectedAnonymous = new AnonymousAuthenticationToken("customKey", "customAnonymous",
|
||||
AuthorityUtils.createAuthorityList("ROLE_CUSTOM"));
|
||||
interceptor.setAnonymousAuthentication(expectedAnonymous);
|
||||
|
||||
interceptor.preSend(messageBuilder.build(), channel);
|
||||
@@ -160,8 +161,7 @@ public class SecurityContextChannelInterceptorTests {
|
||||
|
||||
interceptor.beforeHandle(messageBuilder.build(), channel, handler);
|
||||
|
||||
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(
|
||||
authentication);
|
||||
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(authentication);
|
||||
}
|
||||
|
||||
// SEC-2845
|
||||
@@ -201,20 +201,17 @@ public class SecurityContextChannelInterceptorTests {
|
||||
// SEC-2829
|
||||
@Test
|
||||
public void restoresOriginalContext() {
|
||||
TestingAuthenticationToken original = new TestingAuthenticationToken("original",
|
||||
"original", "ROLE_USER");
|
||||
TestingAuthenticationToken original = new TestingAuthenticationToken("original", "original", "ROLE_USER");
|
||||
SecurityContextHolder.getContext().setAuthentication(original);
|
||||
|
||||
messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, authentication);
|
||||
interceptor.beforeHandle(messageBuilder.build(), channel, handler);
|
||||
|
||||
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(
|
||||
authentication);
|
||||
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(authentication);
|
||||
|
||||
interceptor.afterMessageHandled(messageBuilder.build(), channel, handler, null);
|
||||
|
||||
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(
|
||||
original);
|
||||
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(original);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -223,48 +220,41 @@ public class SecurityContextChannelInterceptorTests {
|
||||
*/
|
||||
@Test
|
||||
public void restoresOriginalContextNestedThreeDeep() {
|
||||
AnonymousAuthenticationToken anonymous = new AnonymousAuthenticationToken("key",
|
||||
"anonymous", AuthorityUtils.createAuthorityList("ROLE_USER"));
|
||||
AnonymousAuthenticationToken anonymous = new AnonymousAuthenticationToken("key", "anonymous",
|
||||
AuthorityUtils.createAuthorityList("ROLE_USER"));
|
||||
|
||||
TestingAuthenticationToken origional = new TestingAuthenticationToken("original",
|
||||
"origional", "ROLE_USER");
|
||||
TestingAuthenticationToken origional = new TestingAuthenticationToken("original", "origional", "ROLE_USER");
|
||||
SecurityContextHolder.getContext().setAuthentication(origional);
|
||||
|
||||
messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, authentication);
|
||||
interceptor.beforeHandle(messageBuilder.build(), channel, handler);
|
||||
|
||||
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(
|
||||
authentication);
|
||||
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(authentication);
|
||||
|
||||
// start send websocket
|
||||
messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, null);
|
||||
interceptor.beforeHandle(messageBuilder.build(), channel, handler);
|
||||
|
||||
assertThat(SecurityContextHolder.getContext().getAuthentication().getName())
|
||||
.isEqualTo(anonymous.getName());
|
||||
assertThat(SecurityContextHolder.getContext().getAuthentication().getName()).isEqualTo(anonymous.getName());
|
||||
|
||||
interceptor.afterMessageHandled(messageBuilder.build(), channel, handler, null);
|
||||
|
||||
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(
|
||||
authentication);
|
||||
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(authentication);
|
||||
// end send websocket
|
||||
|
||||
interceptor.afterMessageHandled(messageBuilder.build(), channel, handler, null);
|
||||
|
||||
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(
|
||||
origional);
|
||||
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(origional);
|
||||
}
|
||||
|
||||
private void assertAnonymous() {
|
||||
Authentication currentAuthentication = SecurityContextHolder.getContext()
|
||||
.getAuthentication();
|
||||
assertThat(currentAuthentication)
|
||||
.isInstanceOf(AnonymousAuthenticationToken.class);
|
||||
Authentication currentAuthentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
assertThat(currentAuthentication).isInstanceOf(AnonymousAuthenticationToken.class);
|
||||
|
||||
AnonymousAuthenticationToken anonymous = (AnonymousAuthenticationToken) currentAuthentication;
|
||||
assertThat(anonymous.getName()).isEqualTo(expectedAnonymous.getName());
|
||||
assertThat(anonymous.getAuthorities()).containsOnlyElementsOf(
|
||||
expectedAnonymous.getAuthorities());
|
||||
assertThat(anonymous.getAuthorities()).containsOnlyElementsOf(expectedAnonymous.getAuthorities());
|
||||
assertThat(anonymous.getKeyHash()).isEqualTo(expectedAnonymous.getKeyHash());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+75
-89
@@ -57,27 +57,29 @@ import org.springframework.util.ReflectionUtils;
|
||||
import static java.util.stream.Collectors.joining;
|
||||
|
||||
/**
|
||||
* NOTE: This class is a replica of the same class in spring-web so it can
|
||||
* be used for tests in spring-messaging.
|
||||
* NOTE: This class is a replica of the same class in spring-web so it can be used for
|
||||
* tests in spring-messaging.
|
||||
*
|
||||
* <p>Convenience class to resolve method parameters from hints.
|
||||
* <p>
|
||||
* Convenience class to resolve method parameters from hints.
|
||||
*
|
||||
* <h1>Background</h1>
|
||||
*
|
||||
* <p>When testing annotated methods we create test classes such as
|
||||
* "TestController" with a diverse range of method signatures representing
|
||||
* supported annotations and argument types. It becomes challenging to use
|
||||
* naming strategies to keep track of methods and arguments especially in
|
||||
* combination with variables for reflection metadata.
|
||||
* <p>
|
||||
* When testing annotated methods we create test classes such as "TestController" with a
|
||||
* diverse range of method signatures representing supported annotations and argument
|
||||
* types. It becomes challenging to use naming strategies to keep track of methods and
|
||||
* arguments especially in combination with variables for reflection metadata.
|
||||
*
|
||||
* <p>The idea with {@link ResolvableMethod} is NOT to rely on naming techniques
|
||||
* but to use hints to zero in on method parameters. Such hints can be strongly
|
||||
* typed and explicit about what is being tested.
|
||||
* <p>
|
||||
* The idea with {@link ResolvableMethod} is NOT to rely on naming techniques but to use
|
||||
* hints to zero in on method parameters. Such hints can be strongly typed and explicit
|
||||
* about what is being tested.
|
||||
*
|
||||
* <h2>1. Declared Return Type</h2>
|
||||
*
|
||||
* When testing return types it's likely to have many methods with a unique
|
||||
* return type, possibly with or without an annotation.
|
||||
* When testing return types it's likely to have many methods with a unique return type,
|
||||
* possibly with or without an annotation.
|
||||
*
|
||||
* <pre>
|
||||
* import static org.springframework.web.method.ResolvableMethod.on;
|
||||
@@ -100,8 +102,8 @@ import static java.util.stream.Collectors.joining;
|
||||
*
|
||||
* <h2>2. Method Arguments</h2>
|
||||
*
|
||||
* When testing method arguments it's more likely to have one or a small number
|
||||
* of methods with a wide array of argument types and parameter annotations.
|
||||
* When testing method arguments it's more likely to have one or a small number of methods
|
||||
* with a wide array of argument types and parameter annotations.
|
||||
*
|
||||
* <pre>
|
||||
* import static org.springframework.web.method.MvcAnnotationPredicates.requestParam;
|
||||
@@ -136,16 +138,13 @@ public class ResolvableMethod {
|
||||
// Matches ValueConstants.DEFAULT_NONE (spring-web and spring-messaging)
|
||||
private static final String DEFAULT_VALUE_NONE = "\n\t\t\n\t\t\n\uE000\uE001\uE002\n\t\t\t\t\n";
|
||||
|
||||
|
||||
private final Method method;
|
||||
|
||||
|
||||
private ResolvableMethod(Method method) {
|
||||
Assert.notNull(method, "'method' is required");
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the resolved method.
|
||||
*/
|
||||
@@ -188,8 +187,8 @@ public class ResolvableMethod {
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter on method arguments with annotation.
|
||||
* See {@link org.springframework.web.method.MvcAnnotationPredicates}.
|
||||
* Filter on method arguments with annotation. See
|
||||
* {@link org.springframework.web.method.MvcAnnotationPredicates}.
|
||||
*/
|
||||
@SafeVarargs
|
||||
public final ArgResolver annot(Predicate<MethodParameter>... filter) {
|
||||
@@ -210,25 +209,21 @@ public class ResolvableMethod {
|
||||
return new ArgResolver().annotNotPresent(annotationTypes);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ResolvableMethod=" + formatMethod();
|
||||
}
|
||||
|
||||
|
||||
private String formatMethod() {
|
||||
return (method().getName() +
|
||||
Arrays.stream(this.method.getParameters())
|
||||
.map(this::formatParameter)
|
||||
.collect(joining(",\n\t", "(\n\t", "\n)")));
|
||||
return (method().getName() + Arrays.stream(this.method.getParameters()).map(this::formatParameter)
|
||||
.collect(joining(",\n\t", "(\n\t", "\n)")));
|
||||
}
|
||||
|
||||
private String formatParameter(Parameter param) {
|
||||
Annotation[] anns = param.getAnnotations();
|
||||
return (anns.length > 0 ?
|
||||
Arrays.stream(anns).map(this::formatAnnotation).collect(joining(",", "[", "]")) + " " + param :
|
||||
param.toString());
|
||||
return (anns.length > 0
|
||||
? Arrays.stream(anns).map(this::formatAnnotation).collect(joining(",", "[", "]")) + " " + param
|
||||
: param.toString());
|
||||
}
|
||||
|
||||
private String formatAnnotation(Annotation annotation) {
|
||||
@@ -242,8 +237,8 @@ public class ResolvableMethod {
|
||||
}
|
||||
|
||||
private static ResolvableType toResolvableType(Class<?> type, Class<?>... generics) {
|
||||
return (ObjectUtils.isEmpty(generics) ? ResolvableType.forClass(type) :
|
||||
ResolvableType.forClassWithGenerics(type, generics));
|
||||
return (ObjectUtils.isEmpty(generics) ? ResolvableType.forClass(type)
|
||||
: ResolvableType.forClassWithGenerics(type, generics));
|
||||
}
|
||||
|
||||
private static ResolvableType toResolvableType(Class<?> type, ResolvableType generic, ResolvableType... generics) {
|
||||
@@ -253,7 +248,6 @@ public class ResolvableMethod {
|
||||
return ResolvableType.forClassWithGenerics(type, genericTypes);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a {@code ResolvableMethod} builder for the given handler class.
|
||||
*/
|
||||
@@ -261,7 +255,6 @@ public class ResolvableMethod {
|
||||
return new Builder<>(objectClass);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Builder for {@code ResolvableMethod}.
|
||||
*/
|
||||
@@ -271,13 +264,11 @@ public class ResolvableMethod {
|
||||
|
||||
private final List<Predicate<Method>> filters = new ArrayList<>(4);
|
||||
|
||||
|
||||
private Builder(Class<?> objectClass) {
|
||||
Assert.notNull(objectClass, "Class must not be null");
|
||||
this.objectClass = objectClass;
|
||||
}
|
||||
|
||||
|
||||
private void addFilter(String message, Predicate<Method> filter) {
|
||||
this.filters.add(new LabeledPredicate<>(message, filter));
|
||||
}
|
||||
@@ -294,15 +285,14 @@ public class ResolvableMethod {
|
||||
* Filter on methods with the given parameter types.
|
||||
*/
|
||||
public Builder<T> argTypes(Class<?>... argTypes) {
|
||||
addFilter("argTypes=" + Arrays.toString(argTypes), method ->
|
||||
ObjectUtils.isEmpty(argTypes) ? method.getParameterCount() == 0 :
|
||||
Arrays.equals(method.getParameterTypes(), argTypes));
|
||||
addFilter("argTypes=" + Arrays.toString(argTypes), method -> ObjectUtils.isEmpty(argTypes)
|
||||
? method.getParameterCount() == 0 : Arrays.equals(method.getParameterTypes(), argTypes));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter on annotated methods.
|
||||
* See {@link org.springframework.web.method.MvcAnnotationPredicates}.
|
||||
* Filter on annotated methods. See
|
||||
* {@link org.springframework.web.method.MvcAnnotationPredicates}.
|
||||
*/
|
||||
@SafeVarargs
|
||||
public final Builder<T> annot(Predicate<Method>... filters) {
|
||||
@@ -312,15 +302,14 @@ public class ResolvableMethod {
|
||||
|
||||
/**
|
||||
* Filter on methods annotated with the given annotation type.
|
||||
* @see #annot(Predicate[])
|
||||
* See {@link org.springframework.web.method.MvcAnnotationPredicates}.
|
||||
* @see #annot(Predicate[]) See
|
||||
* {@link org.springframework.web.method.MvcAnnotationPredicates}.
|
||||
*/
|
||||
@SafeVarargs
|
||||
public final Builder<T> annotPresent(Class<? extends Annotation>... annotationTypes) {
|
||||
String message = "annotationPresent=" + Arrays.toString(annotationTypes);
|
||||
addFilter(message, method ->
|
||||
Arrays.stream(annotationTypes).allMatch(annotType ->
|
||||
AnnotatedElementUtils.findMergedAnnotation(method, annotType) != null));
|
||||
addFilter(message, method -> Arrays.stream(annotationTypes)
|
||||
.allMatch(annotType -> AnnotatedElementUtils.findMergedAnnotation(method, annotType) != null));
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -332,8 +321,8 @@ public class ResolvableMethod {
|
||||
String message = "annotationNotPresent=" + Arrays.toString(annotationTypes);
|
||||
addFilter(message, method -> {
|
||||
if (annotationTypes.length != 0) {
|
||||
return Arrays.stream(annotationTypes).noneMatch(annotType ->
|
||||
AnnotatedElementUtils.findMergedAnnotation(method, annotType) != null);
|
||||
return Arrays.stream(annotationTypes).noneMatch(
|
||||
annotType -> AnnotatedElementUtils.findMergedAnnotation(method, annotType) != null);
|
||||
}
|
||||
else {
|
||||
return method.getAnnotations().length == 0;
|
||||
@@ -373,10 +362,11 @@ public class ResolvableMethod {
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a {@code ResolvableMethod} from the provided filters which must
|
||||
* resolve to a unique, single method.
|
||||
* <p>See additional resolveXxx shortcut methods going directly to
|
||||
* {@link Method} or return type parameter.
|
||||
* Build a {@code ResolvableMethod} from the provided filters which must resolve
|
||||
* to a unique, single method.
|
||||
* <p>
|
||||
* See additional resolveXxx shortcut methods going directly to {@link Method} or
|
||||
* return type parameter.
|
||||
* @throws IllegalStateException for no match or multiple matches
|
||||
*/
|
||||
public ResolvableMethod method() {
|
||||
@@ -391,8 +381,8 @@ public class ResolvableMethod {
|
||||
}
|
||||
|
||||
private String formatMethods(Set<Method> methods) {
|
||||
return "\nMatched:\n" + methods.stream()
|
||||
.map(Method::toGenericString).collect(joining(",\n\t", "[\n\t", "\n]"));
|
||||
return "\nMatched:\n"
|
||||
+ methods.stream().map(Method::toGenericString).collect(joining(",\n\t", "[\n\t", "\n]"));
|
||||
}
|
||||
|
||||
public ResolvableMethod mockCall(Consumer<T> invoker) {
|
||||
@@ -403,12 +393,12 @@ public class ResolvableMethod {
|
||||
return new ResolvableMethod(method);
|
||||
}
|
||||
|
||||
|
||||
// Build & resolve shortcuts...
|
||||
|
||||
/**
|
||||
* Resolve and return the {@code Method} equivalent to:
|
||||
* <p>{@code build().method()}
|
||||
* <p>
|
||||
* {@code build().method()}
|
||||
*/
|
||||
public final Method resolveMethod() {
|
||||
return method().method();
|
||||
@@ -416,7 +406,8 @@ public class ResolvableMethod {
|
||||
|
||||
/**
|
||||
* Resolve and return the {@code Method} equivalent to:
|
||||
* <p>{@code named(methodName).build().method()}
|
||||
* <p>
|
||||
* {@code named(methodName).build().method()}
|
||||
*/
|
||||
public Method resolveMethod(String methodName) {
|
||||
return named(methodName).method().method();
|
||||
@@ -424,7 +415,8 @@ public class ResolvableMethod {
|
||||
|
||||
/**
|
||||
* Resolve and return the declared return type equivalent to:
|
||||
* <p>{@code build().returnType()}
|
||||
* <p>
|
||||
* {@code build().returnType()}
|
||||
*/
|
||||
public final MethodParameter resolveReturnType() {
|
||||
return method().returnType();
|
||||
@@ -432,7 +424,8 @@ public class ResolvableMethod {
|
||||
|
||||
/**
|
||||
* Shortcut to the unique return type equivalent to:
|
||||
* <p>{@code returning(returnType).build().returnType()}
|
||||
* <p>
|
||||
* {@code returning(returnType).build().returnType()}
|
||||
* @param returnType the return type
|
||||
* @param generics optional array of generic types
|
||||
*/
|
||||
@@ -442,7 +435,8 @@ public class ResolvableMethod {
|
||||
|
||||
/**
|
||||
* Shortcut to the unique return type equivalent to:
|
||||
* <p>{@code returning(returnType).build().returnType()}
|
||||
* <p>
|
||||
* {@code returning(returnType).build().returnType()}
|
||||
* @param returnType the return type
|
||||
* @param generic at least one generic type
|
||||
* @param generics optional extra generic types
|
||||
@@ -457,20 +451,17 @@ public class ResolvableMethod {
|
||||
return returning(returnType).method().returnType();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ResolvableMethod.Builder[\n" +
|
||||
"\tobjectClass = " + this.objectClass.getName() + ",\n" +
|
||||
"\tfilters = " + formatFilters() + "\n]";
|
||||
return "ResolvableMethod.Builder[\n" + "\tobjectClass = " + this.objectClass.getName() + ",\n"
|
||||
+ "\tfilters = " + formatFilters() + "\n]";
|
||||
}
|
||||
|
||||
private String formatFilters() {
|
||||
return this.filters.stream().map(Object::toString)
|
||||
.collect(joining(",\n\t\t", "[\n\t\t", "\n\t]"));
|
||||
return this.filters.stream().map(Object::toString).collect(joining(",\n\t\t", "[\n\t\t", "\n\t]"));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Predicate with a descriptive label.
|
||||
@@ -481,13 +472,11 @@ public class ResolvableMethod {
|
||||
|
||||
private final Predicate<T> delegate;
|
||||
|
||||
|
||||
private LabeledPredicate(String label, Predicate<T> delegate) {
|
||||
this.label = label;
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean test(T method) {
|
||||
return this.delegate.test(method);
|
||||
@@ -512,8 +501,8 @@ public class ResolvableMethod {
|
||||
public String toString() {
|
||||
return this.label;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolver for method arguments.
|
||||
@@ -522,15 +511,14 @@ public class ResolvableMethod {
|
||||
|
||||
private final List<Predicate<MethodParameter>> filters = new ArrayList<>(4);
|
||||
|
||||
|
||||
@SafeVarargs
|
||||
private ArgResolver(Predicate<MethodParameter>... filter) {
|
||||
this.filters.addAll(Arrays.asList(filter));
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter on method arguments with annotations.
|
||||
* See {@link org.springframework.web.method.MvcAnnotationPredicates}.
|
||||
* Filter on method arguments with annotations. See
|
||||
* {@link org.springframework.web.method.MvcAnnotationPredicates}.
|
||||
*/
|
||||
@SafeVarargs
|
||||
public final ArgResolver annot(Predicate<MethodParameter>... filters) {
|
||||
@@ -541,8 +529,8 @@ public class ResolvableMethod {
|
||||
/**
|
||||
* Filter on method arguments that have the given annotations.
|
||||
* @param annotationTypes the annotation types
|
||||
* @see #annot(Predicate[])
|
||||
* See {@link org.springframework.web.method.MvcAnnotationPredicates}.
|
||||
* @see #annot(Predicate[]) See
|
||||
* {@link org.springframework.web.method.MvcAnnotationPredicates}.
|
||||
*/
|
||||
@SafeVarargs
|
||||
public final ArgResolver annotPresent(Class<? extends Annotation>... annotationTypes) {
|
||||
@@ -556,10 +544,9 @@ public class ResolvableMethod {
|
||||
*/
|
||||
@SafeVarargs
|
||||
public final ArgResolver annotNotPresent(Class<? extends Annotation>... annotationTypes) {
|
||||
this.filters.add(param ->
|
||||
(annotationTypes.length > 0 ?
|
||||
Arrays.stream(annotationTypes).noneMatch(param::hasParameterAnnotation) :
|
||||
param.getParameterAnnotations().length == 0));
|
||||
this.filters.add(param -> (annotationTypes.length > 0
|
||||
? Arrays.stream(annotationTypes).noneMatch(param::hasParameterAnnotation)
|
||||
: param.getParameterAnnotations().length == 0));
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -593,14 +580,12 @@ public class ResolvableMethod {
|
||||
*/
|
||||
public final MethodParameter arg() {
|
||||
List<MethodParameter> matches = applyFilters();
|
||||
Assert.state(!matches.isEmpty(), () ->
|
||||
"No matching arg in method\n" + formatMethod());
|
||||
Assert.state(matches.size() == 1, () ->
|
||||
"Multiple matching args in method\n" + formatMethod() + "\nMatches:\n\t" + matches);
|
||||
Assert.state(!matches.isEmpty(), () -> "No matching arg in method\n" + formatMethod());
|
||||
Assert.state(matches.size() == 1,
|
||||
() -> "Multiple matching args in method\n" + formatMethod() + "\nMatches:\n\t" + matches);
|
||||
return matches.get(0);
|
||||
}
|
||||
|
||||
|
||||
private List<MethodParameter> applyFilters() {
|
||||
List<MethodParameter> matches = new ArrayList<>();
|
||||
for (int i = 0; i < method.getParameterCount(); i++) {
|
||||
@@ -612,15 +597,14 @@ public class ResolvableMethod {
|
||||
}
|
||||
return matches;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class MethodInvocationInterceptor
|
||||
implements org.springframework.cglib.proxy.MethodInterceptor, MethodInterceptor {
|
||||
|
||||
private Method invokedMethod;
|
||||
|
||||
|
||||
Method getInvokedMethod() {
|
||||
return this.invokedMethod;
|
||||
}
|
||||
@@ -642,6 +626,7 @@ public class ResolvableMethod {
|
||||
public Object invoke(org.aopalliance.intercept.MethodInvocation inv) throws Throwable {
|
||||
return intercept(inv.getThis(), inv.getMethod(), inv.getArguments(), null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -658,7 +643,7 @@ public class ResolvableMethod {
|
||||
else {
|
||||
Enhancer enhancer = new Enhancer();
|
||||
enhancer.setSuperclass(type);
|
||||
enhancer.setInterfaces(new Class<?>[] {Supplier.class});
|
||||
enhancer.setInterfaces(new Class<?>[] { Supplier.class });
|
||||
enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
|
||||
enhancer.setCallbackType(org.springframework.cglib.proxy.MethodInterceptor.class);
|
||||
|
||||
@@ -679,12 +664,13 @@ public class ResolvableMethod {
|
||||
proxy = ReflectionUtils.accessibleConstructor(proxyClass).newInstance();
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
throw new IllegalStateException("Unable to instantiate proxy " +
|
||||
"via both Objenesis and default constructor fails as well", ex);
|
||||
throw new IllegalStateException(
|
||||
"Unable to instantiate proxy " + "via both Objenesis and default constructor fails as well",
|
||||
ex);
|
||||
}
|
||||
}
|
||||
|
||||
((Factory) proxy).setCallbacks(new Callback[] {interceptor});
|
||||
((Factory) proxy).setCallbacks(new Callback[] { interceptor });
|
||||
return (T) proxy;
|
||||
}
|
||||
}
|
||||
|
||||
+16
-11
@@ -36,6 +36,7 @@ import static org.assertj.core.api.Assertions.*;
|
||||
* @author Rob Winch
|
||||
*/
|
||||
public class AuthenticationPrincipalArgumentResolverTests {
|
||||
|
||||
private AuthenticationPrincipalArgumentResolver resolver = new AuthenticationPrincipalArgumentResolver();
|
||||
|
||||
@Test
|
||||
@@ -52,9 +53,9 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
||||
@Test
|
||||
public void resolveArgumentWhenAuthenticationPrincipalThenFound() {
|
||||
Authentication authentication = TestAuthentication.authenticatedUser();
|
||||
Mono<UserDetails> result = (Mono<UserDetails>) this.resolver.resolveArgument(arg0("authenticationPrincipalOnMonoUserDetails"), null)
|
||||
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication))
|
||||
.block();
|
||||
Mono<UserDetails> result = (Mono<UserDetails>) this.resolver
|
||||
.resolveArgument(arg0("authenticationPrincipalOnMonoUserDetails"), null)
|
||||
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication)).block();
|
||||
assertThat(result.block()).isEqualTo(authentication.getPrincipal());
|
||||
}
|
||||
|
||||
@@ -70,9 +71,9 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
||||
@Test
|
||||
public void resolveArgumentWhenMonoAndAuthenticationPrincipalThenFound() {
|
||||
Authentication authentication = TestAuthentication.authenticatedUser();
|
||||
Mono<UserDetails> result = (Mono<UserDetails>) this.resolver.resolveArgument(arg0("currentUserOnMonoUserDetails"), null)
|
||||
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication))
|
||||
.block();
|
||||
Mono<UserDetails> result = (Mono<UserDetails>) this.resolver
|
||||
.resolveArgument(arg0("currentUserOnMonoUserDetails"), null)
|
||||
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication)).block();
|
||||
assertThat(result.block()).isEqualTo(authentication.getPrincipal());
|
||||
}
|
||||
|
||||
@@ -83,14 +84,15 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
||||
@Test
|
||||
public void resolveArgumentWhenExpressionThenFound() {
|
||||
Authentication authentication = TestAuthentication.authenticatedUser();
|
||||
Mono<String> result = (Mono<String>) this.resolver.resolveArgument(arg0("authenticationPrincipalExpression"), null)
|
||||
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication))
|
||||
.block();
|
||||
Mono<String> result = (Mono<String>) this.resolver
|
||||
.resolveArgument(arg0("authenticationPrincipalExpression"), null)
|
||||
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication)).block();
|
||||
assertThat(result.block()).isEqualTo(authentication.getName());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private void authenticationPrincipalExpression(@AuthenticationPrincipal(expression = "username") Mono<String> username) {
|
||||
private void authenticationPrincipalExpression(
|
||||
@AuthenticationPrincipal(expression = "username") Mono<String> username) {
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -109,5 +111,8 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
||||
|
||||
@AuthenticationPrincipal
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@interface CurrentUser {}
|
||||
@interface CurrentUser {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+18
-12
@@ -37,6 +37,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* @author Rob Winch
|
||||
*/
|
||||
public class CurrentSecurityContextArgumentResolverTests {
|
||||
|
||||
private CurrentSecurityContextArgumentResolver resolver = new CurrentSecurityContextArgumentResolver();
|
||||
|
||||
@Test
|
||||
@@ -46,16 +47,17 @@ public class CurrentSecurityContextArgumentResolverTests {
|
||||
|
||||
@Test
|
||||
public void resolveArgumentWhenAuthenticationPrincipalAndEmptyContextThenNull() {
|
||||
Object result = this.resolver.resolveArgument(arg0("currentSecurityContextOnMonoSecurityContext"), null).block();
|
||||
Object result = this.resolver.resolveArgument(arg0("currentSecurityContextOnMonoSecurityContext"), null)
|
||||
.block();
|
||||
assertThat(result).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveArgumentWhenAuthenticationPrincipalThenFound() {
|
||||
Authentication authentication = TestAuthentication.authenticatedUser();
|
||||
Mono<SecurityContext> result = (Mono<SecurityContext>) this.resolver.resolveArgument(arg0("currentSecurityContextOnMonoSecurityContext"), null)
|
||||
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication))
|
||||
.block();
|
||||
Mono<SecurityContext> result = (Mono<SecurityContext>) this.resolver
|
||||
.resolveArgument(arg0("currentSecurityContextOnMonoSecurityContext"), null)
|
||||
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication)).block();
|
||||
assertThat(result.block().getAuthentication()).isEqualTo(authentication);
|
||||
}
|
||||
|
||||
@@ -71,9 +73,9 @@ public class CurrentSecurityContextArgumentResolverTests {
|
||||
@Test
|
||||
public void resolveArgumentWhenMonoAndAuthenticationPrincipalThenFound() {
|
||||
Authentication authentication = TestAuthentication.authenticatedUser();
|
||||
Mono<UserDetails> result = (Mono<UserDetails>) this.resolver.resolveArgument(arg0("currentUserOnMonoUserDetails"), null)
|
||||
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication))
|
||||
.block();
|
||||
Mono<UserDetails> result = (Mono<UserDetails>) this.resolver
|
||||
.resolveArgument(arg0("currentUserOnMonoUserDetails"), null)
|
||||
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication)).block();
|
||||
assertThat(result.block()).isEqualTo(authentication.getPrincipal());
|
||||
}
|
||||
|
||||
@@ -84,14 +86,15 @@ public class CurrentSecurityContextArgumentResolverTests {
|
||||
@Test
|
||||
public void resolveArgumentWhenExpressionThenFound() {
|
||||
Authentication authentication = TestAuthentication.authenticatedUser();
|
||||
Mono<String> result = (Mono<String>) this.resolver.resolveArgument(arg0("authenticationPrincipalExpression"), null)
|
||||
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication))
|
||||
.block();
|
||||
Mono<String> result = (Mono<String>) this.resolver
|
||||
.resolveArgument(arg0("authenticationPrincipalExpression"), null)
|
||||
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication)).block();
|
||||
assertThat(result.block()).isEqualTo(authentication.getName());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private void authenticationPrincipalExpression(@CurrentSecurityContext(expression = "authentication?.principal?.username") Mono<String> username) {
|
||||
private void authenticationPrincipalExpression(
|
||||
@CurrentSecurityContext(expression = "authentication?.principal?.username") Mono<String> username) {
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -110,5 +113,8 @@ public class CurrentSecurityContextArgumentResolverTests {
|
||||
|
||||
@CurrentSecurityContext(expression = "authentication?.principal")
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@interface CurrentUser {}
|
||||
@interface CurrentUser {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+2
@@ -30,6 +30,7 @@ import org.springframework.messaging.Message;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class AndMessageMatcherTests {
|
||||
|
||||
@Mock
|
||||
private MessageMatcher<Object> delegate;
|
||||
|
||||
@@ -113,4 +114,5 @@ public class AndMessageMatcherTests {
|
||||
|
||||
assertThat(matcher.matches(message)).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+2
@@ -30,6 +30,7 @@ import org.springframework.messaging.Message;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class OrMessageMatcherTests {
|
||||
|
||||
@Mock
|
||||
private MessageMatcher<Object> delegate;
|
||||
|
||||
@@ -112,4 +113,5 @@ public class OrMessageMatcherTests {
|
||||
|
||||
assertThat(matcher.matches(message)).isTrue();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+14
-26
@@ -26,6 +26,7 @@ import org.springframework.util.AntPathMatcher;
|
||||
import org.springframework.util.PathMatcher;
|
||||
|
||||
public class SimpDestinationMessageMatcherTests {
|
||||
|
||||
MessageBuilder<String> messageBuilder;
|
||||
|
||||
SimpDestinationMessageMatcher matcher;
|
||||
@@ -55,8 +56,7 @@ public class SimpDestinationMessageMatcherTests {
|
||||
|
||||
@Test
|
||||
public void matchesAllWithDestination() {
|
||||
messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER,
|
||||
"/destination/1");
|
||||
messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/destination/1");
|
||||
|
||||
assertThat(matcher.matches(messageBuilder.build())).isTrue();
|
||||
}
|
||||
@@ -65,8 +65,7 @@ public class SimpDestinationMessageMatcherTests {
|
||||
public void matchesSpecificWithDestination() {
|
||||
matcher = new SimpDestinationMessageMatcher("/destination/1");
|
||||
|
||||
messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER,
|
||||
"/destination/1");
|
||||
messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/destination/1");
|
||||
|
||||
assertThat(matcher.matches(messageBuilder.build())).isTrue();
|
||||
}
|
||||
@@ -75,43 +74,36 @@ public class SimpDestinationMessageMatcherTests {
|
||||
public void matchesFalseWithDestination() {
|
||||
matcher = new SimpDestinationMessageMatcher("/nomatch");
|
||||
|
||||
messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER,
|
||||
"/destination/1");
|
||||
messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/destination/1");
|
||||
|
||||
assertThat(matcher.matches(messageBuilder.build())).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchesFalseMessageTypeNotDisconnectType() {
|
||||
matcher = SimpDestinationMessageMatcher.createMessageMatcher("/match",
|
||||
pathMatcher);
|
||||
matcher = SimpDestinationMessageMatcher.createMessageMatcher("/match", pathMatcher);
|
||||
|
||||
messageBuilder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER,
|
||||
SimpMessageType.DISCONNECT);
|
||||
messageBuilder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.DISCONNECT);
|
||||
|
||||
assertThat(matcher.matches(messageBuilder.build())).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchesTrueMessageType() {
|
||||
matcher = SimpDestinationMessageMatcher.createMessageMatcher("/match",
|
||||
pathMatcher);
|
||||
matcher = SimpDestinationMessageMatcher.createMessageMatcher("/match", pathMatcher);
|
||||
|
||||
messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/match");
|
||||
messageBuilder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER,
|
||||
SimpMessageType.MESSAGE);
|
||||
messageBuilder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.MESSAGE);
|
||||
|
||||
assertThat(matcher.matches(messageBuilder.build())).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchesTrueSubscribeType() {
|
||||
matcher = SimpDestinationMessageMatcher.createSubscribeMatcher("/match",
|
||||
pathMatcher);
|
||||
matcher = SimpDestinationMessageMatcher.createSubscribeMatcher("/match", pathMatcher);
|
||||
|
||||
messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/match");
|
||||
messageBuilder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER,
|
||||
SimpMessageType.SUBSCRIBE);
|
||||
messageBuilder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.SUBSCRIBE);
|
||||
|
||||
assertThat(matcher.matches(messageBuilder.build())).isTrue();
|
||||
}
|
||||
@@ -121,8 +113,7 @@ public class SimpDestinationMessageMatcherTests {
|
||||
matcher = new SimpDestinationMessageMatcher("/match");
|
||||
|
||||
messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/match");
|
||||
messageBuilder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER,
|
||||
SimpMessageType.MESSAGE);
|
||||
messageBuilder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.MESSAGE);
|
||||
|
||||
assertThat(matcher.matches(messageBuilder.build())).isTrue();
|
||||
}
|
||||
@@ -132,8 +123,7 @@ public class SimpDestinationMessageMatcherTests {
|
||||
matcher = new SimpDestinationMessageMatcher("/topics/{topic}/**");
|
||||
|
||||
messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/topics/someTopic/sub1");
|
||||
messageBuilder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER,
|
||||
SimpMessageType.MESSAGE);
|
||||
messageBuilder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.MESSAGE);
|
||||
|
||||
assertThat(matcher.extractPathVariables(messageBuilder.build()).get("topic")).isEqualTo("someTopic");
|
||||
}
|
||||
@@ -146,11 +136,9 @@ public class SimpDestinationMessageMatcherTests {
|
||||
|
||||
@Test
|
||||
public void typeConstructorParameterIsTransmitted() {
|
||||
matcher = SimpDestinationMessageMatcher.createMessageMatcher("/match",
|
||||
pathMatcher);
|
||||
matcher = SimpDestinationMessageMatcher.createMessageMatcher("/match", pathMatcher);
|
||||
|
||||
MessageMatcher<Object> expectedTypeMatcher = new SimpMessageTypeMatcher(
|
||||
SimpMessageType.MESSAGE);
|
||||
MessageMatcher<Object> expectedTypeMatcher = new SimpMessageTypeMatcher(SimpMessageType.MESSAGE);
|
||||
|
||||
assertThat(matcher.getMessageTypeMatcher()).isEqualTo(expectedTypeMatcher);
|
||||
|
||||
|
||||
+6
-8
@@ -25,6 +25,7 @@ import org.springframework.messaging.simp.SimpMessageType;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
|
||||
public class SimpMessageTypeMatcherTests {
|
||||
|
||||
private SimpMessageTypeMatcher matcher;
|
||||
|
||||
@Before
|
||||
@@ -39,20 +40,16 @@ public class SimpMessageTypeMatcherTests {
|
||||
|
||||
@Test
|
||||
public void matchesMessageMessageTrue() {
|
||||
Message<String> message = MessageBuilder
|
||||
.withPayload("Hi")
|
||||
.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER,
|
||||
SimpMessageType.MESSAGE).build();
|
||||
Message<String> message = MessageBuilder.withPayload("Hi")
|
||||
.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.MESSAGE).build();
|
||||
|
||||
assertThat(matcher.matches(message)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchesMessageConnectFalse() {
|
||||
Message<String> message = MessageBuilder
|
||||
.withPayload("Hi")
|
||||
.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER,
|
||||
SimpMessageType.CONNECT).build();
|
||||
Message<String> message = MessageBuilder.withPayload("Hi")
|
||||
.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.CONNECT).build();
|
||||
|
||||
assertThat(matcher.matches(message)).isFalse();
|
||||
}
|
||||
@@ -63,4 +60,5 @@ public class SimpMessageTypeMatcherTests {
|
||||
|
||||
assertThat(matcher.matches(message)).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+3
-2
@@ -35,6 +35,7 @@ import org.springframework.security.web.csrf.MissingCsrfTokenException;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class CsrfChannelInterceptorTests {
|
||||
|
||||
@Mock
|
||||
MessageChannel channel;
|
||||
|
||||
@@ -125,8 +126,7 @@ public class CsrfChannelInterceptorTests {
|
||||
|
||||
@Test(expected = InvalidCsrfTokenException.class)
|
||||
public void preSendInvalidToken() {
|
||||
messageHeaders.setNativeHeader(token.getHeaderName(), token.getToken()
|
||||
+ "invalid");
|
||||
messageHeaders.setNativeHeader(token.getHeaderName(), token.getToken() + "invalid");
|
||||
|
||||
interceptor.preSend(message(), channel);
|
||||
}
|
||||
@@ -149,4 +149,5 @@ public class CsrfChannelInterceptorTests {
|
||||
Map<String, Object> headersToCopy = messageHeaders.toMap();
|
||||
return MessageBuilder.withPayload("hi").copyHeaders(headersToCopy).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+2
-1
@@ -34,13 +34,14 @@ import java.util.Map;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Rob Winch
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class CsrfTokenHandshakeInterceptorTests {
|
||||
|
||||
@Mock
|
||||
WebSocketHandler wsHandler;
|
||||
|
||||
@Mock
|
||||
ServerHttpResponse response;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user