public class AuthenticationInterceptor implements MessageDispatchInterceptor<Message<?>> {
@Override
public BiFunction<Integer, Message<?>, Message<?>> handle(
List<Message<?>> messages) {
return (index, message) -> SecurityContextHolder.getContext().getAuthentication() == null
? message : message
.andMetaData(Collections.singletonMap("username",
SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString()));
}
}
@EventHandler
public void on(ProjectImportedEvent event, @MetaDataValue("username") String username,
@Timestamp Instant timestamp)
@Configuration
public class AxonConfig {
@Bean()
CorrelationDataProvider correlationDataProviders() {
return new SimpleCorrelationDataProvider("username");
}
}
--
You received this message because you are subscribed to the Google Groups "Axon Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to axonframewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
@Configuration
public class AxonAuditConfiguration {
@Autowired
public void registerInterceptors(CommandBus commandBus) {
if (commandBus instanceof SimpleCommandBus) {
commandBus.registerHandlerInterceptor(new AuthenticationInterceptor());
}
}
}
@NoArgsConstructor
public class AuthenticationInterceptor implements MessageHandlerInterceptor<Message<?>> {
@Override
public Object handle(UnitOfWork<? extends Message<?>> unitOfWork, InterceptorChain interceptorChain) throws Exception {
if (SecurityContextHolder.getContext().getAuthentication() != null) {
unitOfWork.transformMessage(message ->
message.andMetaData(Collections.singletonMap("username",
SecurityContextHolder.getContext().getAuthentication().getPrincipal()().get().toString()))
);
unitOfWork.registerCorrelationDataProvider(new SimpleCorrelationDataProvider("username"));
}
return interceptorChain.proceed();
}
}