• <legend id='D3jsV'><style id='D3jsV'><dir id='D3jsV'><q id='D3jsV'></q></dir></style></legend>

      <i id='D3jsV'><tr id='D3jsV'><dt id='D3jsV'><q id='D3jsV'><span id='D3jsV'><b id='D3jsV'><form id='D3jsV'><ins id='D3jsV'></ins><ul id='D3jsV'></ul><sub id='D3jsV'></sub></form><legend id='D3jsV'></legend><bdo id='D3jsV'><pre id='D3jsV'><center id='D3jsV'></center></pre></bdo></b><th id='D3jsV'></th></span></q></dt></tr></i><div id='D3jsV'><tfoot id='D3jsV'></tfoot><dl id='D3jsV'><fieldset id='D3jsV'></fieldset></dl></div>

        <small id='D3jsV'></small><noframes id='D3jsV'>

        <tfoot id='D3jsV'></tfoot>
        • <bdo id='D3jsV'></bdo><ul id='D3jsV'></ul>

      1. 使用嵌套的允许字段播放框架 2 (Java) 表单数据绑定

        时间:2024-08-24
          <i id='VcyGE'><tr id='VcyGE'><dt id='VcyGE'><q id='VcyGE'><span id='VcyGE'><b id='VcyGE'><form id='VcyGE'><ins id='VcyGE'></ins><ul id='VcyGE'></ul><sub id='VcyGE'></sub></form><legend id='VcyGE'></legend><bdo id='VcyGE'><pre id='VcyGE'><center id='VcyGE'></center></pre></bdo></b><th id='VcyGE'></th></span></q></dt></tr></i><div id='VcyGE'><tfoot id='VcyGE'></tfoot><dl id='VcyGE'><fieldset id='VcyGE'></fieldset></dl></div>
            <tfoot id='VcyGE'></tfoot>

              • <bdo id='VcyGE'></bdo><ul id='VcyGE'></ul>
                  <tbody id='VcyGE'></tbody>

              • <small id='VcyGE'></small><noframes id='VcyGE'>

                  <legend id='VcyGE'><style id='VcyGE'><dir id='VcyGE'><q id='VcyGE'></q></dir></style></legend>
                  本文介绍了使用嵌套的允许字段播放框架 2 (Java) 表单数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  考虑以下模型:

                  public class Contact {
                  
                    @Required
                    public String name;
                  
                    @Valid
                    public List<Information> informations;
                  
                    }
                  
                    public static class Information {
                  
                      public String securedField;
                  
                      @Required
                      public String email;
                  
                      @Valid
                      public List<Phone> phones;
                  
                      public static class Phone {
                  
                        @Required
                        @Pattern(value = "[0-9.+]+", message = "A valid phone number is required")
                        public String number;
                      }
                  
                    }
                  
                  }
                  

                  我不希望 InformationsecureField 受到 批量分配漏洞.所以我决定为 Contact Form 设置 allowedFields 数组.

                  I don't want Information securedField to be affected by mass assignment vulnerability. So i decided to set array of allowedFields for Contact Form.

                  据我所知,播放表单是基于Spring DataBinder的,所以可以处理集合字段吗?我不想这样写:

                  As i know, play forms are based on Spring DataBinder, so is it possible to handle collection fields? I don't want to write smth like:

                  • 姓名
                  • 信息[0].email
                  • 信息[0].phones*
                  • 信息[1].email
                  • 信息[1].phones*

                  以下不起作用:

                  • 姓名
                  • 信息.电子邮件
                  • 信息.电话*

                  在这种情况下我应该扩展现有的 Spring DataBinderForm 类并覆盖 bind 方法吗?

                  Should i extend existing Spring DataBinder and Form classes and override bind method in this case?

                  推荐答案

                  这是一个可以说是更简单的解决方案.如果 POST 数据包含任何 informations[%d].securedField 值,如何定义将触发验证失败的额外约束?

                  Here's an arguably simpler solution. How about defining an extra constraint that will trigger a validation failure if the POST data contains any informations[%d].securedField values?

                  import javax.validation.constraints.Null;
                  
                  public static class Information {
                  
                      @Null
                      public String securedField;
                  
                      ...
                  
                  }
                  

                  我认为这样您可以调用默认的 bindFromRequest 方法,而不是接受表单字段名称白名单的方法,并且仍然可以防止批量分配攻击.

                  I think that this way you can call the default bindFromRequest method instead of the one that accepts a whitelist of form field names, and still be protected against a mass assignment attack.

                  不可否认,这种方法的一个缺点是,如果发生协同的批量分配攻击,它最终会泄露您的内部字段的名称.但是,如果它们的名称相当平淡无奇,例如 securedField(无意冒犯!),我不确定攻击者如何利用这些信息.

                  One shortcoming with this approach admittedly is that it would ultimately leak the names of your internal fields in the event of a concerted mass assignment attack. However if they had fairly bland, meaningless names such as securedField (no offence intended!), I'm not sure how this information could be exploited by an attacker.

                  如果您想允许根据当前用户类型分配给字段,也许 bean 验证组会有所帮助:

                  If you want to allow assignment to the field based on the current user type, maybe bean validation groups could help:

                  import javax.validation.constraints.Null;
                  
                  public class Contact {
                  
                      public interface Administrator {}
                  
                      public interface User {}
                  
                      ...
                  
                      public class Information {
                  
                          @Null(groups = User.class)
                          public String securedField;
                  
                          ...
                  
                      }
                  
                  }
                  

                  控制器代码

                  ...
                  final Form<Contact> contactForm;
                  if (currentUser.isAdministrator()) {
                      contactForm = form(Contact.class, Administrator.class).bindFromRequest();
                  } else {
                      contactForm = form(Contact.class, User.class).bindFromRequest();
                  }
                  ...
                  

                  这篇关于使用嵌套的允许字段播放框架 2 (Java) 表单数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在同一个应用程序中同时使用 Spring JPA 和 Hibernate? 下一篇:Spring rest 资源变更路径

                  相关文章

                1. <tfoot id='7Zxli'></tfoot>

                  <small id='7Zxli'></small><noframes id='7Zxli'>

                        <bdo id='7Zxli'></bdo><ul id='7Zxli'></ul>
                    1. <legend id='7Zxli'><style id='7Zxli'><dir id='7Zxli'><q id='7Zxli'></q></dir></style></legend>

                    2. <i id='7Zxli'><tr id='7Zxli'><dt id='7Zxli'><q id='7Zxli'><span id='7Zxli'><b id='7Zxli'><form id='7Zxli'><ins id='7Zxli'></ins><ul id='7Zxli'></ul><sub id='7Zxli'></sub></form><legend id='7Zxli'></legend><bdo id='7Zxli'><pre id='7Zxli'><center id='7Zxli'></center></pre></bdo></b><th id='7Zxli'></th></span></q></dt></tr></i><div id='7Zxli'><tfoot id='7Zxli'></tfoot><dl id='7Zxli'><fieldset id='7Zxli'></fieldset></dl></div>