简体   繁体   English

无法在数据库中使用 BCryptPassword 保存加密密码

[英]Unable to save an encrypted password with BCryptPassword in database

I am trying to save an encrypted password in postgreSQL database with BCrypt and I have an error.我正在尝试使用 BCrypt 在 postgreSQL 数据库中保存加密密码,但出现错误。 I saw the other answers on stackoverflow I tried to use those hints but I have the same error:This application has no explicit mapping for /error, so you are seeing this as a fallback.我在 stackoverflow 上看到了其他答案 我尝试使用这些提示,但我有同样的错误:This application has no explicit mapping for /error,所以你将其视为后备。

Tue Aug 25 19:37:41 EEST 2020 There was an unexpected error (type=Internal Server Error, status=500). 2020 年 8 月 25 日星期二 19:37:41 EEST 出现意外错误(类型=内部服务器错误,状态=500)。 Could not commit JPA transaction;无法提交 JPA 事务; nested exception is javax.persistence.RollbackException: Error while committing the transaction org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction;嵌套异常是 javax.persistence.RollbackException:提交事务时出错 org.springframework.transaction.TransactionSystemException:无法提交 JPA 事务; nested exception is javax.persistence.RollbackException: Error while committing the transaction com.sun.proxy.$Proxy76.save(Unknown Source).... at com.car.carDealer.service.UserService.saveUser(UserService.java:38) at com.car.carDealer.controller.Register.register(Register.java:46)嵌套异常是 javax.persistence.RollbackException: Error while committing the transaction com.sun.proxy.$Proxy76.save(Unknown Source).... 在 com.car.carDealer.service.UserService.saveUser(UserService.java:38 ) 在 com.car.carDealer.controller.Register.register(Register.java:46)

My code is:我的代码是:

    > > @Configuration 
         public class WebConfig implements WebMvcConfigurer {
    >     @Bean
    >     public BCryptPasswordEncoder passwordEncoder() {
    >         return new BCryptPasswordEncoder();
    >     } 
}
public interface UserRepository extends CrudRepository<User,Integer> {
        User findByEmail(String email);
        User findByEmailAndPassword(String email,String password);
}
@Service
public class UserService {

    @Autowired
    private final UserRepository userRep;

    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    BCryptPasswordEncoder bCryptEncoder;

    @Autowired
    public UserService(UserRepository userRep) {
        this.userRep = userRep;
    }

    public User findByEmailAndPassword(String email, String password) {
        return userRep.findByEmailAndPassword(email, password);
    }

    public User findByEmail(String email) {
        return userRep.findByEmail(email);
    }

    public void saveUser(User user) {
        userRep.save(user);
    }
 @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String register(
            @Valid User user, BindingResult result, Model model) {
    @PostMapping("/register")
    public String register(@Valid User user, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return "user/register";
        } else {

            User valid = userService.findByEmail(user.getEmail());
            if (valid == null) {
                user.setPassword(bCryptEncoder.encode(user.getPassword()));
                userService.saveUser(user);
                String msg = " succes!";
                model.addAttribute("msg", msg);
                return "user/register";
            } else {
                String msg = "Please choose another email !";
                model.addAttribute("msg", msg);

I've tried it out.我试过了。 Once I remove the validation it ecrypts the password & store it in database.一旦我删除验证,它就会加密密码并将其存储在数据库中。 But when I put validations like: password must be 8 to 15 characters, and Even I fulfil this condition but it does not store it in data and gives this error但是当我进行如下验证时:密码必须为 8 到 15 个字符,即使我满足了这个条件,但它不会将其存储在数据中并给出此错误

Could not commit JPA transaction;无法提交 JPA 事务; nested exception is javax.persistence.RollbackException: Error while committing the transaction嵌套异常是 javax.persistence.RollbackException: Error while committing the transaction

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM