服务层中的 hibernate / spring 验证

发布于 2024-10-04 04:42:32 字数 214 浏览 3 评论 0原文

---添加(编辑)--

即使您在前端进行验证,您是否应该在服务层进行验证?

例如,struts 有验证功能,因此您可以对其进行多次验证。 (我不是指java脚本验证。)

-

即使您已经在前端进行了验证,您是否在服务层中使用hibernate验证器或spring验证?

(前端验证不必是 hibernate 或 spring 验证)

---added (edit) --

should you do validation at the service layer, even if you validate at the front-end?

eg struts has validations so you can validate it a lot. (I don't mean java script validations.)

--

are you using hibernate validator or spring validations in your service layer even though you have validations already on front-end?

(front-end validation don't have to be hibernate or spring one)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烦人精 2024-10-11 04:42:32

一致的数据是企业应用程序中最重要的事情之一!

所以是的:您需要检查两次:在前端和服务层。

在我们的网络应用程序中,我们在不同级别有不同的检查:
(我不是在谈论不一致的检查,我是在谈论检查的粒度和范围。)

  1. 输入字段的 Java 脚本检查(最大长度,非空...)
  2. 与 (1) 相同的检查以及服务器中一些更复杂的检查侧 Web 控制器
  3. 会破坏应用程序逻辑的重要(有时是复杂)约束在实体和服务中进行检查
  4. 数据库可以轻松检查的所有内容(不是 null、外键、唯一)都由数据库约束进行检查。

IHO 最好检查所有地方,但这是不可能的。因此,您需要一个一致的政策来检查什么。此策略很大程度上取决于您正在开发的应用程序的类型。

Consistent Data are one of the most important thing in Enterprice Applications!

So yes: you need to check it twice: in the frontend and in the Service layer.

In our web applications we have different checks at different levels:
(I am not talking about inconsistent checks, I am talking about granularity and scope of checks.)

  1. Java Script checks of inputfields (max length, not empty ...)
  2. The same checks like (1) and some more complex checks in the Server side web controller
  3. The important (and sometimes complex) constraints which would break the application logic are checked in the entities and services
  4. Everything a database can check easily (not null, forein key, unique) are checked by the database constraints.

IHO it would be the best to check all and everywhere, but this is not possible. So you need a consistent policy where you check what. And this policy strongly depends on the kind of application your are developing.

热情消退 2024-10-11 04:42:32

我们将它与 wicket 结合使用作为前端框架。您必须考虑到需要分两个步骤进行验证:

  1. 验证输入,例如:它是否是一个整数
  2. ,根据业务逻辑进行验证

我们使用业务验证在检票口内进行验证解决了问题。因此您不必编写两次代码。

we used it in combination with wicket as fronend framework. You have to consider that you need to validate in two steps:

  1. validating the input, for example: is it an Integer
  2. validating against the business logic

We solved the problem using the business validation to validate within wicket. So you don't have to write code twice.

﹎☆浅夏丿初晴 2024-10-11 04:42:32

您的问题不够具体,即它看起来不能解决任何一个问题(这更像是一个民意调查)。

要回答更具体的问题“即使在前端验证,是否应该在服务层进行验证?”,答案是绝对的。几乎所有前端验证,尤其是网络应用程序的验证,都可以被绕过。您的服务应该实施适当的验证以确保它们的一致性,并且不应依赖于用户空间中的验证(如 Javascript 检查),因为它们很容易被击败。

更新:当决定在代码前端(调度程序/控制器)和服务层之间进行验证时,我想说没有硬性规定。我可能会检查我们收到的输入在前端是否合理,并在服务层进行域级验证。

Your question is not specific enough i.e. it doesn't look to solve a single problem (it's more of a poll).

To answer the more specific question of "should you do validation at the service layer, even if you validate at the front-end?", the answer is absolutely. Almost all front-end validation, particularly for web-apps, can be bypassed. You services should implement appropriate validation to ensure that they are consistent and should not rely on validation in the user-space (like Javascript checks) as they are easily defeated.

UPDATE: when deciding to validate between the front-end of the code (the dispatcher/controller) and the service layers, I'd say there's no hard and fast rule. I'd probably check that the input we receive is sensible at the front-end and do the domain-level validation at the service layer.

甜扑 2024-10-11 04:42:32

我强烈建议使用 Hibernate Validator 并将所有验证保留到模型中。它对 Oops 封装有很大的促进作用,因为模型现在可以验证自身。

I would highly recommend using Hibernate Validator and keeping all your validations to the model. Its a good boost to Oops encapsulation as the model now validates itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文