使用Primefaces进行j_security_check

Tap*_*ose 4 java authentication jaas primefaces jsf-2

如何实现j_security_checkPrimefaces?通常在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>
Run Code Online (Sandbox Code Playgroud)

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

  • 这将是什么行动
  • 我们如何摆脱id或名称 formId:componentId
  • 此外p:commandButton,默认情况下,它在Primefaces中被ajaxified,因此它如何以非ajax方式提交表单

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

Tap*_*ose 18

解决方案非常简单.

  • 您需要定义h:formwith prependId="false",以便它不会生成组件的id或名称formId:componentId.
  • 你需要action="j_security_check"h:formas中定义onsubmit="document.getElementById('login').action='j_security_check';"
  • 设置to 的ajax属性,以便不以ajax方式提交表单.p:commandButtonfalse

而已.以下是登录表单的完整代码,可由上述表格替换:

<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>
Run Code Online (Sandbox Code Playgroud)

谢谢.

  • 你可以使用`<form>`而不是`<h:form>`.不需要令人讨厌的JS代码(通过删除`document.getElementById('login')可以使其更加简化.)part. (6认同)