JSF 2.3 基于表单的登录和 ViewExpiredException

JSF 2.3 Form Based Login and ViewExpiredException

我有一个 Web 应用程序当前部署在 Wildfly 22 上,使用 JSF 2.3 和 OpenJDK 11。 我目前正在将登录页面从 j_security_check 迁移到编程登录,按照 BalusC 示例 post:

Performing user authentication in Java EE / JSF using j_security_check

我没有 post登录代码,因为它和 BalusC 完全一样 post。

登录过程运行良好,但登录页面上的会话超时到期时除外。换句话说,当用户请求受保护的资源时,将显示登录页面。如果会话在提交登录表单之前过期,则会抛出 ViewExpiredException 并向用户显示错误。

我知道这是预期的行为,但对于最终用户而言,这不是理想的情况。 我设法使用 OmniFaces 的 ViewExpiredExceptionHandler 将这种情况降至最低。 这样,当抛出 ViewExpiredException 时,OmniFaces 处理程序将捕获它并使用查询字符串重定向到当前 URL。 换句话说,用户在会话过期后尝试登录,登录页面再次呈现给用户。

我设法使用 #{flash['org.omnifaces.view_expired'] eq true} 向用户显示一条友好的消息,解释发生了超时。

有什么方法可以解决这种情况,即使会话过期也能成功登录,这样用户就不必输入两次凭据了吗?

感谢您的帮助!

Is there any way to workaround this situation, and performing a successful login even when the session expires, so that the user doesn't have to enter his credentials twice?

是,通过使用无状态 JSF,将 <f:view>transient 属性设置为 true

<f:view transient="true">
    <h:form>
        ...
        <h:commandButton ... action="#{requestScopedBean.login}" />
    </h:form>
</f:view>

请注意,支持 bean 必须是 @RequestScoped,而不是 @ViewScoped 或更宽的。

另请参阅:

  • What is the usefulness of statelessness in JSF?