Spring Security:按操作自定义权限
我需要使用 spring security 3 启动遗留应用程序。
该应用程序已经具有其安全数据模型:
非常到目前为止很简单。我可以编写自定义的 usersByUsernameQuery
和 authoritiesByUsernameQuery
。
问题是,还有另一个表指示角色可以执行的操作(即@Service
层方法):
因此,管理员可以启用/禁用角色通过 Web 界面访问操作,而无需重新部署应用程序。
例如,我仍然可以使用 @Secure('ROLE_ADMIN')
注释业务方法,但我的自定义 UserDetailsService
必须至少知道受保护的方法名称,因此我可以执行正确的查询。
所以,问题是:有没有办法让我的自定义 UserDetailsService
可以拦截受保护的方法的名称?
I need to make a legacy application start using spring security 3.
This app already has its security data model with:
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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您的访问决策是基于“操作角色”,而不是用户角色,因此最好直接在 Spring Security 约束中使用“操作角色”。这本质上是一种 RBAC 方法,其中用户角色和允许他们执行的操作之间存在映射。
您可以在
AuthenticationProvider
而不是UserDetailsService
中解决该问题,方法是在其中添加一个映射层来转换用户角色(由UserDetailsService
提供) >) 进入用户在应用程序中拥有的权限。这些将构成在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 theUserDetailsService
, by adding a mapping layer in there which translates the user roles (supplied by theUserDetailsService
) into the rights that the user has within the application. These would make up the collection of authorities that are returned in theAuthentication
object created by theAuthenticationProvider
.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.