spring-boot - 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。
解决方案
你得到的错误是什么?也许是因为它无法获得用户。你可以试试:
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);
推荐阅读
- sql-server - T-SQL 通过 CTE 过滤聚合列或使用视图中的列进行子查询
- tshark - 从包含流记录的 pcap 确定吞吐量
- python - 仅使用 Python 内置函数,是否可以仅读取一组指定的列以添加到 Python 字典中?
- ruby-on-rails - 如何知道共享关注中的当前类
- android - 如何解决加载文件时的编码错误?
- nginx - nginx入口子路径重定向
- sql-server - Bcp 实用程序 - 如何导入 10 亿条记录。数据文件到 SQL 服务器
- python - Pandas 在读取格式错误的数据时不会引发异常
- python-3.x - Python等待消息的时间限制
- ios - 如何从特定的 UICollectionViewCell 中删除 firebase 子节点 - Swift