为了账号安全,请及时绑定邮箱和手机立即绑定

如何模拟 SecurityContext 进行 JPA 审计?

如何模拟 SecurityContext 进行 JPA 审计?

皈依舞 2022-08-17 10:48:19
在Spring Boot应用程序中,我想测试(JUnit 5)启用审计的持久性层(@EnableJpaAuditing)。我使用 Liquibase 来设置 H2 db 和 Hibernate 作为 JPA 实现。@Configuration//@EnableTransactionManagement@EnableJpaAuditing//@EnableJpaRepositoriespublic 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>
查看完整描述

2 回答

?
慕田峪4524236

TA贡献1875条经验 获得超5个赞

这是一个古老的问题,但对于那些可能偶然发现它试图让Spring Data审计在他们的集成测试中工作的人来说,这可能会有所帮助。审计功能需要一个 Bean 来获取当前用户。在这一点上似乎缺失了。使其可用的一种方法是向测试中添加配置。AuditingAwareDataJpaTest@Bean


@RunWith(SpringRunner.class)

@DataJpaTest

@Import({DatabaseIntegrationTest.TestConfig.class})

@WithMockUser

class DatabaseIntegrationTest {


  @TestConfiguration

  @RequiredArgsConstructor

  static class TestConfig {

    @Bean

    public AuditorAware<String> auditorAware() {

      return () -> Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication().getName());

    }

  }

}


查看完整回答
反对 回复 2022-08-17
?
月关宝盒

TA贡献1772条经验 获得超5个赞

@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注释


查看完整回答
反对 回复 2022-08-17
  • 2 回答
  • 0 关注
  • 101 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号