使用Spring安全性,您是否可以为用户添加权限,如:
canEditPage canViewPage canLogin
等?
如果是,那么这些是否存储在字节数组中?
答案 0 :(得分:1)
Spring Security支持为每个用户分配一个或多个自定义角色。在我的网站上,我使用自定义表来保存这些角色,并设置身份验证提供程序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>