java Spring 数据中带有@Pattern 注释的正则表达式验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48795068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 10:10:29  来源:igfitidea点击:

Regex validation with @Pattern annotation in spring data

javaregexspring-dataconstraints

提问by rbednarska

I need password validation (for example a password must contain at least 4 chars, max 8 and min 1 numeric digit). I have model (of course with getters and setters):

我需要密码验证(例如密码必须包含至少 4 个字符,最多 8 个和最少 1 个数字)。我有模型(当然有 getter 和 setter):

   @Entity
public class User implements Serializable{


    /**
     * 
     */
    private static final long serialVersionUID = 5534441879591858724L;

    @Id
    @GeneratedValue
    private long id;

    @NotBlank
    @Email
    private String email;

    @Pattern(regexp = "^(?=.*\d).{4,8}$", flags = Flag.UNICODE_CASE)
    private String password;

    @NotBlank
    @Size(min=2, max=30)
    private String name;

I'm catching ConstraintViolationException during saving user info to database and use informations from this exception to inform the user during registration what fields must be corrected because of invalid length etc.

我在将用户信息保存到数据库期间捕获 ConstraintViolationException 并使用此异常中的信息在注册期间通知用户由于长度无效等原因必须更正哪些字段。

Everything is ok with validation, but not with password. I checked regex expression out of this model class and it works ok, but when i put this regex into annotation parameter ( @Pattern(regexp = "^(?=.*\\d).{4,8}$", flags = Flag.UNICODE_CASE)) it doesn't work and I have an error:

验证一切正常,但密码不行。我从这个模型类中检查了正则表达式,它工作正常,但是当我把这个正则表达式放入注释参数 ( @Pattern(regexp = "^(?=.*\\d).{4,8}$", flags = Flag.UNICODE_CASE)) 时,它不起作用,我有一个错误:

HHH000346: Error during managed flush [Validation failed for classes [pl.rpf.kingdom.models.User] during persist time for groups [javax.validation.groups.Default, ] List of constraint violations:[ ConstraintViolationImpl{interpolatedMessage='must match "^(?=.*\d).{4,8}$"', propertyPath=password, rootBeanClass=class pl.rpf.kingdom.models.User, messageTemplate='{javax.validation.constraints.Pattern.message}'} ]]

HHH000346:托管刷新期间出错 [类 [pl.rpf.kingdom.models.User] 的持久化期间的类验证失败 [javax.validation.groups.Default,] 约束违规列表:[ ConstraintViolationImpl{interpolatedMessage='must match "^(?=.*\d).{4,8}$"', propertyPath=password, rootBeanClass=class pl.rpf.kingdom.models.User, messageTemplate='{javax.validation.constraints.Pattern.message }'} ]]

Please help me with understanding this error and maybe you have some idea how to solve this problem.

请帮助我理解此错误,也许您对如何解决此问题有所了解。

回答by rbednarska

Problem was with password encrypting, I forgot about it. Regex matches mathod was always false, because it was trying to compare password after encrypting. I solved problem by putting validation out of model class, before password encrypting. Other way to resolve problem could be using spring @Validannotation to validate form before trying save it to database (in my situation it could be problematic from other cases).

问题是密码加密,我忘记了。正则表达式匹配 mathod 总是错误的,因为它试图在加密后比较密码。我通过在密码加密之前将验证放在模型类之外来解决问题。解决问题的其他方法可能是@Valid在尝试将表单保存到数据库之前使用 spring注释来验证表单(在我的情况下,其他情况可能会出现问题)。