【问题标题】:Mock security context for JPA integration testJPA 集成测试的模拟安全上下文
【发布时间】:2019-05-21 21:42:30
【问题描述】:

我正在尝试测试 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-boot spring-security spring-data-jpa


    【解决方案1】:

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

    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);
    

    【讨论】:

      猜你喜欢
      • 2012-04-25
      • 1970-01-01
      • 2013-08-18
      • 2012-08-21
      • 2018-12-02
      • 1970-01-01
      • 2018-02-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多