rawpassword不能为空异常

3xiyfsfu  于 2021-07-13  发布在  Java
关注(0)|答案(2)|浏览(1280)

我有以下问题:
我想测试一个用户是否成功注册。我不是通过用户界面而是通过 Postman 。问题是当我使用post命令时出现了一个异常。我已经检查了所有注解。
Postman 截图
例外情况:
java.lang.illegalargumentexception:org.springframework.security.crypto.bcrypt.bcryptpasswordencoder.encode(bcryptpasswordencoder)中的rawpassword不能为空。java:103)~[spring-security-core-5.3.3.发布。jar:5.3.3.release]在com.example.application.backend.data.service.userservice.singupuser(userservice。java:41)~[classes/:na]在com.example.application.backend.data.registration.registrationservice.register(注册服务)。java:22)~[classes/:na]位于com.example.application.backend.data.registration.registrationcontroller.register(registrationcontroller)。java:18)
这是我的userservice类:

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";
}

注册服务类别:

@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;
}

注册控制器类:

@Autowired
private RegistrationService registrationService;

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

n7taea2i1#

当提供给bcryptencoder的密码字符串为空或null时,会发生此错误。
在userservice.java类中,

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

user.getpassword()为null,这意味着在registrationservice.java类中,

request.getPassword()

正在获取空值。
请检查您是否从请求对象获取了密码的正确参数名。如果可能,在代码中添加一些logger语句并进行调试。
github公关

jaxagkaj

jaxagkaj2#

它正在工作。问题是@column注解被设置为nullable false。

相关问题