首页 > 解决方案 > JPA 集成测试的模拟安全上下文

问题描述

我正在尝试测试 spring JPA 存储库接口以确保我的映射正确。我的实体扩展了一个带有注释的基本实体。

@EntityListeners(BaseEntityEventListener.class)
@MappedSuperclass
public abstract class BaseEntity {...

事件侦听器填充了一些审计属性。

public class BaseEntityEventListener {
    @PrePersist
    public void onPreInsert(BaseEntity baseEntity){
        MyUserPrincipal principal = (MyUserPrincipal) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = principal.getUsername();
        baseEntity.setCreationUser(username);
        Timestamp ts = new Timestamp(System.currentTimeMillis());
        baseEntity.setCreationDate(ts);
    }...

这没关系,但是当我想测试存储库时,我得到了 SecurityContextHolder 的空指针。

@RunWith(SpringRunner.class)
@SpringBootTest
public class RepositoryTest {
    @Autowired private MyRepository myRepo;

    @Test
    public void testSaveEntity() throws Exception {
        Entity entity = new Entity(TEST_ID);
        myRepo.save(entity);
    }...

从测试中调用事件侦听器类时,未设置安全上下文。我曾尝试使用 @WithMockUser 但这似乎不起作用。我可以在服务中包装对安全上下文的调用,然后在我的集成测试中以某种方式模拟这个调用。如果这是一个选项,我如何在实体侦听器上设置模拟。当我使用 @CreatedBy 和 @CreatedDate 时,安全上下文不是问题,但出于单独的原因,我需要手动使用 @PreInsert。

标签: spring-bootspring-securityspring-data-jpa

解决方案


你得到的错误是什么?也许是因为它无法获得用户。你可以试试:

val userPrincipal =
                new org.springframework.security.core.userdetails.User(username,
                        "",
                        true,
                        true,
                        true,
                        true,
                        authorities);
        val auth = new TestingAuthenticationToken(userPrincipal, null, authorities);
        auth.setAuthenticated(true);
        SecurityContextHolder.getContext().setAuthentication(auth);

推荐阅读