@Test @WithMockUser(USERNAME_USER) void validateCurrentUserPassword() { User existingUser = mock(User.class); when(existingUser.getPassword()).thenReturn("encrypted-password"); when(passwordEncoder.matches("password", "encrypted-password")).thenReturn(true); when(userService.getUser(USERNAME_USER)).thenReturn(existingUser); assertTrue(userAccountServiceImpl.validateCurrentUserPassword("password")); assertFalse(userAccountServiceImpl.validateCurrentUserPassword("wrong-password")); }
@Override @PreAuthorize("hasAnyRole('ROLE_SU', 'ROLE_USER')") @Transactional public boolean validateCurrentUserPassword(String password) { if (password == null || password.isEmpty()) return false; String currentUsername = SecurityUtils.getCurrentUsername(); User currentUser = userService.getUser(currentUsername); if (currentUser == null) { throw new RuntimeException( "User does not exist [" + SecurityUtils.getCurrentUsername() + "]"); } return passwordEncoder.matches(password, currentUser.getPassword()); }
UserAccountServiceImpl implements UserAccountService { @Override @PreAuthorize("hasAnyRole('ROLE_SU', 'ROLE_USER')") @Transactional public boolean validateCurrentUserPassword(String password) { if (password == null || password.isEmpty()) return false; String currentUsername = SecurityUtils.getCurrentUsername(); User currentUser = userService.getUser(currentUsername); if (currentUser == null) { throw new RuntimeException( "User does not exist [" + SecurityUtils.getCurrentUsername() + "]"); } return passwordEncoder.matches(password, currentUser.getPassword()); } }
UserAccountServiceImpl implements UserAccountService { @Override @PreAuthorize("hasAnyRole('ROLE_SU', 'ROLE_USER')") @Transactional public boolean validateCurrentUserPassword(String password) { if (password == null || password.isEmpty()) return false; String currentUsername = SecurityUtils.getCurrentUsername(); User currentUser = userService.getUser(currentUsername); if (currentUser == null) { throw new RuntimeException( "User does not exist [" + SecurityUtils.getCurrentUsername() + "]"); } return passwordEncoder.matches(password, currentUser.getPassword()); }  }
UserAccountServiceImpl implements UserAccountService { @Override @PreAuthorize("hasAnyRole('ROLE_SU', 'ROLE_USER')") @Transactional public boolean validateCurrentUserPassword(String password) { if (password == null || password.isEmpty()) return false; String currentUsername = SecurityUtils.getCurrentUsername(); User currentUser = userService.getUser(currentUsername); if (currentUser == null) { throw new RuntimeException( "User does not exist [" + SecurityUtils.getCurrentUsername() + "]"); } return passwordEncoder.matches(password, currentUser.getPassword()); }  @Override @Transactional(readOnly = true) User getCurrentUser(); @Override @PreAuthorize("hasAnyRole('ROLE_SU', 'ROLE_USER')") @Transactional void updateCurrentUser(User updatedCurrentUser); @Override @PreAuthorize("hasAnyRole('ROLE_SU', 'ROLE_USER')") @Transactional boolean validateCurrentUserPassword(String password); void setPasswordEncoder(PasswordEncoder passwordEncoder); }
UserAccountServiceImpl implements UserAccountService { @Override @PreAuthorize("hasAnyRole('ROLE_SU', 'ROLE_USER')") @Transactional public boolean validateCurrentUserPassword(String password) { if (password == null || password.isEmpty()) return false; String currentUsername = SecurityUtils.getCurrentUsername(); User currentUser = userService.getUser(currentUsername); if (currentUser == null) { throw new RuntimeException( "User does not exist [" + SecurityUtils.getCurrentUsername() + "]"); } return passwordEncoder.matches(password, currentUser.getPassword()); }  @Override @Transactional(readOnly = true) User getCurrentUser(); @Override @PreAuthorize("hasAnyRole('ROLE_SU', 'ROLE_USER')") @Transactional void updateCurrentUser(User updatedCurrentUser); @Override @PreAuthorize("hasAnyRole('ROLE_SU', 'ROLE_USER')") @Transactional boolean validateCurrentUserPassword(String password); void setPasswordEncoder(PasswordEncoder passwordEncoder);  }