I also found a quick solution to add a user to the security context. Since I need this to call secure service methods in the Kafka listener class, this does not affect any authentication process in itself.
User user = new User("SYSTEM", "SYSTEM", "SYSTEM");
UserDetailsImpl userDetails = new UserDetailsImpl(user,
Arrays.asList(<YOUR_ROLE_HERE>));
// UserDetailsImpl - class that implements
// org.springframework.security.core.userdetails.UserDetails
UsernamePasswordAuthenticationToken token =
new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(token);
source
share