簡體   English   中英

如果用戶登錄(如果使用JSTL <if>語句),如何檢查JSP,並顯示用戶名?

[英]How to check in JSP if user is logged (using JSTL <if> statement), and display username?

我知道在SO上有許多類似的問題 - 大多數都是指用servlet過濾器實現這個概念。

我使用基於表單的聲明式安全方法。 並且,當一些用戶在登錄表單上輸入他(或她)的憑證(用戶名和密碼)時,他被重定向到某個/安全的jsp頁面。 該頁面內容的一部分是:

Hello <%=request.getUserPrincipal().getName().toString()%>
    You are able to view this page because you are authorized user.

現在,如果用戶已登錄,我想顯示他/她的用戶名,無論是否有安全的jsp頁面。 像這樣的東西:

<c:if test="${statement that checks if some user is logged in}">
   User: ${username of the logged user here}
</c:if>

編輯我還想用適當的JSTL命令替換JSP scriptlet部分: <%=request.getUserPrincipal().getName().toString()%>

<c:if test="${not empty pageContext.request.userPrincipal}">
    User: <c:out value="${pageContext.request.userPrincipal.name}" />
</c:if>

使用c:out正確地轉義打印值並防止XSS攻擊。

我在兩種情況下都缺少pageContext對象, request本身不是JSP頁面中的隱式對象。

<c:if test="${not empty pageContext.request.userPrincipal}">
    User <c:out value="${pageContext.request.userPrincipal.name}" />
</c:if>

<%=request.getUserPrincipal().getName().toString()%> JSTL等價物是: <c:out value="${pageContext.request.userPrincipal.name}"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM