Spring Security:按操作自定义权限

发布于 2024-10-23 19:21:28 字数 647 浏览 2 评论 0原文

我需要使用 spring security 3 启动遗留应用程序。

该应用程序已经具有其安全数据模型:

simple model

非常到目前为止很简单。我可以编写自定义的 usersByUsernameQueryauthoritiesByUsernameQuery

问题是,还有另一个表指示角色可以执行的操作(即@Service层方法):

real model

因此,管理员可以启用/禁用角色通过 Web 界面访问操作,而无需重新部署应用程序。

例如,我仍然可以使用 @Secure('ROLE_ADMIN') 注释业务方法,但我的自定义 UserDetailsS​​ervice 必须至少知道受保护的方法名称,因此我可以执行正确的查询。

所以,问题是:有没有办法让我的自定义 UserDetailsS​​ervice 可以拦截受保护的方法的名称?

I need to make a legacy application start using spring security 3.

This app already has its security data model with:

simple model

Very simple by far. I can write my custom usersByUsernameQuery and authoritiesByUsernameQuery.

The thing is that there is another table indicating the operation (i.e. @Service layer method) that a Role can execute:

real model

So the administrator can enable/disable a role from accessing an operation through a web interface, without redeploying the app.

I still can annotate the business methods with @Secure('ROLE_ADMIN') for example, but my custom UserDetailsService must know at least the method name that is being secured, so I can perform the right query.

So, the question is: is there a way that my custom UserDetailsService can intercept the method's name that is being secured?

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

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

发布评论

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

评论(1

眼藏柔 2024-10-30 19:21:28

听起来您的访问决策是基于“操作角色”,而不是用户角色,因此最好直接在 Spring Security 约束中使用“操作角色”。这本质上是一种 RBAC 方法,其中用户角色和允许他们执行的操作之间存在映射。

您可以在 AuthenticationProvider 而不是 UserDetailsS​​ervice 中解决该问题,方法是在其中添加一个映射层来转换用户角色(由 UserDetailsS​​ervice 提供) >) 进入用户在应用程序中拥有的权限。这些将构成在 AuthenticationProvider 创建的 Authentication 对象中返回的权限集合。

映射层将直接使用您的管理界面提供的数据。

您可能想看看 Mike Weisner 的此演示文稿,其中涵盖了类似的材料等等。

Spring Security 3.1 还不会包含额外的 GrantedAuthorityMapper 策略以便更轻松地插入此类映射。

It sounds like your access-decision is based on the "operation role", rather than the user roles, so it might be better to use the "operational role" directly in the Spring Security constraints. That is essentially an RBAC approach, where there is a mapping between the user roles and the operations they are allowed to perform.

You would address the issue in the AuthenticationProvider rather than the UserDetailsService, by adding a mapping layer in there which translates the user roles (supplied by the UserDetailsService) into the rights that the user has within the application. These would make up the collection of authorities that are returned in the Authentication object created by the AuthenticationProvider.

The mapping layer would directly use the data which your administration interface provides.

You might want to take a look at this presentation, by Mike Weisner, which covers similar material, amongst other things.

Not also that Spring Security 3.1 will include an additional GrantedAuthorityMapper strategy to make it easier to plug in a mapping of this kind.

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