如何在Grails中为从rabbitmq消息完成的事务建立KeyClope安全上下文

如何在Grails中为从rabbitmq消息完成的事务建立KeyClope安全上下文,grails,spring-security,keycloak,Grails,Spring Security,Keycloak,我有一个Grails应用程序,它在生产中使用gorm插件来查找做了更改的用户,并存储他的完整信息。在所有应用程序中,KeyClope都是集成的 我在这里遇到的问题是,有些实现是通过@RabbitListener方法调用的,这些方法没有任何安全上下文,因为它不是web请求 我可以期望客户端通过消息头传递用户名,然后我可以使用message.getMessageProperties().getHeaders().get(“用户名”)获取该请求的用户名,并建立简单的安全上下文,如下面所示: Authe

我有一个Grails应用程序,它在生产中使用gorm插件来查找做了更改的用户,并存储他的完整信息。在所有应用程序中,KeyClope都是集成的

我在这里遇到的问题是,有些实现是通过@RabbitListener方法调用的,这些方法没有任何安全上下文,因为它不是web请求

我可以期望客户端通过消息头传递用户名,然后我可以使用message.getMessageProperties().getHeaders().get(“用户名”)获取该请求的用户名,并建立简单的安全上下文,如下面所示:

Authentication authentication = new UsernamePasswordAuthenticationToken(user, null,
AuthorityUtils.createAuthorityList("ROLE_USER"));
SecurityContextHolder.getContext().setAuthentication(authentication);
LOGGER.debug("user:{} fullname:{}", springSecurityService?.principal?.username, springSecurityService?.authentication?.principal?.getAt("fullName"))
但是,我仍然需要创建一个keydeposecuritycontext,它可以用于使用Keyclock主体中定义的主体属性的所有层和插件。Gorm插件需要从springSecurityService获取用户信息,如下面所示:

Authentication authentication = new UsernamePasswordAuthenticationToken(user, null,
AuthorityUtils.createAuthorityList("ROLE_USER"));
SecurityContextHolder.getContext().setAuthentication(authentication);
LOGGER.debug("user:{} fullname:{}", springSecurityService?.principal?.username, springSecurityService?.authentication?.principal?.getAt("fullName"))