使用 Spring Security,您可以为用户添加权限,例如:
可以编辑页面 可以查看页面 可以登录
等等?
如果是,这些是否存储在字节数组中?
最佳答案
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>
设置角色后,您可以在 Controller 或 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>
关于java - spring security,你能添加细粒度的安全级别吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1996954/