-->

在Spring使用setAllowedFields()方法(Using the setAllowed

2019-07-20 09:11发布

我使用Spring 3.2.0。 我已经注册了一些自定义属性编辑器的一些基本需求如下。

import editors.DateTimeEditor;
import editors.StrictNumberFormatEditor;
import java.math.RoundingMode;
import java.net.URL;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import org.joda.time.DateTime;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.beans.propertyeditors.URLEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.context.request.WebRequest;

@ControllerAdvice
public final class GlobalDataBinder 
{
    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request)
    {
        binder.setIgnoreInvalidFields(true);
        binder.setIgnoreUnknownFields(true);
        //binder.setAllowedFields(someArray);
        NumberFormat numberFormat=DecimalFormat.getInstance();
        numberFormat.setGroupingUsed(false);
        numberFormat.setMaximumFractionDigits(2);
        numberFormat.setRoundingMode(RoundingMode.HALF_UP);

        binder.registerCustomEditor(DateTime.class, new DateTimeEditor("MM/dd/yyyy HH:mm:ss", true));
        binder.registerCustomEditor(Double.class, new StrictNumberFormatEditor(Double.class, numberFormat, true));
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
        binder.registerCustomEditor(URL.class, new URLEditor());
    } 
}

我至今注册这么多的编辑。 其中两个DateTimeEditorStrictNumberFormatEditor已经通过覆盖相应的方法,以满足数字格式和定制需求定制乔达时间 。

由于我使用Spring 3.2.0,我可以利用的@ControllerAdvice

春建议列出一组与允许的领域setAllowedFields()方法,使恶意用户不能注入值到绑定的对象。

从文档有关DataBinder

粘合剂,其允许设置属性值到目标物体上,包括支持验证和结合结果分析。 装订处理可以通过指定允许字段,所需的字段,自定义编辑器等来定制

请注意,在未按照规定允许的字段数组潜在的安全隐患。 在例如HTTP POST形式的数据的情况下,恶意客户端可以尝试通过对不窗体上存在字段或属性提供值颠覆的应用程序。 在某些情况下,这可能会导致非法数据被上命令对象或自己的嵌套对象设置。 出于这个原因,我们强烈建议指定allowedFields在DataBinder的属性


我有一个大的应用程序,显然有成千上万的领域。 指定并列出他们都与setAllowedFields()是一个单调乏味的工作。 此外,不知何故,我需要记住他们。

更改网页删除某些字段或添加其他领域的需求再次出现需要修改的参数值setAllowedFields()方法来反映这些变化。

是否有任何替代品吗?

Answer 1:

而不是使用的setAllowedFields()至白名单,你可以使用setDisallowedFields()到黑名单。 例如,从PetClinic示例应用:

@InitBinder
public void setAllowedFields(WebDataBinder dataBinder) {
    dataBinder.setDisallowedFields("id");
}

单纯从安全角度来看白名单优先黑色上市,但它也许有助于减轻负担一些。



Answer 2:

setAllowedFields()是非常方便的在使用实体直接在网层对象。 可选地,可以使用专用的数据传输对象(DTO),从该实体对象在服务层构成。 不仅可以在工厂中重复使用,而且使用的网络环境之外,例如用于异步消息。 此外,DTO继承不必遵循实体继承,所以你可以自由根据的用例需求来设计DTO层次。



Answer 3:

从http://static.springsource.org/spring-webflow/docs/2.0.x/reference/htmlsingle/spring-webflow-reference.html#view-model

4.9。 指定绑定明确

使用粘合剂元件配置精确的集合由视图可用模型绑定。 这是在Spring MVC环境限制设定按次“允许的域”的特别有用。

<view-state id="enterBookingDetails" model="booking">
    <binder>
        <binding property="creditCard" />
        <binding property="creditCardName" />
        <binding property="creditCardExpiryMonth" />
        <binding property="creditCardExpiryYear" />
    </binder>
    <transition on="proceed" to="reviewBooking" />
    <transition on="cancel" to="cancel" bind="false" />
</view-state>

如果未指定粘结剂元素,该模型的所有公共属性有资格获得由视图结合。 与指定的粘合剂元件,仅明确配置的绑定是允许的。

每个结合也可以应用转换器格式化在自定义方式用于显示的模型的属性值。 如果没有指定转换器,将用于模型属性的类型的默认转换器。

<view-state id="enterBookingDetails" model="booking">
    <binder>
        <binding property="checkinDate" converter="shortDate" />
        <binding property="checkoutDate" converter="shortDate" />    
        <binding property="creditCard" />
        <binding property="creditCardName" />
        <binding property="creditCardExpiryMonth" />
        <binding property="creditCardExpiryYear" />
    </binder>
    <transition on="proceed" to="reviewBooking" />
    <transition on="cancel" to="cancel" bind="false" />
</view-state>

在上面的例子中,转换器shortDate绑定到checkinDate和checkoutDate性质。 自定义转换器可以与应用程序的ConversionService注册。

每个结合也可应用所需的检查,如果用户提供的值是在回发形式零,这将产生一个验证错误:

<view-state id="enterBookingDetails" model="booking">
    <binder>
        <binding property="checkinDate" converter="shortDate" required="true" />
        <binding property="checkoutDate" converter="shortDate" required="true" />
        <binding property="creditCard" required="true" />
        <binding property="creditCardName" required="true" />
        <binding property="creditCardExpiryMonth" required="true" />
        <binding property="creditCardExpiryYear" required="true" />
    </binder>
    <transition on="proceed" to="reviewBooking">
    <transition on="cancel" to="bookingCancelled" bind="false" />
</view-state>

在上面的例子中,所有的绑定是必需的。 如果一个或多个空白输入值被约束,将生成的验证错误和与这些错误的视图将重新呈现。



文章来源: Using the setAllowedFields() method in Spring