在Spring安全性中拒绝具有相同角色的多个用户的访问权限

编程入门 行业动态 更新时间:2024-10-24 12:30:57
本文介绍了在Spring安全性中拒绝具有相同角色的多个用户的访问权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我遇到过这种情况:我的应用程序有多个角色(管理员,主持人,用户)。主持人和用户可以编辑一些表格。所有的权利都可以。但是当我作为用户(角色用户)登录并更改网址中的ID时,我可以简单地获取并编辑另一个用户(角色用户)的表单。

I've run into a such situation: my application has several roles(administrator, moderator, user). Moderator and User can edit some forms. All permisions are ok. But when I'm loggen in as a user(role User) and change an id in the url, I can simply get and edit form of another user(role User).

如何拒绝访问并阻止此类操作?

How to deny access and prevent such actions?

ps。春季和春季安全版本为3.1.2

ps. version of spring and spring security is 3.1.2

更新 - 添加春季安全背景

update - added spring security context

<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="www.springframework/schema/security" xmlns:beans="www.springframework/schema/beans" xmlns:xsi="www.w3/2001/XMLSchema-instance" xsi:schemaLocation="www.springframework/schema/beans www.springframework/schema/beans/spring-beans-3.1.xsd www.springframework/schema/security www.springframework/schema/security/spring-security-3.1.xsd"> <http use-expressions="true" auto-config="false" entry-point-ref="authenticationEntryPoint" access-denied-page="/403.jsp"> <form-login login-page="/login.html" authentication-failure-url="/login.html?error=true" login-processing-url="/j_spring_security_check" authentication-success-handler-ref="successHandler" /> <logout logout-url="/logout" logout-success-url="/login.html" /> <intercept-url pattern="/admin/**" access="hasRole('adminViewPermission')" /> <intercept-url pattern="/moderator/**" access="hasRole('moderatorViewPermission')" /> <intercept-url pattern="**/form-management" access="hasRole('formManagementPermission')" /> </http> <authentication-manager alias="authenticationManager"> <authentication-provider user-service-ref="userDetailsService" /> </authentication-manager> <beans:bean id="authenticationEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint"> <beans:property name="loginFormUrl" value="/login.html" /> <beans:property name="forceHttps" value="false" /> </beans:bean> <beans:bean id="successHandler" class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler"> <beans:property name="defaultTargetUrl" value="/authenticate" /> <beans:property name="alwaysUseDefaultTargetUrl" value="true"/> </beans:bean> <beans:bean id="userDetailsService" class="com.jack.MyYserDetailsService"> </beans:bean> </beans:beans>

推荐答案

看起来你想要考虑实际的域对象为您的安全规则。具有用户和角色的正常SpringSecurity设置可以添加如下安全规则:who(具有某个角色的athenticated用户)可以访问某些URL /方法调用。如果您希望能够使用这样的增强规则:who(具有某个角色的athenticated用户)可以访问某些URL /方法调用以及他可以使用的域对象那么您需要使用 ACL功能。

It looks like you want take into account actual domain object for your security rule. Normal SpringSecurity setup with users and roles can add security rules like this: who (athenticated user with some role) may access to some URL / method invocation. If you want to be able use enhanced rules like this: who (athenticated user with some role) may access to some URL / method invocation and what domain objects he can use then you need to use ACL feature.

编辑。但是如果你需要像这样的一个安全规则那么设置ACL可能是一种过度杀伤力。您可以尝试通过自定义Web安全表达式增强实际的SpringSecurity设置:

EDIT. But if you need just one security rule like this then set up ACL may be an overkill. You can try enhance your actual SpringSecurity setup by custom web security expression:

<intercept-url pattern="/moderator/**" access="hasRole('moderatorViewPermission') and userIsAuthor()" />

userIsAuthor()方法的位置:

  • 从URL中提取对象的ID(我想类似 / moderator / item / 56 )
  • 检查当前用户是否是项目id = 56的作者。

更多推荐

在Spring安全性中拒绝具有相同角色的多个用户的访问权限

本文发布于:2023-05-25 10:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/226446.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   安全性   访问权限   角色   用户

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!