在SecurityContext中注入身份验证,用于JUNIT测试(Java+ Spring)

发布于 2025-01-24 20:26:59 字数 2057 浏览 3 评论 0原文

我有一种用于待办事项方法的测试方法。服务方法具有下一个注释:

@PreAuthorize("verifyRole(T(java.util.Set).of(" +
    "'ROLE_HEAD_BANK', 'ROLE_OPERATOR_BANK_REPORTER', 'ROLE_AUTHORIZED_BANK_REPORTER'," +
    " 'ROLE_AUTHORIZED_BANK_REPORTER', 'ROLE_OPERATOR_BANK_REPORTER'))")

方法verifyrole:

public boolean verifyRole(Set<String> roles) {
  var grantedAuthorities =
    ((User) authentication.getPrincipal()).getGrantedAuthorities()
    .stream()
    .map(GrantedAuthority::getAuthority)
    .collect(Collectors.toSet());

  return !Collections.disjoint(grantedAuthorities, roles);
}

我尝试使用@withmockuser(roles = {“ head_bank”,“ euthorized_bank_reporter”}) ,并且在on (用户)Authentication.getPrincipal()带有下一个错误: java.lang.ClassCastException: class org.springframework.security.core.userdetails.User cannot be cast to class com.kilma.raw.domain.entity.User (org.springframework.security.core.userdetails.User and com.kilma.raw.domain.entity.user位于加载程序'app''的未命名模块中)

我尝试了其他类型的求解时刻,而@withmockuser我可以走了最大的努力。如果还有其他方法可以告诉我吗?也许我需要在SecurityContext上工作,但已经尝试过并且没有工作。

ps当我评论@preatuthorize注释时,一切都很好。

我的测试代码在reportervice.getReport上失败, @preatuthorize注释:getReport方法:

@Test
@WithMockUser(roles = {
  "HEAD_BANK",
  "AUTHORIZED_BANK_REPORTER"
})
void getReport_success() {
  final ReportResponse report = reportService.getReport(1 L, bank1MockedAuth).getPayload();

  final long publishedWithDataReportId = 1 L;
  assertEquals(publishedWithDataReportId, report.getId());
  final Instant instant = Instant.parse("2022-04-01T00:00:00.000Z");
  final Long testDate = instant.toEpochMilli();
  assertEquals(testDate, report.getCreatedAt());
  assertEquals(testDate, report.getModifiedAt());
  assertEquals(KEY_MORTGAGE_INDICATOR, report.getReportType());
  assertEquals(testDate, report.getReportPeriod());
  assertEquals(PUBLISHED, report.getStatus());
}      

I have a test method for serivice method. Service method has the next annotation:

@PreAuthorize("verifyRole(T(java.util.Set).of(" +
    "'ROLE_HEAD_BANK', 'ROLE_OPERATOR_BANK_REPORTER', 'ROLE_AUTHORIZED_BANK_REPORTER'," +
    " 'ROLE_AUTHORIZED_BANK_REPORTER', 'ROLE_OPERATOR_BANK_REPORTER'))")

Method verifyRole:

public boolean verifyRole(Set<String> roles) {
  var grantedAuthorities =
    ((User) authentication.getPrincipal()).getGrantedAuthorities()
    .stream()
    .map(GrantedAuthority::getAuthority)
    .collect(Collectors.toSet());

  return !Collections.disjoint(grantedAuthorities, roles);
}

I try to run my test method with @WithMockUser(roles = {"HEAD_BANK","AUTHORIZED_BANK_REPORTER"}) and it fails in verifyRole method on (User) authentication.getPrincipal() with the next error:
java.lang.ClassCastException: class org.springframework.security.core.userdetails.User cannot be cast to class com.kilma.raw.domain.entity.User (org.springframework.security.core.userdetails.User and com.kilma.raw.domain.entity.User are in unnamed module of loader 'app')

I've tried different type of solving that moment and @WithMockUser the farest I could go. If there any other way to handle it please tell me? Maybe I need to work on SecurityContext but already tried and didn't work.

P.S. When I comment @PreAuthorize annotation everything works well.

My test code that fails on reportService.getReport, getReport method with @PreAuthorize annotation:

@Test
@WithMockUser(roles = {
  "HEAD_BANK",
  "AUTHORIZED_BANK_REPORTER"
})
void getReport_success() {
  final ReportResponse report = reportService.getReport(1 L, bank1MockedAuth).getPayload();

  final long publishedWithDataReportId = 1 L;
  assertEquals(publishedWithDataReportId, report.getId());
  final Instant instant = Instant.parse("2022-04-01T00:00:00.000Z");
  final Long testDate = instant.toEpochMilli();
  assertEquals(testDate, report.getCreatedAt());
  assertEquals(testDate, report.getModifiedAt());
  assertEquals(KEY_MORTGAGE_INDICATOR, report.getReportType());
  assertEquals(testDate, report.getReportPeriod());
  assertEquals(PUBLISHED, report.getStatus());
}      

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文