3

I have to do in Spring Boot an LoginController which verifies if a username and password exists in database or not. For verification I made my repository, UserRepository which extends CrudRepository.

When I send the username and password to repository, they are sent okay, but when I do the query it doesn't take into account the case sensitivity of my parameters.

For example: in USER table I have username = "Test" and password = "test" If I make an request with username = "test" and password = "Test" it seems like I have this username and password in the table, but I haven't.

Bellow I put a peace of code with my query and I would be very grateful if you could help me with this problem.

Thank you for your time.

@Repository
public interface UserRepository extends CrudRepository<User, Long>{

User findByUsername(String username);

User findByEmail(String email);

@Query(value = "SELECT user_id FROM user WHERE username = :username AND password = :password", nativeQuery = true)
Long findUserIdByUsernameAndPassword(@Param("username") String username, @Param("password") String password);

}

9
  • I believe the default for these is supposed to be case-sensitive, because IgnoreCase is a special keyword for Spring Data JPA queries. findByEmail should be case-sensitive, but findByEmailIgnoreCase would not be case-sensitive. What have you tried to check the values being passed into the function and the values on the DB are not the same casing?
    – Wrokar
    Aug 8, 2018 at 20:55
  • Yes, I verified. After I make the request in postman I print the username and password and they are okay. And from DB.... I've made a query which returns the username for one specifically user_id and returns well.
    – Cati
    Aug 8, 2018 at 20:57
  • 1
    You may want to look at this answer to a similar question. Depending on how the MySQL db is created, strings may be case-insensitive by default apparently.
    – Wrokar
    Aug 8, 2018 at 21:01
  • And I tried to create the method without using query annotation, just declaration of the method, but I can't select just user_id like I do right now. I have to change from "Long findByUsernameAndPassword" to "User findByUsernameAndPassword"
    – Cati
    Aug 8, 2018 at 21:02
  • 1
    This issue has to be down at the database level, not at the Spring level. And MySQL is known to be problematic - it performs case insensitive comparisons when you might expect case sensitivity. This page has some information that may be useful to you. Aug 8, 2018 at 21:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy