I'm writing an application using Vaadin 8 and Spring Boot / Spring Data that needs to implement a simple login system. I have a table in PostgreSQL hosting user details, and to log-in I wanted to move the password check to the DB side by using a stored procedure.
Thing is, I have the following class that handles the authentication:
public class Authentication {
@Autowired
private JdbcTemplate jdbcTemplate;
public Boolean authenticate(String username, String password) {
String passwordCheck = "SELECT crypt(?, password) = password " +
"FROM usuaris " +
"WHERE usuari = ?;";
PreparedStatementCallback<Boolean> psCallback = (ps) -> {
ps.setString(1, password);
ps.setString(2, username);
return ps.execute();
};
return jdbcTemplate.execute(passwordCheck, psCallback);
}
}
That gets used in a Login Dialog like so:
public class LoginDialog extends Window {
@Autowired
Authentication auth;
private void tryToLogIn() {
if (auth.authenticate(username.getValue(), password.getValue())) {
Notification.show("Login successful!", Notification.Type.HUMANIZED_MESSAGE);
} else {
Notification.show("Invalid credentials", Notification.Type.ERROR_MESSAGE);
}
}
}
When I try to debug the application, it keeps throwing a NPE in the authenticate() method with the Autowired Authentication class.
I have tried annotating the Authentication class as a @Component, @Autowiring a setter instead of the JdbcTemplate, and everything keeps failing.
Does anybody know what could I be missing here?
Thank you!
EDIT1: I have checked the question this one could be a duplicate of, and I made sure I'm not creating the Authentication class as New. I've also tried using the @Configurable annotation and it still does throw the NPE.
I should be able to Autowire the class, so why is it still not working? I forgot to mention I tried to just autowire a standalone JdbcTemplate in the LoginDialog class, and it still threw a NPE.