java - j_security_check 与 Primefaces

标签 java authentication jsf-2 primefaces jaas

如何使用 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 表单例份验证,我在这里分享解决方案;它可能会对某些人派上用场。

最佳答案

解决方案非常简单。

  • 您需要使用 prependId="false" 定义 h:form,这样它就不会生成组件的 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>

谢谢。

关于java - j_security_check 与 Primefaces,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13620287/

相关文章:

c# - 通过 C# 使用 Windows Hello 进行 Webauthn 身份验证

java - 在 OAuth 2.0 安全 Web 应用程序上使用 JMeter 脚本进行性能测试

jsf - 如何确保JSF taglib子组件的id不同?

java - 在 Activity 之间传输 int 无法正常工作

java - Spark DataFrame - 选择 n 随机行

尝试从 .jar 文件加载小程序时出现 java.lang.ClassNotFoundException

java - to.UpperCase 跳过它应该工作的过程

iOS/Cocoa - NSURLSession - 处理基本的 HTTPS 授权

jsf-2 - 我应该使用 <h :outputText> to print static text?

java - 如何从 PrimeFaces 上传文件的 byte[] 获取 KeyStore