@ -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" ) ;
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* you may not use this file except in compliance with the License .
@ -19,6 +19,7 @@ package org.springframework.messaging.simp;
import java.security.Principal ;
import java.security.Principal ;
import java.util.List ;
import java.util.List ;
import java.util.Map ;
import java.util.Map ;
import java.util.function.Consumer ;
import org.springframework.lang.Nullable ;
import org.springframework.lang.Nullable ;
import org.springframework.messaging.Message ;
import org.springframework.messaging.Message ;
@ -84,6 +85,10 @@ public class SimpMessageHeaderAccessor extends NativeMessageHeaderAccessor {
public static final String IGNORE_ERROR = "simpIgnoreError" ;
public static final String IGNORE_ERROR = "simpIgnoreError" ;
@Nullable
private Consumer < Principal > userCallback ;
/ * *
/ * *
* A constructor for creating new message headers .
* A constructor for creating new message headers .
* This constructor is protected . See factory methods in this and sub - classes .
* 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 ) {
public void setUser ( @Nullable Principal principal ) {
setHeader ( USER_HEADER , 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 ) ;
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
@Override
public String getShortLogMessage ( Object payload ) {
public String getShortLogMessage ( Object payload ) {
if ( getMessageType ( ) = = null ) {
if ( getMessageType ( ) = = null ) {