Spring Security 我已经尝试了所有这些东西,但它是说它找不到实体id

a9wyjsp7  于 2023-08-05  发布在  Spring
关注(0)|答案(1)|浏览(104)

我已经尝试了所有这些东西,但它是说它找不到实体ID,我得到以下错误。
org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“authController”的Bean时出错:通过字段“userService”表示的未满足的依赖关系:创建名为'userServiceImpl'的bean时出错:通过字段“userRepo”表示的未满足的依赖关系:创建在com.example.demo.repositories中定义的名为“userRepo”的bean时出错。在JpaRepositoriesRegistrar上声明的@EnableJpaRepositories中定义的UserRepo。EnableJpaRepositoriesConfiguration:无法为公共抽象java.util创建查询。可选com.example.demo.repositories.UserRepo.findOneByEmailIdAndPassword(java.lang.String,java.lang.String);原因:无法为方法public abstract java.util.Optional com.example.demo.repositories.UserRepo.findOneByEmailIdAndPassword(jav your text a.lang.String,java.lang.String)创建查询;未找到类型“String”的属性“id”;遍历路径:User.email
用户控制器

@RestController
@RequestMapping("/users")
@CrossOrigin("*")
public class UserController {

        @Autowired
    private UserService userService;

    @Autowired
    private UserRepo userRepo;

    @PostMapping("/login")
    public ResponseEntity<?> loginEmployee(@RequestBody LoginDto loginDto)
    {
        LoginMessage loginMessage=userService.loginUser(loginDto);
    return ResponseEntity.ok(loginMessage);
    }
}

字符串
用户服务

@Service
public interface UserService {

String addUser(UserDto userDto);
LoginMessage loginUser(LoginDto loginDto);

}


UserServiceImplementation

@Service
public class UserServiceImpl implements UserService {

@Autowired
private UserRepo userRepo;

@Autowired
private ModelMapper modelMapper;

@Override
public String addUser(UserDto userDto) {
    User user=new User(
    userDto.getId(),
    userDto.getName(),
    userDto.getEmail(),
    userDto.getPassword()
    );
    userRepo.save(user);
    return user.getName();
}

private PasswordEncoder passwordEncoder;

@Override
public LoginMessage loginUser(LoginDto loginDto)
{
    String msg="";
    User user1=userRepo.findByEmail(loginDto.getEmail());
    if(user1 !=null)
    {
        String password=loginDto.getPassword();
        String encodedPassword=user1.getPassword();
        Boolean isPwdRight=passwordEncoder.matches(password,encodedPassword);
        if(isPwdRight)
        {
            Optional<User>
      user=userRepo.findOneByEmailIdAndPassword(loginDto.getEmail(),encodedPassword);
            if(user.isPresent())
            {
                return new LoginMessage("Login Success", true);
            }
            else
            {
                return new LoginMessage("Login Failed", false);
            }
        }
        else
        {
            return new LoginMessage("Password Not Match", false);
        }
    }
    else
    {
        return new LoginMessage("Email not exist", false);
    }
}   
 }


UserRepository

@EnableJpaRepositories
@Repository
public interface UserRepo extends JpaRepository<User, Integer>{

Optional<User> findOneByEmailIdAndPassword(String email, String password);

User findByEmail(String email);

}

1zmg4dgp

1zmg4dgp1#

这里的问题似乎是使用Spring Data 错误地构建了查询。

findOneByEmailIdAndPassword

字符串
这看起来是错误消息中的问题。也许还可以添加User.java类来检查查询是否写得很好

相关问题