【问题标题】:JSF Authentication: cannot intercept error messagesJSF 身份验证:无法拦截错误消息
【发布时间】:2012-05-09 04:14:43
【问题描述】:

我开发了一个简单的登录表单,用于我的 JSF + PrimeFaces 页面:

<form action="j_security_check" method="post"> 
    <p:dialog modal="true" header="Login" widgetVar="loginDlg">
        <h:panelGrid columns="3" cellpadding="5">  
            <h:outputLabel for="j_username">Username:</h:outputLabel> 
            <h:inputText id="j_username" required="true" /> 
            <h:message for="j_username" /> 
            <h:outputLabel for="j_password">Password:</h:outputLabel> 
            <h:inputSecret id="j_password" required="true" /> 
            <h:message for="j_password" /> 
            <br /> 
            <h:commandButton value="Login" /> 
        </h:panelGrid>
    </p:dialog>                
</form> 

尝试使用空密码,但h:message 组件未捕获到缺少的密码(这是必需的)。我还切换到p:commandButton,认为问题可能出在按钮的 Ajax 行为中,但由于 PrimeFaces 抱怨 CommandButton 不在表单元素内,因此未呈现页面。容器抛出的异常是:

com.sun.enterprise.security.auth.login.common.LoginException: Login failed: Access denied on empty password for user pippo

总结一下,我有两个问题:

  1. 为什么在提交表单之前缺少密码不会产生消息?
  2. 如何捕获 LoginException 并在对话框中显示错误消息?

【问题讨论】:

  • 尝试把&lt;h:messages /&gt;放在那里,这应该会拦截整个页面上的所有错误消息

标签: jsf login primefaces j-security-check


【解决方案1】:

j_security_check 请求由 Web 容器处理,而不是由 JSF。这说明required="true" 不起作用。仅当您在与命令按钮关联的操作方法中使用 JSF &lt;h:form&gt;HttpServletRequest#login() 编程登录时才有效。

您能做的最好的事情是在web.xml 中配置一个&lt;form-error-page&gt;,指向与&lt;form-login-page&gt; 完全相同的URL。然后您可以检查请求是否已由j_security_check 本身转发,这意味着发生了登录错误。

<h:panelGroup rendered="#{requestScope['javax.servlet.forward.servlet_path'] == '/j_security_check'}">
    <h:outputText value="Unknown login, please try again" styleClass="error" />
</h:panelGroup>

用这个代替&lt;h:message&gt;

至于&lt;p:commandButton&gt;为什么抱怨没有表格,只是因为你没有使用&lt;h:form&gt;


无关与具体问题,&lt;form&gt;(或&lt;h:form&gt;,只要您决定切换到程序登录)最好放在&lt;p:dialog&gt; 的正文中,而不是放在外面。 &lt;p:dialog&gt; 可以通过 JS 重新定位到正文的末尾,这将导致它不再处于表单中。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-08
  • 2021-07-05
  • 2018-12-05
  • 2023-03-22
  • 1970-01-01
  • 2020-08-22
相关资源
最近更新 更多