I'm new to spring, and i'm trying to create my own user and authenticate it. So far everything works, i can login and logout, but i can't autowire my userrepository in the authentication success/failure handler. I can do it everywhere else, for example in a controller.
My handler looks like this:
@Service
public class MyAuthenticationSuccessHandler extends
SavedRequestAwareAuthenticationSuccessHandler {
@Autowired
UserRepository userRepository;
@Override
public void onAuthenticationSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication)
throws ServletException, IOException {
[...]
User user = (User)authentication.getPrincipal();
user.setLastLogin(now);
userRepository.save(user);
[...]
}
}
when i'm trying to save the user, i get a nullpointer exception, because the userrepository is null.
The userrepository looks like this:
@Repository
public interface UserRepository extends CrudRepository<User, Long> {
Optional<User> findByName(String name);
}
However i can autowire it everywhere else the same way i'm trying in the sucesshandler. For example in a controller.
Could someone tell me what am i doing wrong?