创建时将当前用户添加到实体

Add current user to entity when created

我在 Yeoman 生成器的帮助下创建了一个实体。我选择不创建 DAO。该实体与用户有关系。

现在,当我创建一个对象时,我可以在下拉列表中选择所有者,但我想将所有者设置为当前登录的用户。执行此操作的最佳方法是什么?

我试过了

@Autowired
private Authentication authentication;

activity.setOwner((User) authentication.getPrincipal());

在资源 Class 中,但这会引发此异常

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.security.core.Authentication

问候 马蒂亚斯

嗯,我认为你所有的问题都在于获取当前用户。

那么是什么让您停止使用 your.package.security.SecurityUtils.getCurrentLogin()

以上代码将为您提供登录名。如果您需要 iduser 对象作为一个整体,那么您将需要从数据库中获取。您也可以使用 UserService 来做到这一点。