【问题标题】:j_security_check with Primefacesj_security_check 与 Primefaces
【发布时间】:2012-11-17 04:51:12
【问题描述】:

如何使用 Primefaces 实现 j_security_check?一般在JSP中如果要使用JAAS登录,登录形式一般为:

<form action="j_security_check" method="POST">
   Username:<input type="text" name="j_username"><br>
   Password:<input type="password" name="j_password">
   <input type="submit" value="Login">
</form>

但是我们如何在 JSF 或 Primefaces 中实现它!

  • 将采取什么行动
  • 我们如何摆脱像formId:componentId这样的id或name
  • 此外,p:commandButton 默认情况下在 Primefaces 中是 ajaxified,那么它是如何实现的 以非 ajax 方式提交表单

我需要使用 Primefaces 实现 JAAS 表单身份验证,我在这里分享解决方案;它可能会派上用场。

【问题讨论】:

标签: java authentication jsf-2 primefaces jaas


【解决方案1】:

解决方案非常简单。

  • 您需要将h:form定义为prependId="false",这样它就不会生成组件的id或名称为formId:componentId
  • 需要将h:form中的action="j_security_check"定义为onsubmit="document.getElementById('login').action='j_security_check';"
  • p:commandButtonajax属性设置为false,这样表单就不会以ajax方式提交。

就是这样。这是登录表单的完整代码,可以用上述表单代替:

<h:form id="login" onsubmit="document.getElementById('login').action='j_security_check';" prependId="false">
    <h:panelGrid columns="2">
        <p:outputLabel for="j_username" value="Username" />
        <p:inputText id="j_username" name="j_username" />            
        <p:outputLabel for="j_password" value="Password" />
        <p:password id="j_password" name="j_password"/>
        <p:commandButton id="submit" value="Login" ajax="false"/>
    </h:panelGrid>
</h:form>

谢谢。

【讨论】:

  • 您可以只使用&lt;form&gt; 而不是&lt;h:form&gt;。不需要讨厌的 JS 代码(通过删除 document.getElementById('login'). 部分,它本身也可以更加简化)。
  • @BalusC 对迟到的评论感到抱歉,但看起来两者都不是正确的解决方案。我同意 javascript 是丑陋的 - 但如果你只使用 &lt;form&gt; 你不能在 p:commandButton 中使用 id="submit" 因此登录将无法正常工作....知道是否有更清洁的解决方案?
  • @OschtärEi:只需使用带有 PF 样式类的 &lt;input type="submit"&gt;
  • 如果您不需要 id 来代替 &lt;h:form&gt;,只需使用 &lt;h:form onsubmit="this.action='j_security_check';"&gt;
【解决方案2】:

有工作(使用 Primefaces 5)代码(从 p:inputText 和 p:password 中删除了名称属性,由 BalusC 部分建议删除):

<h:form id="login" onsubmit="action='j_security_check';" prependId="false">
    <h:panelGrid columns="2">
        <p:outputLabel for="j_username" value="Username" />
        <p:inputText id="j_username" />            
        <p:outputLabel for="j_password" value="Password" />
        <p:password id="j_password" />
        <p:commandButton id="submit" value="Login" ajax="false"/>
    </h:panelGrid>
</h:form> 

【讨论】:

    猜你喜欢
    • 2013-05-25
    • 2023-03-18
    • 1970-01-01
    • 2010-09-12
    • 2013-03-30
    • 2012-03-20
    • 2011-03-28
    • 2011-04-01
    • 1970-01-01
    相关资源
    最近更新 更多