微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在从数据种子开始的应用程序中,auditAware试图加载不存在的用户我该怎么办?

如何解决在从数据种子开始的应用程序中,auditAware试图加载不存在的用户我该怎么办?

启动我的应用程序时出现问题,
加载所有内容时,我有一个数据种子类
并将所有必需的数据(例如用户角色和第一个管理员用户)加载到数据库
当我的数据库加载了数据时,一切正常,但是当使用空数据库启动应用程序时,它开始

我认为问题是-AuditorAwareImpl无法找到任何要添加created的用户。通过可选的方法,user_role记录和第一个用户记录。

这是代码
DataSeed类:

@Component
public class DataSeed implements InitializingBean {


    ...


    @Override
    public void afterPropertiesSet() throws Exception {
        for (UserRole.Role role : UserRole.Role.values()) {
            createRole(role);
        }
        createDefaultUser();
    }

    private void createRole(UserRole.Role role) {
        String roleCheck = role.roleName();
        if (!userRoleRepository.roleExists(roleCheck)) {
            userRoleRepository.save(UserRole.apply(role));
        }
    }

    private void createDefaultUser() {
        String admin = "admin";
        if (!userRepository.existsByLogin(admin)) {
            User defaultUser = new User();
            ... // default user data 
            userRepository.save(defaultUser);
        }
    }

用户实体(用户角色基于相同的基础,不会浪费时间阅读重复的代码):

@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "USER_ENTITY")
public class User  extends BaseEntity implements AuditInterface{


   ... // some static stuff
    

    private String login;

    private String password;


    @ManyToMany
    private List<UserRole> roles;

    public User() {
    }

基础实体具有附加的版本字段,并且扩展了AbstractAuditable

我还有用于审核的配置:

@Configuration
@EnableJpaRepositories(basePackages = {"com.teamcompetencymatrix.www"})
@EnableTransactionManagement
@EnableJpaAuditing
public class AuditConfig {

    private final UserService userService;

    public AuditConfig(UserService userService) {
        this.userService = userService;
    }

    @Bean
    public AuditorAware<User> auditorProvider() {
        return new AuditorAwareImpl(userService);
    }

}

和AuditorAware实施

public class AuditorAwareImpl implements AuditorAware<User> {

    private UserService userService;

    public AuditorAwareImpl(UserService userService) {
        this.userService = userService;
    }
    
    @NonNull
    @Override
    public Optional<User> getCurrentAuditor() {
        return userService.getAuditor();
    }
}

这是错误

Caused by: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request,or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message,your code is probably running outside of dispatcherServlet: In this case,use RequestContextListener or RequestContextFilter to expose the current request.
    at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.web.context.support.WebApplicationContextUtils.currentRequestAttributes(WebApplicationContextUtils.java:313) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.web.context.support.WebApplicationContextUtils.access$400(WebApplicationContextUtils.java:66) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.web.context.support.WebApplicationContextUtils$RequestObjectFactory.getobject(WebApplicationContextUtils.java:329) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.web.context.support.WebApplicationContextUtils$RequestObjectFactory.getobject(WebApplicationContextUtils.java:324) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.beans.factory.support.AutowireUtils$ObjectFactoryDelegatingInvocationHandler.invoke(AutowireUtils.java:294) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at com.sun.proxy.$Proxy106.getUserPrincipal(UnkNown Source) ~[na:na]
    at com.teamcompetencymatrix.www.service.UserService.getAuditor(UserService.java:95) ~[classes/:na]
    at com.teamcompetencymatrix.www.config.AuditorAwareImpl.getCurrentAuditor(AuditorAwareImpl.java:23) ~[classes/:na]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。