pageContext.request.userPrincipal 是否存储在会话中?我也可以得到密码吗?

Is pageContext.request.userPrincipal stored in session? Can I get password too?

我正在学习 Spring 安全性,据我所知,通过使用 request.getUserPrincipal() 我们可以访问 name,我们可以使用 pageContext.request.userPrincipal.name ,

这是我的代码(一切正常):

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page session="true"%>
<html>
    <body>
        <h5>Title : ${title}</h5>
        <h5>Message : ${message}</h5>


        <c:if test="${pageContext.request.userPrincipal.name != null}">
            <h2>Hi User : ${pageContext.request.userPrincipal.name}></h2> 
            <br>
        </c:if>

    </body>
</html>

我的问题是:

1).这是 name on pageContext.request.userPrincipal.name 从会话中检索到的吗?因为表格顶部有 <%@page session="true"%>

2).是否也可以检索 密码 ?我的意思是 pageContext.request.userPrincipal.password,如果不是,我怎么能在表单中获取密码?

谢谢你,我非常感谢你帮助我理解这个框架,因为我没有足够的钱买一些 spring 书籍,我只是在使用互联网上的一些教程,所以你的帮助会非常刻薄大部头书。

Is this name on pageContext.request.userPrincipal.name retrieved from session?

不,它是从 request 中检索到的,而不是从 session 中检索到的。然而,在幕后,安全框架可能会将内部标识符存储在 HTTP 会话中。但这应该是你最不关心的。

顺便说一句,有一种更短的方法来检索主体名称。

${pageContext.request.remoteUser}

另见 a.o。 How to get login attributes from a servlet/jsp.


because there's <%@page session="true"%> on the top of the form

这具有不同的含义,并且已经是默认值。默认情况下,当 JSP 打开时,如果尚未创建,它将隐式创建 HTTP 会话。这在设计为无状态的页面中可能是不可取的。然后开发人员将使用 <%@page session="false"%> 关闭隐式会话创建并将其留给 servlet 代码。另见 a.o。


Is it possible to retrieve the password also? I mean something like pageContext.request.userPrincipal.password, if not how I could get the password in the form?

根据问题的评论,我了解到您需要它来验证登录。这是没有意义的。如果登录无效,则首先没有登录用户。