Spring Data + SpringBootTest:如何为JPA审计模拟SecurityContext?

时间:2019-03-17 22:03:46

标签: java spring-boot spring-security spring-data-jpa spring-test

在Spring Boot应用程序中,我想使用启用的审核(@EnableJpaAuditing)测试(JUnit 5)持久层。 我使用Liquibase来设置H2数据库并将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>

我尝试了几种注释组合:

@SpringBootTest //(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//@DataJpaTest
@ContextConfiguration(classes = MyPersistenceConfig.class)
@EnableAutoConfiguration
//@SecurityTestExecutionListeners
//@Import(SpringBootWebSecurityConfiguration.class)
@WithMockUser(username = "test", password = "test", roles = "USER")
@ExtendWith(SpringExtension.class)
class MyRepositoryTest {

    @Autowired
    private MyRepository testee;

...
}

但是无论我如何尝试,要么存储库为空(自动装配),要么在插入条目时出现异常:

NULL not allowed for column "CREATED_BY"; SQL statement:

我想我需要一个SecurityContext(当前无法自动装配)。

提供与审核和@WithMockUser一起使用的模拟SecurityContext的最简单方法是什么?

1 个答案:

答案 0 :(得分:0)

@Before
public void setup() {

    User user = userService.findByEmail("umanking@gmail.com").get();

    SecurityContextHolder.getContext().setAuthentication(new Authentication() {
        @Override
        public Collection<? extends GrantedAuthority> getAuthorities() {
            return null;
        }

        @Override
        public Object getCredentials() {
            return user.getPassword();
        }

        @Override
        public Object getDetails() {
            return user;
        }

        @Override
        public Object getPrincipal() {
            return null;
        }

        @Override
        public boolean isAuthenticated() {
            return true;
        }

        @Override
        public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {

        }

        @Override
        public String getName() {
            return user.getName();
        }
    });

}

也许您可以使用@Before注释