gpt4 book ai didi

spring - 在 Spring 中使用 setAllowedFields() 方法

转载 作者:行者123 更新时间:2023-12-02 05:42:14 32 4
gpt4 key购买 nike

我正在使用 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 已通过重写各自的方法进行自定义,以满足数字格式和 Joda-Time 的自定义需求。 .

由于我使用的是 Spring 3.2.0,因此我可以利用 @ControllerAdvice .

Spring 建议使用 setAllowedFields() 列出一组允许的字段。方法,以便恶意用户无法将值注入(inject)到绑定(bind)对象中。

来自docs关于DataBinder

Binder that allows for setting property values onto a target object, including support for validation and binding result analysis. The binding process can be customized through specifying allowed fields, required fields, custom editors, etc.

Note that there are potential security implications in failing to set an array of allowed fields. In the case of HTTP form POST data for example, malicious clients can attempt to subvert an application by supplying values for fields or properties that do not exist on the form. In some cases this could lead to illegal data being set on command objects or their nested objects. For this reason, it is highly recommended to specify the allowedFields property on the DataBinder.

<小时/>

我有一个很大的应用程序,显然有数千个字段。使用 setAllowedFields() 指定并列出所有这些是一项乏味的工作。此外,我需要以某种方式记住它们。

更改网页以删除某些字段或在需要时添加其他字段需要修改 setAllowedFields() 的参数值反射(reflect)这些变化的方法。

有什么替代方案吗?

最佳答案

您可以使用 setDisallowedFields() 列入黑名单,而不是使用 setAllowedFields() 列入白名单。例如,来自 petclinic 示例应用程序:

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

从纯粹的安全角度来看,白名单优于黑名单,但它可能有助于减轻一些负担。

关于spring - 在 Spring 中使用 setAllowedFields() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15031049/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com