我正在学习 Spring 安全性,据我了解,使用 request.getUserPrincipal()
我们可以访问名称,我们可以通过 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).这是 pageContext.request.userPrincipal.name
上的名字吗?从 session 中检索?因为有<%@page session="true"%>
在表单顶部
2).是否也可以找回密码?我的意思是类似 pageContext.request.userPrincipal.password
,如果不是我如何获取表单中的密码?
谢谢你,我真的很感谢你帮助我理解这个框架,因为我没有足够的钱去买一些 springs 书籍,我只是使用互联网上的一些教程,所以你的帮助对我来说非常重要。
最佳答案
Is this name on pageContext.request.userPrincipal.name retrieved from session?
不,它是从 request
检索的,不是来自session
。然而,在幕后,安全框架可能会将内部标识符存储在 HTTP session 中。但这应该是您最不关心的。
顺便说一下,有一种更短的方法来检索主体名称。
${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 session 。这对于设计为无状态的页面来说可能是不可取的。然后开发人员将使用<%@page session="false"%>
关闭隐式 session 创建并将其留给 servlet 代码。另请参见 a.o. Can I turn off the HttpSession in web.xml?
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?
根据问题的评论,我认为您需要它来验证登录。这毫无意义。如果登录无效,则首先不会有登录用户。
关于jsp - pageContext.request.userPrincipal 是否存储在 session 中?我也能得到密码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35244160/