如何使用 j_security_check jsf

ang*_*gus 3 j-security-check primefaces jsf-2

我想使用 j_security_check 身份验证来验证用户凭据。

基本上我想要实现的是,当用户按下提交时,如果他使用错误的凭据,则会显示一条消息 (p:growl),如果成功,则对话框将关闭。

网上有很多例子,但不幸的是我仍然无法理解如何完成这个难题:(

在我的项目中,我使用的是 primefaces 4.0 和 weblogic 10.3.2.0 (JAVA EE 5)。

一些代码示例:

<p:dialog id="dialog" widgetVar="dlg" resizable="false"> 
    <h:form id="fLogin" prependId="false"
            onsubmit="document.getElementById('fLogin').action = 'j_security_check';">        
        <h:panelGrid columns="2" cellpadding="5">  
            <h:outputLabel for="j_username" value="Username:" />  
            <p:inputText value="#{expBean.username}"   
                         id="j_username" label="username"/>  
            <h:outputLabel for="j_password" value="Password:" />  
            <h:inputSecret value="#{expBean.password}"   
                           id="j_password" label="password"/>  
            <p:commandButton id="submitButton" 
                             value="Submit"
                             actionListener="#{expBean.run}" /> 
        </h:panelGrid> 
    </h:form>
</p:dialog>
Run Code Online (Sandbox Code Playgroud)

网页.xml

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
    <url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<security-constraint>
    <web-resource-collection>
        <web-resource-name>main</web-resource-name>
        <description/>
        <url-pattern>main.jsf</url-pattern>
        <http-method>POST</http-method>
    </web-resource-collection>
</security-constraint>
<login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>my-realm</realm-name>
</login-config>
<security-role>
    <description/>
    <role-name>MyRole</role-name>
</security-role>
Run Code Online (Sandbox Code Playgroud)

exeBean:

   public void run() {


      FacesContext facesContext = FacesContext.getCurrentInstance();

   }
Run Code Online (Sandbox Code Playgroud)

任何指导方针和有用的例子将不胜感激

谢谢

Bal*_*usC 5

您正在通过 PrimeFaces ajax 提交表单。这就是它失败的原因。该j_security_check处理器不明白传入JSF / PrimeFaces味Ajax请求,不能由返回所需的XML响应适当地处理它们。它必须是常规(同步)提交。

关闭 ajax 的东西:

<p:commandButton ... ajax="false" />
Run Code Online (Sandbox Code Playgroud)

顺便说一句,您的表单声明很笨拙。只需使用<form>代替<h:form>.

<form id="fLogin" action="j_security_check">
Run Code Online (Sandbox Code Playgroud)