【发布时间】:2011-01-01 02:27:53
【问题描述】:
使用 Spring Security,您能否为以下用户添加权限:
可以编辑页面 可以查看页面 可以登录
等等?
如果是,这些是否存储在内部的字节数组中?
【问题讨论】:
标签: java spring-security spring
使用 Spring Security,您能否为以下用户添加权限:
可以编辑页面 可以查看页面 可以登录
等等?
如果是,这些是否存储在内部的字节数组中?
【问题讨论】:
标签: java spring-security spring
Spring Security 支持为每个用户分配 1 个或多个自定义角色。在我的站点上,我使用自定义表来保存这些角色,并设置身份验证提供程序 bean 以从该表中选择它们。查询中的参数是他们的用户名(这是我网站上的电子邮件地址)。我只将它设置为每个用户支持 1 个角色,但它可以很容易地分解成一个单独的表。
<authentication-provider>
<password-encoder hash="md5"/>
<jdbc-user-service data-source-ref="dataSource"
users-by-username-query="select email, password, '1' from user where email=?"
authorities-by-username-query="select email, role, '1' from user where email=?" />
</authentication-provider>
一旦你设置了角色,你可以在你的控制器或 JSP 文件中检查角色(使用http://www.springframework.org/security/tags taglib)。下面是一个这样的 JSP 示例:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %>
<h2>Edit Comment</h2>
<br/>
<security:authorize ifNotGranted="ROLE_ADMIN">
You are not authorized to edit comments.
</security:authorize>
<security:authorize ifAnyGranted="ROLE_ADMIN">
<!-- Display the whole admin edit comment page -->
</security:authorize>
【讨论】: