rawPassword cannot be null exception

Viewed 3692

I have the following problem:

I want to test if a user registers successfully. I'm not doing this over a UI but via Postman. The problem is that I get an exception when I use the POST Command. I have already checked all annotations.

Postman Screenshot

Exception:

java.lang.IllegalArgumentException: rawPassword cannot be null at org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder.encode(BCryptPasswordEncoder.java:103) ~[spring-security-core-5.3.3.RELEASE.jar:5.3.3.RELEASE] at com.example.application.backend.data.service.UserService.singUpUser(UserService.java:41) ~[classes/:na] at com.example.application.backend.data.registration.RegistrationService.register(RegistrationService.java:22) ~[classes/:na] at com.example.application.backend.data.registration.RegistrationController.register(RegistrationController.java:18)

This is my UserService Class:

private final static  String USER_NOT_FOUND_MSG = "user with email %s not found";
@Autowired
private final UserRepository userRepository;
@Autowired
private final BCryptPasswordEncoder bCryptPasswordEncoder;

public UserService(
        UserRepository userRepository,
        BCryptPasswordEncoder bCryptPasswordEncoder) {
    this.userRepository = userRepository;
    this.bCryptPasswordEncoder = bCryptPasswordEncoder;
}

@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
    return userRepository.findByEmail(email)
            .orElseThrow(() ->
                    new UsernameNotFoundException(String.format(USER_NOT_FOUND_MSG, email)));
}

public String singUpUser(User user) {
    boolean userExists = userRepository.findByEmail(user.getEmail()).isPresent();

    if(userExists){
        throw new IllegalStateException("email already taken");
    }

    String encodedPassword = bCryptPasswordEncoder.encode(user.getPassword());

    user.setPassword(encodedPassword);

    userRepository.save(user);

    return "it works";
}

Registration Service Class:

@Autowired
private final UserService userService;
private final EmailValidator emailValidator;

public String register(RegistrationRequest request) {
    boolean isValidEmail = emailValidator.test(request.getEmail());
    if (!isValidEmail) {
        throw new IllegalStateException("email not valid");
    }
    return userService.singUpUser(
            new User(
                    request.getFirstName(),
                    request.getLastName(),
                    request.getEmail(),
                    request.getPassword(),
                    UserRole.USER
            )
    );
}

public RegistrationService(UserService userService, EmailValidator emailValidator) {
    this.userService = userService;
    this.emailValidator = emailValidator;
}

Registration Controller Class:

@Autowired
private RegistrationService registrationService;

@PostMapping
public String register(@RequestBody RegistrationRequest request) {
    return registrationService.register(request);
}
3 Answers

This error occurs when the password string that is fed to the bCryptEncoder is empty or null.

In your UserService.java class,

String encodedPassword = bCryptPasswordEncoder.encode(user.getPassword());

user.getPassword() is null, which means in RegistrationService.java class,

request.getPassword()

is fetching a null value.

Please check if you are getting the correct parameter name for password from the request object. If possible, add some logger statements in your code and debug.

GitHub PR

It is working. The problem was that the @Column annotations were set to nullable false.

Expanding @Sidharth's answer. For anyone experiencing this problem check your password getter at AppUser.java class. It is probably returning null.

Change

public String getPassword(){return null;}

To

public String getPassword(){return password;}
Related