在SecurityContext中注入身份验证,用于JUNIT测试(Java+ Spring)
我有一种用于待办事项方法的测试方法。服务方法具有下一个注释:
@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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论