英文:
Password Encoder Match not working Spring boot
问题
我正在创建使用哈希密码的Spring Boot应用程序登录。在没有设置哈希密码的情况下,登录成功。但是设置了哈希密码后,在编写代码时出现错误。
passwordEncoder.matches(loginDTO.getEmail())
错误提示为
'org.springframework.security.crypto.password.PasswordEncoder'中的'matches(java.lang.CharSequence, java.lang.String)'无法应用于'(java.lang.String)'
我尝试过的内容我附在下面的完整代码中。
LoginDTO.java
package com.example.Registration.Dto;
public class LoginDTO {
private String email;
private String password;
public LoginDTO() {
}
public LoginDTO(String email, String password) {
this.email = email;
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "LoginDTO{" +
"email='" + email + '\'' +
", password='" + password + '\'' +
'}';
}
}
EmployeeController.java
@PostMapping(path = "/login")
public String loginEmployee(@RequestBody LoginDTO loginDTO)
{
String email = employeeService.loginEmployee(loginDTO);
return email;
}
EmployeeService
public interface EmployeeService {
String loginEmployee(LoginDTO loginDTO);
}
EmployeeRepo
@EnableJpaRepositories
@Repository
public interface EmployeeRepo extends JpaRepository<Employee,Integer>
{
Optional<Employee> findOneByEmailAndPassword(String email, String password);
}
EmployeeIMPL
@Override
public String loginEmployee(LoginDTO loginDTO) {
CharSequence password = loginDTO.getPassword();
String encodedPassword = loginDTO.getPassword(); // 假设这是从数据库中获取的编码后的密码
Optional<Employee> employee = employeeRepo.findOneByEmailAndPassword(loginDTO.getEmail(), passwordEncoder.matches(password,encodedPassword));
if (employee.isPresent()) {
return "登录成功 for " + employee.get();
} else {
return "登录失败";
}
}
请确保你在代码中正确引入了相应的包,并且 passwordEncoder
已经正确初始化。
英文:
i am creating the Spring Boot application login with hash password.without set hash password login working successfully.with hash password set getting error while writing the code.
passwordEncoder.matches(loginDTO.getEmail())
it indicate the error was
'matches(java.lang.CharSequence, java.lang.String)' in 'org.springframework.security.crypto.password.PasswordEncoder' cannot be applied to '(java.lang.String)'
what i tried so far i attached the full code below.
LoginDTO.java
package com.example.Registation.Dto;
public class LoginDTO {
private String email;
private String password;
public LoginDTO() {
}
public LoginDTO(String email, String password) {
this.email = email;
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "LoginDTO{" +
"email='" + email + '\'' +
", password='" + password + '\'' +
'}';
}
}
EmployeeController.java
@PostMapping(path = "/login")
public String loginEmployee(@RequestBody LoginDTO loginDTO)
{
String email = employeeService.loginEmployee(loginDTO);
return email;
}
Employee Service
public interface EmployeeService {
String loginEmployee(LoginDTO loginDTO);
}
EmployeeRepo
@EnableJpaRepositories
@Repository
public interface EmployeeRepo extends JpaRepository<Employee,Integer>
{
Optional<Employee> findOneByEmailAndPassword(String email, String password);
}
EmployeeIMPL
@Override
public String loginEmployee(LoginDTO loginDTO) {
CharSequence password = loginDTO.getPassword();
String encodedPassword = loginDTO.getPassword(); // assume this is the encoded password from the database
Optional<Employee> employee = employeeRepo.findOneByEmailAndPassword(loginDTO.getEmail(), passwordEncoder.matches(password,encodedPassword));
if (employee.isPresent()) {
return "Login Success for " + employee.get();
} else {
return "Login Fail";
}
}
}
答案1
得分: -1
// 密码编码器bean
@Autowired
private PasswordEncoder passwordEncoder;
// 检查密码
if (passwordEncoder.matches(changePasswordRequest.getOldPassword(), user.getPassword())){
}
Both password type is
String `OldPassword and Password type is `String
英文:
import org.springframework.security.crypto.password.PasswordEncoder;
//PasswordEncoder bean
@Autowired
private PasswordEncoder passwordEncoder;
//check password
if (passwordEncoder.matches(changePasswordRequest.getOldPassword(), user.getPassword())){
}
Both password type is String
.
OldPassword and Password type is String
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论