如何模拟 SecurityContext 进行 JPA 审计?

在Spring Boot应用程序中,我想测试(JUnit 5)启用审计的持久性层(@EnableJpaAuditing)。我使用 Liquibase 来设置 H2 db 和 Hibernate 作为 JPA 实现。


@Configuration

//@EnableTransactionManagement

@EnableJpaAuditing

//@EnableJpaRepositories

public class MyPersistenceConfig {

}

我的实体具有以下字段:


@CreatedDate

@Column(name = "CREATED_AT", updatable = false)

private Instant createdAt;


@CreatedBy

@Column(name = "CREATED_BY", updatable = false)

private String createdBy;


@CreatedDate

@Column(name = "LAST_MODIFIED_AT")

private Instant lastModifiedAt;


@CreatedBy

@Column(name = "LAST_MODIFIED_BY")

private String lastModifiedBy;

我有以下依赖关系:


    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-data-jpa</artifactId>

    </dependency>

    <dependency>

        <groupId>javax.validation</groupId>

        <artifactId>validation-api</artifactId>

    </dependency>

    <dependency>

        <groupId>org.liquibase</groupId>

        <artifactId>liquibase-core</artifactId>

        <scope>runtime</scope>

        <!--<scope>test</scope>-->

    </dependency>


    <!-- Testing -->

    <dependency>

        <groupId>org.junit.jupiter</groupId>

        <artifactId>junit-jupiter-api</artifactId>

        <scope>test</scope>

    </dependency>

    <dependency>

        <groupId>org.junit.jupiter</groupId>

        <artifactId>junit-jupiter-engine</artifactId>

        <scope>test</scope>

    </dependency>

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-test</artifactId>

        <scope>test</scope>

    </dependency>

    <dependency>

        <groupId>org.springframework.security</groupId>

        <artifactId>spring-security-test</artifactId>

        <scope>test</scope>

    </dependency>

    <dependency>

        <groupId>com.h2database</groupId>

        <artifactId>h2</artifactId>

        <scope>test</scope>

    </dependency>


皈依舞
浏览 102回答 2
2回答

慕田峪4524236

这是一个古老的问题,但对于那些可能偶然发现它试图让Spring Data审计在他们的集成测试中工作的人来说,这可能会有所帮助。审计功能需要一个 Bean 来获取当前用户。在这一点上似乎缺失了。使其可用的一种方法是向测试中添加配置。AuditingAwareDataJpaTest@Bean@RunWith(SpringRunner.class)@DataJpaTest@Import({DatabaseIntegrationTest.TestConfig.class})@WithMockUserclass DatabaseIntegrationTest {&nbsp; @TestConfiguration&nbsp; @RequiredArgsConstructor&nbsp; static class TestConfig {&nbsp; &nbsp; @Bean&nbsp; &nbsp; public AuditorAware<String> auditorAware() {&nbsp; &nbsp; &nbsp; return () -> Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication().getName());&nbsp; &nbsp; }&nbsp; }}

月关宝盒

@Beforepublic void setup() {&nbsp; &nbsp; User user = userService.findByEmail("umanking@gmail.com").get();&nbsp; &nbsp; SecurityContextHolder.getContext().setAuthentication(new Authentication() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public Collection<? extends GrantedAuthority> getAuthorities() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public Object getCredentials() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return user.getPassword();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public Object getDetails() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return user;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public Object getPrincipal() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public boolean isAuthenticated() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public String getName() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return user.getName();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}也许你可以使用@Before注释
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java