为什么验证不适用于编码密码

发布于 2025-01-15 09:02:29 字数 1626 浏览 0 评论 0原文

在我的项目中,我为注册表单字段添加了一些验证。点击提交按钮密码验证时,不会检查原始密码,如 @AAAzzz123,而是检查编码密码,如 $2a$10$kUm6AxxH3SNSIoUtP6V7WOlFTIORTOILKDFGOP 并产生验证错误消息。这是我的代码。

实体

public class User {
    ...
    ...
    ...

    @Pattern(regexp = "(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[^\\da-zA-Z]).{8,15}$")
    private String password;
}

控制器

@RequestMapping(value = "/register", method = RequestMethod.POST)
    public String resiterUser(@Valid @ModelAttribute("user") User user, 
                                  BindingResult result)
    {
        
        if(result.hasErrors())
        {
            return "signup";
        }

        // Problem is here
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        userRepo.save(user);
        return "redirect:/";
}

视图

<form th:action="@{/register}" method="post">
    
    <div class="form-outline mb-4">
        <label class="form-label" for="form3Example4cg">Password</label>
        <input type="password" 
            th:classappend="${#fields.hasErrors('password') ? 'is-invalid' : ''}" 
            class="form-control form-control-lg"
            name="password" />
        <div id="validation" class="text-danger" th:each="e: ${#fields.errors('password')}" th:text=${e}>

        </div>
    </div>
    
    <div class="d-flex">
        <button type="submit" class="btn bg-primary">Submit</button>
    </div>
    
</form>

In my project i add some validation for Signup form fields. While click on submit button password validation not check orignal password like @AAAzzz123 but it check encoded password like $2a$10$kUm6AxxH3SNSIoUtP6V7WOlFTIORTOILKDFGOP and produce validation error message. Here down is my code.

Entity

public class User {
    ...
    ...
    ...

    @Pattern(regexp = "(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[^\\da-zA-Z]).{8,15}
quot;)
    private String password;
}

Controller

@RequestMapping(value = "/register", method = RequestMethod.POST)
    public String resiterUser(@Valid @ModelAttribute("user") User user, 
                                  BindingResult result)
    {
        
        if(result.hasErrors())
        {
            return "signup";
        }

        // Problem is here
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        userRepo.save(user);
        return "redirect:/";
}

View

<form th:action="@{/register}" method="post">
    
    <div class="form-outline mb-4">
        <label class="form-label" for="form3Example4cg">Password</label>
        <input type="password" 
            th:classappend="${#fields.hasErrors('password') ? 'is-invalid' : ''}" 
            class="form-control form-control-lg"
            name="password" />
        <div id="validation" class="text-danger" th:each="e: ${#fields.errors('password')}" th:text=${e}>

        </div>
    </div>
    
    <div class="d-flex">
        <button type="submit" class="btn bg-primary">Submit</button>
    </div>
    
</form>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨落星ぅ辰 2025-01-22 09:02:29

验证有两个阶段 - MVC 层和 JPA 层。如果对两个层使用相同的模型类,则不能对两个不同的事物使用相同的字段。

您可以像这样将它们分开,例如:

@Entity
@Table(name = "USER")
public class User {

    @Column(name = "password")
    @Pattern(regexp = "^\\$2[aby]\\$.{56}$")
    private String encodedPassword

    // for form binding only
    private transient String newPassword;

}
user.setEncodedPassword(bCryptPasswordEncoder.encode(user.getNewPassword()));

There are two phases of validation - the MVC layer and the JPA layer. If you use the same model class for both layers then you can't use the same field for two different things.

You can separate them like this, for example:

@Entity
@Table(name = "USER")
public class User {

    @Column(name = "password")
    @Pattern(regexp = "^\\$2[aby]\\$.{56}
quot;)
    private String encodedPassword

    // for form binding only
    private transient String newPassword;

}
user.setEncodedPassword(bCryptPasswordEncoder.encode(user.getNewPassword()));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文