简体   繁体   English

在MVC + DDD + Repository Pattern项目中应用安全性的内容是什么?

[英]What to apply security to in MVC + DDD + Repository Pattern project?

I have the broad requirement for a flexible, reasonably granular security system, allowing us to customize what a given role or user is allowed to do within the system. 我对灵活,合理的粒度安全系统有广泛的要求,允许我们自定义允许给定角色或用户在系统内执行的操作。

Facing this requirement, I must choose what objects, classes, or items within the architecture the security should use as its building block - eg. 面对这个要求,我必须选择安全性应该用作构建块的体系结构中的对象,类或项目 - 例如。 if a role us granted access to X, then what is X? 如果一个角色我们授予了X的访问权限,那么什么是X? An entity, a controller action, an item in a custom list of objects etc. 实体,控制器动作,自定义对象列表中的项目等。

Options I am considering: 我正在考虑的选项:

1) Grant by CRUD action on Entities (eg. a user could be granted Create/Read/Update access to the Account entity, and Read access to the Invoice entity, etc) 1)通过CRUD对实体的操作授予(例如,可以授予用户对帐户实体的创建/读取/更新访问权限,以及对发票实体的读取访问权限等)

2) Grant by CRUD action on Entities , with RU actions to individual Entity Properties (eg. access to update specific fields) - could be simplified with "property groups" identified by attributes on the entities 2)CRUD对实体的授权,对单个实体属性的RU操作(例如,访问更新特定字段) - 可以通过实体上的属性标识的“属性组”进行简化

3) Grant by Repository & Repository Function (eg. permitted to call to AccountsRepository.Get(...) or AccountsRepository.GetList(...) etc) 3)通过存储库和存储库功能授予(例如,允许调用AccountsRepository.Get(...)或AccountsRepository.GetList(...)等)

4) Grant by MVC Controller + Action (eg. permitted to access /Accounts/Index or /Accounts/Update/X etc) 4)MVC Controller + Action授予(例如允许访问/ Accounts / Index或/ Accounts / Update / X等)

5) Grant by a custom list of "Security Objects" which can be tied to arbitrary things within the architecture 5)通过自定义的“安全对象”列表授予,该列表可以绑定到体系结构中的任意内容

Option (5) gives the most flexibility but least generic implementation. 选项(5)提供最灵活但最不通用的实现。 Option (4) is attractive as the security items will closely reflect the user interface, but means that the Domain is not securing access and security would not be applied in non-web interfaces. 选项(4)很有吸引力,因为安全项将紧密反映用户界面,但意味着域不保护访问权限,并且安全性不会应用于非Web界面。

What is your opinion & experience designing a security pattern in MVC + DDD + Repository pattern? 您对MVC + DDD + Repository模式中的安全模式设计有什么看法和经验?

Designing authorization is the same regardless of DDD, REpository, MVC, CQRS ,[insert whatever trend of the day]. 无论DDD,REpository,MVC,CQRS,[插入当天的任何趋势],设计授权都是相同的。

You want the security check to be done when an action (not related to controller action) happens. 您希望在发生操作(与控制器操作无关)时完成安全检查。 You check if the user has the right to do a certain action within a specific context. 您检查用户是否有权在特定上下文中执行某项操作。 In your case it's really is a controller action and the easiest way is via an ActionFilter (which i think can be reused with the WebApi as well). 在你的情况下,它实际上是一个控制器动作,最简单的方法是通过ActionFilter(我认为也可以与WebApi一起重用)。

The Domain model business concepts, behavior and use cases, the repository deals with persistence, let the security be its own layer which will care about users, rights and contexts. 域模型业务概念,行为和用例,存储库处理持久性,让安全性成为自己的层,关注用户,权限和上下文。

Even in the use case mentioned by Hippoom, it's still a security layer concerns which will have its very own security rules, similary to a validation layer which validates input data according to some predefined rules. 即使在Hippoom提到的用例中,它仍然是一个安全层问题,它将拥有自己的安全规则,类似于根据某些预定义规则验证输入数据的验证层。

The most common security mechanism only requires role and resource. 最常见的安全机制只需要角色和资源。 In this case, Option (4) seems to be the most common solution I've seen, therefore there should be a few mature secutiry frameworks on your platform. 在这种情况下,选项(4)似乎是我见过的最常见的解决方案,因此在您的平台上应该有一些成熟的安全框架。

The security things are inevitablly mixed into the domain model if the security granularity is on the domain objects. 如果安全粒度在域对象上,则安全性不可避免地混入域模型中。 I think it is usually unnecessary. 我认为这通常是不必要的。

On the other hand, some security requirment need business context, for example, an operator cannot manipulate a trade more than $1000 while his supervisor can. 另一方面,一些安全要求需要业务环境,例如,运营商无法操纵超过1000美元的交易,而他的主管可以。 Honeslty, I have no expierence on how to implement this, but I personally prefer building the security implementation in another bounded context from the core domain. Honeslty,我对如何实现这一点没有任何意义,但我个人更喜欢在核心域的另一个有界上下文中构建安全实现。

I think this is one of the kind of questions a security framework designer ask himself when is thinking about what facilities he can offer in the filed of the Authorization problem. 我认为这是安全框架设计师在考虑他可以在授权问题领域提供哪些设施时问自己的问题之一。

I'd suggest you to look at the design or implementation of actual security frameworks available for your platform. 我建议您查看适用于您的平台的实际安全框架的设计或实现。

I know only the Java-based Spring Security and Apache Shiro. 我只知道基于Java的Spring Security和Apache Shiro。

They usually come with facilities for every authorization requirements and, as for your question, they can offer you a solution at all levels of granularity: 它们通常带有满足每个授权要求的设施,对于您的问题,它们可以为您提供各种粒度级别的解决方案:

  • Resource level (when you are not interested on which object instance apply the security check); 资源级别(当您对哪个对象实例应用安全检查不感兴趣时​​);
  • Instance level (you control access to a specific instance of an object); 实例级别(您控制对特定对象实例的访问);
  • Attribute level (you control access to a specific field of a specific instance of an object). 属性级别(您可以控制对特定对象实例的特定字段的访问)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM