Java EE 安全/编程登录 : How does one make the authentication "stick" in the session?

标签 java security tomcat jakarta-ee apache-tomee

考虑这个代码块:

        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/

相关文章:

java - java中的百分比函数

java - Vaadin 表 - AddNestedContainerProperty

tomcat - 即使密码错误,信任库也能正常工作,为什么?

spring - 运行 "apereo/cas"docker image 打印 "Failed to start connector"错误

java - 我无法从文件中获取 Uri

java - java中的网络通信加密

java - 带有 SHA-2 签名证书的 SunCertPathBuilderException

java - 在数据库中安全存储 1 个密码

tomcat - tomcat webdav 是否支持carddav?

java - 生成的 JSON 格式错误