How is j_username value being managed once login is performed?
我有一个Web应用程序,该应用程序具有FORM类型的身份验证机制,因此当用户请求受保护的资源时,
在以下请求期间,
谁以及如何将该值与当前用户实际耦合?
但这只是一个猜测,任何人都可以确认/反驳吗?
My guess is that the application server is the one that is keeping track of the pairs [userid,sessionid] so that when request.getUserPrincipal().getName() is invoked it can send back the userid associated with the current session.
在servlet规范中没有指定。但是,基于
实际的实现取决于所使用的servlet容器。对于Tomcat,它存储在其
发生的地方是
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 426 if (cache) { 427 Principal principal = request.getUserPrincipal(); 428 if (principal == null) { 429 Session session = request.getSessionInternal(false); 430 if (session != null) { 431 principal = session.getPrincipal(); 432 if (principal != null) { 433 if (log.isDebugEnabled()) { 434 log.debug("We have cached auth type" + 435 session.getAuthType() + 436 " for principal" + 437 session.getPrincipal()); 438 } 439 request.setAuthType(session.getAuthType()); 440 request.setUserPrincipal(principal); 441 } 442 } 443 } 444 } |
在第431行,您可以看到它是从HTTP会话中提取的(至少是Tomcat的内部外观,无法通过公共API访问),在440行,您可以看到它是在Tomcat的HTTP请求的内部外观上设置的。依次可通过
如果您好奇如何以及何时调用
所有其他servlet容器实现都具有类似的方法。
也可以看看:
- 具有声明性安全性的JSP登录-实际身份验证如何进行?
- 在哪里可以找到" j_security_check"?
Web服务器为每个登录用户存储UserPrincipal对象。主体对象链接到sessionId。 sessionId是存储在客户端的cookie的一部分。因此,每次用户访问资源时,都会对sessionId进行匹配,并获取各自的UserPrincipal对象。该实现取决于Web服务器。如果是tomcat,则在Realm类中创建UserPrincipal对象。
这里是解释。