Earlier detection of token authentication
Use a callback to detect token authentication (via inteceptor) thus avoiding a potential race between that detection after the message is sent on the inbound channel (via Executor) and the processing of the CONNECTED frame returned from the broker on the outbound channel. Closes gh-23160
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,6 +19,7 @@ package org.springframework.messaging.simp;
|
||||
import java.security.Principal;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.messaging.Message;
|
||||
@@ -84,6 +85,10 @@ public class SimpMessageHeaderAccessor extends NativeMessageHeaderAccessor {
|
||||
public static final String IGNORE_ERROR = "simpIgnoreError";
|
||||
|
||||
|
||||
@Nullable
|
||||
private Consumer<Principal> userCallback;
|
||||
|
||||
|
||||
/**
|
||||
* A constructor for creating new message headers.
|
||||
* This constructor is protected. See factory methods in this and sub-classes.
|
||||
@@ -171,6 +176,9 @@ public class SimpMessageHeaderAccessor extends NativeMessageHeaderAccessor {
|
||||
|
||||
public void setUser(@Nullable Principal principal) {
|
||||
setHeader(USER_HEADER, principal);
|
||||
if (this.userCallback != null) {
|
||||
this.userCallback.accept(principal);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -181,6 +189,18 @@ public class SimpMessageHeaderAccessor extends NativeMessageHeaderAccessor {
|
||||
return (Principal) getHeader(USER_HEADER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide a callback to be invoked if and when {@link #setUser(Principal)}
|
||||
* is called. This is used internally on the inbound channel to detect
|
||||
* token-based authentications through an interceptor.
|
||||
* @param callback the callback to invoke
|
||||
* @since 5.1.9
|
||||
*/
|
||||
public void setUserChangeCallback(Consumer<Principal> callback) {
|
||||
Assert.notNull(callback, "'callback' is required");
|
||||
this.userCallback = this.userCallback != null ? this.userCallback.andThen(callback) : callback;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getShortLogMessage(Object payload) {
|
||||
if (getMessageType() == null) {
|
||||
|
||||
Reference in New Issue
Block a user