Java Spring启动测试:Bcrypt密码编码器与密码不匹配
正在尝试测试我的spring boot应用程序的Java Spring启动测试:Bcrypt密码编码器与密码不匹配,java,spring,spring-boot,Java,Spring,Spring Boot,正在尝试测试我的spring boot应用程序的login实现。代码非常简单,不确定缺少什么 UserController.java public BaseResponse login(@RequestBody UserCredential credential, HttpServletRequest request, HttpServletResponse response) throws ServletException { Authenticatio
login
实现。代码非常简单,不确定缺少什么
UserController.java
public BaseResponse login(@RequestBody UserCredential credential, HttpServletRequest request,
HttpServletResponse response) throws ServletException {
Authentication authentication = tokenAuthenticationService.getAuthentication(credential);
org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) authentication
.getDetails();
UserDetails existingUser = userService.loadUserByUsername(credential.getUsername());
if (existingUser == null || !passwordEncoder.matches(credential.getPassword(), existingUser.getPassword())) {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
return BaseResponse.builder().status(HttpServletResponse.SC_UNAUTHORIZED).message("Incorrect credentials")
.build();
} else {
return BaseResponse.builder().status(HttpServletResponse.SC_OK).message(token).build();
}
}
@Bean
public PasswordEncoder passwordEncoder() {
PasswordEncoder encoder = new BCryptPasswordEncoder();
return encoder;
}
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.H2)
public class UserControllerTest {
@Test
public void login(){
try {
UserCredential credentials = UserCredential.builder().username("test").password("password").build();
dao.save(credentials);
String response = mvc.perform(post("/login")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(credentials)))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
System.out.println(">>>>>>>>>>> Login response "+response);
} catch (Exception e) {
e.printStackTrace();
}
}
UserControllerTest.java
public BaseResponse login(@RequestBody UserCredential credential, HttpServletRequest request,
HttpServletResponse response) throws ServletException {
Authentication authentication = tokenAuthenticationService.getAuthentication(credential);
org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) authentication
.getDetails();
UserDetails existingUser = userService.loadUserByUsername(credential.getUsername());
if (existingUser == null || !passwordEncoder.matches(credential.getPassword(), existingUser.getPassword())) {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
return BaseResponse.builder().status(HttpServletResponse.SC_UNAUTHORIZED).message("Incorrect credentials")
.build();
} else {
return BaseResponse.builder().status(HttpServletResponse.SC_OK).message(token).build();
}
}
@Bean
public PasswordEncoder passwordEncoder() {
PasswordEncoder encoder = new BCryptPasswordEncoder();
return encoder;
}
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.H2)
public class UserControllerTest {
@Test
public void login(){
try {
UserCredential credentials = UserCredential.builder().username("test").password("password").build();
dao.save(credentials);
String response = mvc.perform(post("/login")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(credentials)))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
System.out.println(">>>>>>>>>>> Login response "+response);
} catch (Exception e) {
e.printStackTrace();
}
}
}只是猜测,但可能是因为现有用户密码未以编码形式保存(即,我认为您是以明文形式存储“测试”用户密码,而不是以BCrypt编码哈希格式存储)?@ShawnSherwood否。事实并非如此<代码>测试用户密码保存为BCrypt编码的哈希。如果没有看到更多代码,我就不能说了。但是有几个问题-UserController中的身份验证和用户对象的用途是什么?这些变量似乎没有被使用。@Bean passwordEncoder()似乎也没有使用(这是本地类变量吗?)。您是否已通过调试器并验证现有User.getPassword()是否已按预期设置?