2014-01-16 35 views
1

首先,我想爲我的糟糕英語道歉。好的,我有一個問題。我正在構建使用jdbcrealm和web.xml來確保安全性的應用程序。登錄是通過網頁表單完成的。應用程序在Apache Tomcat 7上運行,我正在使用Primefaces 4.0。在web.xml中,我定義了一些角色和一些安全約束。當我登錄到應用程序httpservlet request.login(用戶名,密碼)時,請做好這項工作,並且request.isUserInrole(「role」)也會做好工作,Faces.getExternalContext.redirect會將頁面重定向到正確的應用安全約束的文件夾,在瀏覽器中我看到正確的URL .....但頁面是空白的!如果我檢查頁面源,我會看到登錄頁面的頁面源.....我會在屏幕下方放一些截圖。請幫助我...我現在嘗試解決問題2周!web.xml問題

/*

*/ 這是web.xml中

<param-name>primefaces.THEME</param-name> 

    <param-value>afterdark</param-value> 

</context-param> 

<servlet> 

    <servlet-name>Faces Servlet</servlet-name> 

    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 

    <servlet-name>Faces Servlet</servlet-name> 

    <url-pattern>*.xhtml</url-pattern> 
</servlet-mapping> 
<security-role> 
    <description>Administrator A</description> 
    <role-name>1</role-name> 
</security-role> 
<security-constraint> 
    <display-name>Administrator A</display-name> 
    <web-resource-collection> 
     <web-resource-name>Administratorske datoteke</web-resource-name> 
     <description/> 
     <url-pattern>/a1/*</url-pattern> --> 
     <http-method>GET</http-method> 
     <http-method>POST</http-method>   
    </web-resource-collection> 
    <auth-constraint> 
     <description>Administrator A</description> 
     <role-name>1</role-name> 
     </auth-constraint> 
</security-constraint> 


    <login-config> 

    <auth-method>FORM</auth-method> 
    <realm-name>JDBCRealm</realm-name> 
    <form-login-config> 
     <form-login-page>/prijava.xhtml</form-login-page> 
     <form-error-page>/pogreska.xhtml</form-error-page> 
    </form-login-config> 
</login-config> 

<session-config> 
    <session-timeout> 
     30 
    </session-timeout> 
</session-config> 

<welcome-file-list> 
    <welcome-file>prijava.xhtml</welcome-file> 
</welcome-file-list> 

* 這是登錄頁(prijava.xhtml) *

 <div class="slika_za_prijavu"> 

     <h:outputLink id="loginLink" value="javascript:void(0)" onclick="PF('prozor_za_unos').show()" title="prijava"> 
      <p:graphicImage value="/slike/prijava.png" /> 

     </h:outputLink> 

     </div>   

     <p:growl id="growl" showDetail="true" life="3000" /> 

     <p:dialog id="prozor_za_prijavu" header="Prijava" widgetVar="prozor_za_unos" resizable="false"> 

     <h:panelGrid columns="2" cellpadding="5"> 
     <h:outputLabel for="k_ime" value="Korisničko ime:" /> 
     <p:inputText value="#{provjera_prijave.k_ime}" 
       id="k_ime" required="true" label="korisnicko_ime" 
       requiredMessage="Potrebno je upisati korisničko ime!"/> 

     <h:outputLabel for="zaporka" value="Zaporka:" /> 
     <h:inputSecret value="#{provjera_prijave.zaporka}" 
       id="zaporka" required="true" label="zaporka" 
       requiredMessage="Potrebno je upisati zaporku!"/> 

     <f:facet name="footer"> 
      <p:commandButton id="gumb_za_prijavu" value="Prijavi se" update="growl" 
          actionListener="#{provjera_prijave.prijava(actionEvent)}" 
       oncomplete="obrada_zahtjeva_za_prijavu(xhr, status, args)"/> 
     </f:facet> 
     </h:panelGrid> 

     </p:dialog> 
    </h:form> 

這登錄控制器(provjera_prijave)

公共無效prijava(ActionEvent的動作事件)拋出IOException異常{

FacesMessage poruka = null; 
    FacesContext fc = FacesContext.getCurrentInstance(); 
    HttpServletRequest zahtjev = (HttpServletRequest) fc.getExternalContext().getRequest(); 
    try { 
     String pocetna_stranica;    
     zahtjev.login(k_ime, zaporka); 
     HttpSession sesija = zahtjev.getSession(); 
     if (!sesija.isNew()) { 
      sesija.invalidate(); 
      sesija = zahtjev.getSession();         
     } 
     if (zahtjev.isUserInRole("1")) { 
      sesija.setAttribute("trenutni_korisnik",k_ime); 
      pocetna_stranica = "/a1/pocetna_a1.xhtml"; 
      poruka = new FacesMessage(FacesMessage.SEVERITY_INFO, "Dobro došao", k_ime); 
      try { 
       fc.getExternalContext().getFlash().setKeepMessages(true); 
       fc.getExternalContext().redirect(zahtjev.getContextPath()+pocetna_stranica); 

      } 
      catch (IOException ex) { 
       fc.addMessage(null, new FacesMessage("UPOZORENJE!", "Pogreška u izvođenju programa. Nije moguće preusmjeriti stranicu.")); 
      } 
     } 
     else if (zahtjev.isUserInRole("2")) { 

,這是URL這是在我的瀏覽器時,角色爲「1」的用戶登錄。在此之前,本地主機和端口... ERMP是應用程序的神祕......

「ERMP/A1/pocetna_a1.xhtml」

這裏是登錄頁面

我希望這個問題是可以理解的頁面源代碼的空白頁。 當在web.xml家居點評網資源工作 請幫助!謝謝

回答

0

當你想使用內置的(Realm)認證和授權的Tomcat的,有幾件事情你應該照顧。

首先,你的登錄表單需要是這樣的:

<form action="j_security_check" method="post"> 
    <input type="text" name="j_username" placeholder="Login"/> 
    <input type="password" name="j_password" placeholder="Password"/> 
    <input type="submit" value="Sign In" /> 
</form> 

你可以使用primefaces組件保留佈局。在這種情況下,您將需要執行一些「javascripting」,以便定義由JSF h:表格組件組成的格式的操作。 ...

<script> 
jQuery("#form").submit(function() { 
    jQuery(this).attr("action", "j_security_check"); 
    jQuery(loginVar.jqId).attr("name", "j_username"); 
    .... 
}); 
</script> 

第二個細節是,你將不必擔心在登錄部分在您登錄控制器描述。一旦將j_username和j_password發送到j_security_check,一切都會運行良好。

+0

謝謝!我設法去希望的頁面,這是改善,但我只能做到這一點與一個用戶...如果我爲用戶定義歡迎頁面與角色「1」我可以看到頁面,但用戶與角色「2」不能看到頁面....你知道如何在web.xml中定義多個歡迎頁面嗎? – nikolabu