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:
Rossen Stoyanchev
2019-07-03 15:22:56 +01:00
parent 5af9a8edae
commit 4e6e47b726
4 changed files with 75 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 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.
@@ -16,11 +16,14 @@
package org.springframework.messaging.simp;
import java.security.Principal;
import java.util.Collections;
import java.util.function.Consumer;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
/**
* Unit tests for SimpMessageHeaderAccessor.
@@ -63,4 +66,35 @@ public class SimpMessageHeaderAccessorTests {
"{nativeKey=[nativeValue]} payload=p", accessor.getDetailedLogMessage("p"));
}
@Test
public void userChangeCallback() {
UserCallback userCallback = new UserCallback();
SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create();
accessor.setUserChangeCallback(userCallback);
Principal user1 = mock(Principal.class);
accessor.setUser(user1);
assertEquals(user1, userCallback.getUser());
Principal user2 = mock(Principal.class);
accessor.setUser(user2);
assertEquals(user2, userCallback.getUser());
}
private static class UserCallback implements Consumer<Principal> {
private Principal user;
public Principal getUser() {
return this.user;
}
@Override
public void accept(Principal principal) {
this.user = principal;
}
}
}