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());
}
}
これだけの数の編集者が登録されています。そのうちの 2 つは、それぞれのメソッドをオーバーライドして、数値形式とJoda-Timeのカスタム ニーズを満たすようにカスタマイズされていDateTimeEditor
ます。StrictNumberFormatEditor
Spring 3.2.0 を使用しているので、@ControllerAdvice
.
Spring はsetAllowedFields()
、悪意のあるユーザーがバインドされたオブジェクトに値を挿入できないように、メソッドで許可されたフィールドのセットをリストすることをお勧めします。
についてのドキュメントからDataBinder
検証およびバインド結果分析のサポートを含む、ターゲット オブジェクトへのプロパティ値の設定を可能にするバインダー。バインド プロセスは、許可されたフィールド、必須フィールド、カスタム エディターなどを指定してカスタマイズできます。
許可されたフィールドの配列を設定しないと、セキュリティに影響する可能性があることに注意してください。たとえば、HTTP フォーム POST データの場合、悪意のあるクライアントは、フォームに存在しないフィールドまたはプロパティの値を提供することにより、アプリケーションを破壊しようとする可能性があります。場合によっては、コマンド オブジェクトまたはそのネストされたオブジェクトに不正なデータが設定される可能性があります。このため、DataBinderでプロパティを指定することを強くお勧めします。
allowedFields
私には大きなアプリケーションがあり、明らかに何千ものフィールドがあります。それらすべてを で指定して一覧表示するのsetAllowedFields()
は面倒な作業です。さらに、どういうわけかそれらを覚える必要があります。
Web ページを変更して一部のフィールドを削除したり、必要に応じてフィールドを追加したりするには、setAllowedFields()
メソッドのパラメーター値を変更してそれらの変更を反映する必要があります。
これに代わるものはありますか?