我在这里找到了可以解决我的问题的代码,但我不知道它是如何工作的。
代码:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<title>Sample Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<c:choose>
<c:when test="${pageContext.request.isUserInRole('admin')}">
<p>Content for admin.<p>
</c:when>
<c:when test=${pageContext.request.isUserInRole('someRole')}">
<p>Some content here</p>
<c:when>
<c:otherwise>
<p>Another Content</p>
</c:otherwise>
</c:choose>
</body>
</html>
我不知道它是如何工作的pageContext.request.isUserInRole('admin')
。
我的所有用户和 user_roles 都在数据库中。
那么 pageContext.request.isUserInRole('admin')
从哪里获取数据呢?
您能给我举个小例子吗?它是如何工作的,以及我应该如何将有关用户角色的信息从 servlet 传递到 pageContext.request.isUserInRole('admin')
。
最佳答案
你所说的是Role Based Authentication 。您不需要将角色从 servlet 传递到 jsp,而是需要在 xml 文件中对其进行配置
现在检查 request#isUserInRole
是什么按照文档执行,
Returns a boolean indicating whether the authenticated user is included in the specified logical "role". Roles and role membership can be defined using deployment descriptors. If the user has not been authenticated, the method returns false.
完整tutorial这里介绍jsp的安全机制。
另请参阅:
关于java - jsp/servlet,为管理员、用户和未登录用户显示不同的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28294301/