考虑这个代码块:
HttpServletRequest request = Faces.getRequest();
if (request.getRemoteUser() == null) {
try {
request.login(userName, password);
request.getSession().invalidate();
request.getSession(true);
} catch (Exception e) {
log.info("login() failed with exception", e);
Messages.addWarn(null, "Authentication Failed");
return;
}
} else {
log.debug("login() user is already authenticated");
}
假设您的身份验证成功。如果用户刷新页面并再次执行此方法,则不会到达 log.debug("login() user is alreadynauthenticated");
行。
我们希望以编程方式登录用户,然后将该用户与 session 关联起来(就像 spring 和 shiro 的工作方式一样)。你如何做到这一点?
我们的web.xml
:
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>SomeRealm</realm-name>
</login-config>
<security-constraint>
<web-resource-collection>
<web-resource-name>Everything</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
</security-constraint>
最佳答案
未到达该行,因为您正在销毁下一行中的 session ,并再次将您注销:
request.getSession().invalidate();
关于Java EE 安全/编程登录 : How does one make the authentication "stick" in the session?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31686564/