【发布时间】:2018-03-18 20:04:38
【问题描述】:
我正在使用 Spring Boot 1.4.2,而且我是 Spring Boot 新手。 我有一个身份验证过滤器来设置用户登录时的当前用户信息。 在对控制器的建议中,我调用了获取当前 userId,如下所示:
public static String getCurrentUserToken(){
return ((AuthenticatedUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserId();
}
这是我的自定义 AuthenticatedUser:
public class AuthenticatedUser implements Serializable {
private final String userName;
private final String userId;
private final String sessionId;
public AuthenticatedUser(String userName, String userId, String sessionId) {
super();
this.userName = userName;
this.userId = userId;
this.sessionId = sessionId;
}
public String getUserName() {
return userName;
}
public String getUserId() {
return userId;
}
public String getSessionId() {
return sessionId;
}
}
一切正常。 但是,过滤器在集成测试中不起作用,我需要模拟当前用户。 我搜索了很多关于如何模拟用户的内容,但没有一个能帮助我。我终于找到了这个可能接近我想要的指南:https://aggarwalarpit.wordpress.com/2017/05/17/mocking-spring-security-context-for-unit-testing/ 以下是我遵循该指南的测试课程:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
public class PersonalLoanPreApprovalTest {
@Before
public void initDB() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Test
public void testRequestPersonalLoanPreApproval_Me() {
AuthenticatedUser applicationUser = new
AuthenticatedUser("test@abc.com", "2d1b5ae3", "123");
UsernamePasswordAuthenticationToken authentication = new ApiKeyAuthentication(applicationUser);
SecurityContext securityContext = mock(SecurityContext.class);
when(securityContext.getAuthentication()).thenReturn(authentication);
SecurityContextHolder.setContext(securityContext);
// error at this line
when(securityContext.getAuthentication().getPrincipal()) .thenReturn(applicationUser);
// The controller for this api has the advice to get the userId
MyResponse response = restTemplate.getForObject(url.toString(), MyResponse.class);
}
}
我收到了这个错误:
org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
AuthenticatedUser cannot be returned by getAuthentication()
getAuthentication() should return Authentication
我已经花了几天的时间,尝试了很多我找到的建议,但仍然失败。我还尝试删除导致错误的行,但是当错误消失后,我仍然无法在控制器建议中获取当前用户信息。
非常感谢任何建议。
UPDATE1:只是想通过对代码进行一些修改来获得我的结果。
根据@glitch 的建议,我更改了代码以在我的测试方法中模拟身份验证和用户,如下所示:
@Test
public void testRequestPersonalLoanPreApproval_Me() {
AuthenticatedUser applicationUser = new AuthenticatedUser("testtu_free@cs.com", "2d1b5ae3-cf04-44f5-9493-f0518cab4554", "123");
Authentication authentication = Mockito.mock(Authentication.class);
SecurityContext securityContext = Mockito.mock(SecurityContext.class);
Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
SecurityContextHolder.setContext(securityContext);
Mockito.when(authentication.getPrincipal()).thenReturn(applicationUser);
// The controller for this api has the advice to get the userId
MyResponse response = restTemplate.getForObject(url.toString(), MyResponse.class);
}
}
我现在可以摆脱测试类中的错误。我调试了代码,当我在测试类中时,看到 securityContext 具有价值。但是当我跳转到控制器建议中的代码时,下面的 get 返回 null:
SecurityContextHolder.getContext().getAuthentication().getPrincipal()
【问题讨论】:
-
试试:
when(securityContext.getAuthentication()).thenReturn((Authentication)authentication); -
感谢@mangotang 的建议。我试过了,仍然有旧错误:
when(securityContext.getAuthentication().getPrincipal()).thenReturn(applicationUser);。你对我还有什么建议吗?
标签: java authentication spring-boot mockito