密码编码器匹配在Spring Boot中不起作用。

huangapple go评论55阅读模式
英文:

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

&#39;matches(java.lang.CharSequence, java.lang.String)&#39; in &#39;org.springframework.security.crypto.password.PasswordEncoder&#39; cannot be applied to &#39;(java.lang.String)&#39;

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 &quot;LoginDTO{&quot; +
                &quot;email=&#39;&quot; + email + &#39;\&#39;&#39; +
                &quot;, password=&#39;&quot; + password + &#39;\&#39;&#39; +
                &#39;}&#39;;
    }
}

EmployeeController.java

 @PostMapping(path = &quot;/login&quot;)
    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&lt;Employee,Integer&gt;
{
      Optional&lt;Employee&gt; 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&lt;Employee&gt; employee = employeeRepo.findOneByEmailAndPassword(loginDTO.getEmail(), passwordEncoder.matches(password,encodedPassword));

    if (employee.isPresent()) {
        return &quot;Login Success for &quot; + employee.get();
    } else {
        return &quot;Login Fail&quot;;
    }
}

}

答案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.

huangapple
  • 本文由 发表于 2023年2月16日 11:18:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467510.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定