我知道在 SO 上有很多类似的问题 - 大多数问题都提到用 servlet 过滤器实现这个概念。
我正在使用基于表单的声明式安全方法。而且,当某些用户在登录表单上输入他(或她)的凭据(用户名和密码)时,他将被重定向到某个/安全的 jsp 页面。该页面的部分内容是:
Hello <%=request.getUserPrincipal().getName().toString()%>
You are able to view this page because you are authorized user.
现在,如果用户已登录,我想显示他/她的用户名,无论其是否 protected jsp 页面。像这样:
<c:if test="${statement that checks if some user is logged in}">
User: ${username of the logged user here}
</c:if>
编辑
我还想替换 JSP scriptlet 部分:<%=request.getUserPrincipal().getName().toString()%>
使用适当的 JSTL 命令。
最佳答案
<c:if test="${not empty pageContext.request.userPrincipal}">
User: <c:out value="${pageContext.request.userPrincipal.name}" />
</c:if>
使用 c:out
正确转义打印值并防止 XSS 攻击。
关于java - 如果用户已登录(使用 JSTL <if> 语句),如何检查 JSP 并显示用户名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21329256/