2015-08-26 33 views
1

我有一個JSF 2.2應用程序,PrimeFaces 5.2和基於表單的登錄結合使用Wildfly 8.2和PostgreSQL的JBoss安全領域。PrimeFaces Mobile 5.2和基於表單的JAAS登錄不能一起工作

這是工作正常,絕對如預期。現在的問題是使用PrimeFaces Mobile的上述設置。輸入用戶名和密碼並單擊登錄按鈕後,沒有任何反應,因爲我重定向到登錄視圖,並重新定向到原始請求的頁面或錯誤頁面。

讓我們先從我的手機登錄表單:

<ui:define name="content"> 
    <pm:content styleClass="content"> 
     <h:form id="loginForm" method="POST" prependId="false" 
       onsubmit="document.getElementById('loginForm').action = 'j_security_check';"> 
       <p:focus for="j_username"/> 
      <pm:field> 
      <p:outputLabel value="Benutzername"></p:outputLabel> 
      <h:inputText id="j_username" name="j_username" required="true" /> 
     </pm:field> 
     <pm:field> 
      <p:outputLabel value="Passwort"></p:outputLabel> 
      <p:password id="j_password" name="j_password" redisplay="false" required="true" /> 
     </pm:field> 
     <pm:field> 
      <p:commandButton id="login" value="Login" ajax="false" /> 
     </pm:field> 
     </h:form> 
    </pm:content> 
</ui:define> 

正如前面所提到的形式定義的工作原理與非移動版本,我剛添加標籤pm_contentpm:field移動版本。

當檢查在Chrome所生成的DOM我可以看到,在網頁的呈現的移動版本具有的形式和輸入元素比非移動版本不同的ID並且還onsubmit丟失:

<form id="j_idt6:loginForm" name="j_idt6:loginForm" method="post" action="/MyApp/login.xhtml" enctype="application/x-www-form-urlencoded"> 
<input type="hidden" name="j_idt6:loginForm" value="j_idt6:loginForm"> 
<span id="j_idt6:j_idt32"></span><script type="text/javascript">$(function(){PrimeFaces.focus('j_idt6:username');});</script><div class="ui-field-contain"><label id="j_idt6:j_idt16" class="ui-outputlabel ui-widget">Benutzername</label><div class="ui-input-text ui-body-inherit ui-corner-all ui-shadow-inset"><input id="j_idt6:username" type="text" name="j_idt6:username"></div></div><div class="ui-field-contain"><label id="j_idt6:j_idt18" class="ui-outputlabel ui-widget">Passwort</label><div id="j_idt6:password" class="ui-input-text ui-body-inherit ui-corner-all ui-shadow-inset ui-input-has-clear"><input data-role="none" id="j_idt6:password" name="j_idt6:password" type="password"><a href="#" class="ui-input-clear ui-btn ui-icon-delete ui-btn-icon-notext ui-corner-all ui-input-clear-hidden"></a></div></div><div class="ui-field-contain"><button id="j_idt6:login" name="j_idt6:login" class="ui-btn ui-shadow ui-corner-all" onclick="" type="submit">Login</button></div><input type="hidden" name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0" value="-5673897088131963149:-3654042330506594383" autocomplete="off"> 
</form> 

現在產生從非移動登錄輸出:

<form id="loginForm" name="loginForm" method="post" action="/MyApp/login.xhtml" enctype="application/x-www-form-urlencoded" onsubmit="document.getElementById('loginForm').action = 'j_security_check';"> 
<input type="hidden" name="loginForm" value="loginForm"> 
<span id="j_idt11"></span><script type="text/javascript">$(function(){PrimeFaces.focus('j_username');});</script><input id="j_username" type="text" name="j_username"><input id="j_password" type="password" name="j_password" value=""><input id="login" type="submit" name="login" value="Login"><input type="hidden" name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0" value="5464337132357101375:3961658655950415709" autocomplete="off"> 
</form> 

如果我得到它的權利似乎是在移動版本在前面加上ID的問題。

我該如何解決這個問題並在PrimeFaces Mobile 5.2中實現POST到j_security_check

+2

您應該只需使用普通的HTML表單,而不需要''上的所有那些醜陋的prependId和JS hacks。對於PrimeFaces的輸入元素look'n'feel,只需借用所需的樣式類。 – BalusC

+0

噢,很簡單,當我寫了「POST to j_security_check」時,我已經在我的問題中找到了解決方案:)我將在家中執行此操作,非常感謝! – devrys

回答

0

我能鼓搗@BalusC的提示工作的解決方案,使我的形式,現在看起來是這樣的:

<ui:define name="content"> 
     <pm:content styleClass="content"> 
      <h:form> 
       <pm:field> 
        <h:outputLabel for="username" value="Username" /> 
        <h:inputText id="username" value="#{authenticationBean.username}" 
           required="true" /> 
        <h:message for="username" /> 
       </pm:field> 
       <pm:field> 
        <h:outputLabel for="password" value="Password" /> 
        <h:inputSecret id="password" value="#{authenticationBean.password}" 
            required="true" /> 
        <h:message for="password" /> 
       </pm:field> 
       <pm:field> 
        <h:commandButton value="Login" action="#{authenticationBean.login()}" /> 
       </pm:field> 
      </h:form> 
     </pm:content> 
</ui:define> 

登錄現在登錄方法中的@ViewScoped託管bean AuthenticationBean內部程序:

public void login() { 
     log.info("Login attempt"); 

     FacesContext context = FacesContext.getCurrentInstance(); 
     ExternalContext externalContext = context.getExternalContext(); 
     HttpServletRequest request = (HttpServletRequest) externalContext.getRequest(); 

     try { 
      request.login(username, password); 
      log.info("Login successful"); 
      externalContext.redirect(originalURL); 
     } catch (ServletException e) { 
      // Handle unknown username/password in request.login(). 
      context.addMessage(null, new FacesMessage("Unknown login")); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    }