我遇到过这样的情况:我的应用程序有多个角色(管理员、版主、用户)。版主和用户可以编辑一些表格。所有权限都可以。但是当我以用户(角色用户)身份登录并更改 url 中的 id 时,我可以简单地获取和编辑另一个用户(角色用户)的表单。
如何拒绝访问并防止此类行为?
附言。 spring和spring security的版本是3.1.2
更新 - 添加了 spring 安全上下文
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/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 设置可以添加这样的安全规则:谁(具有某些角色的认证用户)可以访问某些 URL/方法调用。如果您希望能够使用这样的增强规则:谁(具有某种角色的认证用户)可以访问某些 URL/方法调用以及他可以使用哪些域对象那么您需要使用 ACL feature .
编辑。但是,如果您只需要一个这样的安全规则,那么设置 ACL 可能就有点矫枉过正了。您可以尝试通过自定义 Web 安全表达式来增强您的实际 SpringSecurity 设置:
<intercept-url pattern="/moderator/**" access="hasRole('moderatorViewPermission') and userIsAuthor()" />
您的 userIsAuthor() 方法将:
- 从 URL 中提取对象的 ID(我想像 /moderator/item/56)
- 检查当前用户是否是 id = 56 的项目的作者。
关于java - 拒绝在 spring security 中具有相同角色的多个用户访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14275687/