java - spring security,你能添加细粒度的安全级别吗?

标签 java spring-security spring

使用 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/

相关文章:

java - 将持久化实体保留在事务之外

java - 如何使用SWIG包装std::function对象?

spring-security - 连接池+模拟查询

mysql - Spring Security - Bcrypt 与 CAS、MySQL、SearchModeSearchDatabaseAuthenticationHandler 和 BasicDataSource

java - ActiveProfile 在 Junit5 测试中不起作用

java - Java中有没有动态实现接口(interface)的方法?

java - 如何获取上传视频文件的缩略图?

java - Spring Security 中不同角色的多个主页

java - 收集所有发出的 SQL 的最简单方法

java - 我如何不使用 Maven 引入第三方依赖项,而是如何将该依赖项的源代码直接实现到我的项目中?