I have a MEssageDispatchInterceptor:
`
private MessageDispatchInterceptor<Message<?>> AuthenticationInterceptor() {
return list -> {
if ( isInitDraftCommand(list.get(0)) ) {
return (index, message) -> message;
}
final PolluxUserDetails user = SecurityUtils.getUser();
if (Objects.nonNull(user)) {
return (index, message) -> message.andMetaData( Collections.singletonMap(“user”, user) );
}
throw new SecurityException(“User not found in context”);
};
}
`
And into a spring service a method with @EventHandler:
`
@Service(“managerService”)
public class ManagerService {
@EventHandler
protected void loadDraft(final LoadDraftEvent event) throws DraftException {
final CompletableFuture loadStationFuture = CompletableFuture.supplyAsync(() -> {
for(Station station: getStations() {
commandGateway.send(new InitStationDraftCommand(stationId, draft.getDraftId(), station));
}
return true;
}
final CompletableFuture loadAreaFeature = CompletableFuture.supplyAsync(() -> {
for(Area area: getAreas() {
commandGateway.send(new InitAreaDraftCommand(areaId, draft.getDraftId(), area));
}
return true;
}
Stream.of(loadStationFuture, loadAreaFeature)
.map(CompletableFuture::join).reduce((a, b) -> a && b)
.orElseThrow(() -> new DraftException("Error when loading Draft: " + draft.getDraftId()));
}
}
`
The problem is on Interceptor the user that I get from SecurityContextHolder.getContext() is null on the first CompletableFuture but not in the second depends on which task start first.
Why this? on my configuration I have: @EnableAsync and on config class constructor SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL);