2011-04-13 45 views
1

我想了解爲什麼我的登錄表單不顯示驗證消息,說「錯誤的電子郵件或密碼」當輸入密碼錯誤時。在所有其他情況下能夠正常工作(只是案件4不工作):當用戶輸入錯誤的密碼時,驗證消息不會顯示在登錄表單中

案例1件作品沒有問題(無輸入):

enter image description here

案例2名的作品沒有問題(僅輸入電子郵件中給出): enter image description here

案例3件作品沒有問題(僅適用於密碼給定的輸入): enter image description here

案例4不起作用(給出錯誤的兩個輸入)

enter image description here

這是4的情況下不能正常工作,這裏是源代碼:

在JSF頁面的表單:

<h:form> 
    <p:panel>     
       <h:outputText value="*[email protected]:" /> 
       <h:inputText id="email" value="#{securityController.email}" binding="#{emailComponent}"/>     
       <br/> 
       <h:outputText value="*Lozinka: " /> 
       <h:inputSecret id="password" value="#{securityController.password}" validator="#{securityController.validate}">      
        <f:attribute name="emailComponent" value="#{emailComponent}" /> 
       </h:inputSecret>    

       <br/> 
       <span style="color: red;"><h:message for="password" 
       showDetail="true" /></span> 
       <br/> 
       <h:commandButton value="Login" action="#{securityController.logIn()}"/>     

      </p:panel> 
     </h:form> 

管理bean從輸入獲取值的字段

@ManagedBean 
@RequestScoped 
public class SecurityController { 

    @EJB 
    private IAuthentificationEJB authentificationEJB; 
    private String email; 
    private String password; 
    private String notificationValue; 

    public String logIn() { 
     if (authentificationEJB.saveUserState(email, password)) { 
      notificationValue = "Dobro dosli"; 
      return "main.xhtml"; 
     } else { 
      return ""; 
     } 

    } 

    public void validate(FacesContext context, UIComponent component, 
      Object value) throws ValidatorException { 

     UIInput emailComponent = (UIInput) component.getAttributes().get(
       "emailComponent"); 
     String email = ""; 
     String password = ""; 
     email = (String) emailComponent.getValue(); 
     password = (String) value; 

     String emailInput = email; 
     String emailPatternText = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; 
     Pattern emailPattern = null; 
     Matcher emailMatcher = null; 
     emailPattern = Pattern.compile(emailPatternText); 
     emailMatcher = emailPattern.matcher(emailInput); 

     String passwordInput = password; 
     String alphanumericPattern = "^[a-zA-Z0-9]+$"; 
     Pattern passwordPattern = null; 
     Matcher passwordMatcher = null; 
     passwordPattern = Pattern.compile(alphanumericPattern); 
     passwordMatcher = passwordPattern.matcher(passwordInput); 

     if (!emailMatcher.matches() && !passwordMatcher.matches()) { 
      if (authentificationEJB.checkCredentials(emailInput, passwordInput) == false) { 
       FacesMessage msg = new FacesMessage(
         "Pogresan email ili lozinka"); 
       throw new ValidatorException(msg); 
      } 
     } 
     if(emailInput == null || passwordInput == null) { 
      FacesMessage msg = new FacesMessage("Pogresan email ili lozinka"); 
      throw new ValidatorException(msg); 
     } 
     if (passwordInput.length() <= 0 || emailInput.length() <= 0) { 
      FacesMessage msg = new FacesMessage("Pogresan email ili lozinka"); 
      throw new ValidatorException(msg); 
     } 
    } 

    public String getEmail() { 
     return email; 
    } 

    public String getPassword() { 
     return password; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 

    public String getNotificationValue() { 
     return notificationValue; 
    } 

    public void setNotificationValue(String notificationValue) { 
     this.notificationValue = notificationValue; 
    } 
} 

訪問數據庫和EJB檢查憑證:

package ejbs; 

import java.util.List; 
import javax.ejb.Stateful; 
import javax.faces.context.FacesContext; 
import javax.persistence.EntityManager; 
import javax.persistence.PersistenceContext; 
import javax.persistence.Query; 

import ejbinterfaces.IAuthentificationEJB; 
import entities.Role; 

@Stateful(name = "ejbs/AuthentificationEJB") 
public class AuthentificationEJB implements IAuthentificationEJB { 

    @PersistenceContext 
    private EntityManager em; 

    // Login 
    public boolean saveUserState(String email, String password) { 
     // 1-Send query to database to see if that user exist 
     Query query = em 
       .createQuery("SELECT r FROM Role r WHERE r.email=:emailparam AND r.password=:passwordparam"); 
     query.setParameter("emailparam", email); 
     query.setParameter("passwordparam", password); 
     // 2-If the query returns the user(Role) object, store it somewhere in 
     // the session 
     List<Object> tmpList = query.getResultList(); 
     if (tmpList.isEmpty() == false) { 
      Role role = (Role) tmpList.get(0); 
      if (role != null && role.getEmail().equals(email) 
        && role.getPassword().equals(password)) { 
       FacesContext.getCurrentInstance().getExternalContext() 
         .getSessionMap().put("userRole", role); 
       // 3-return true if the user state was saved 
       System.out.println(role.getEmail() + role.getPassword()); 
       return true; 
      } 
     } 
     // 4-return false otherwise 
     return false; 
    } 

    // Logout 
    public void releaseUserState() { 
     // 1-Check if there is something saved in the session(or wherever the 
     // state is saved) 
     if (!FacesContext.getCurrentInstance().getExternalContext() 
       .getSessionMap().isEmpty()) { 
      // 2-If 1 then flush it 
      FacesContext.getCurrentInstance().release(); 
     }  
    } 

    // Check if user is logged in 
    public boolean checkAuthentificationStatus() { 
     // 1-Check if there is something saved in the session(This means the 
     // user is logged in) 
     if ((FacesContext.getCurrentInstance().getExternalContext() 
       .getSessionMap().get("userRole") != null)) { 
      // 2-If there is not a user already loged, then return false 
      return true; 
     } 

     return false; 
    } 

    @Override 
    public boolean checkCredentials(String email, String password) { 
     Query checkEmailExists = em 
       .createQuery("SELECT COUNT(r) FROM Role r WHERE r.email=:emailparam AND r.password=:passwordparam"); 
     checkEmailExists.setParameter("emailparam", email); 
     checkEmailExists.setParameter("passwordparam", password); 
     int matchCounter = 0; 
     matchCounter = checkEmailExists.getResultList().size(); 
     if (matchCounter == 1) { 
      return true; 
     } 
     return false; 
    } 
} 

回答

1
if (!emailMatcher.matches() && !passwordMatcher.matches()) { 
    if (authentificationEJB.checkCredentials(emailInput, passwordInput) == false) { 
     FacesMessage msg = new FacesMessage(
       "Pogresan email ili lozinka"); 
     throw new ValidatorException(msg); 
    } 
} 

因此,當電子郵件不匹配和密碼不匹配和憑據不匹配,則將顯示錯誤消息。

這不完全是你想要的。在情況4中,電子郵件匹配。你想這樣的:

if (!emailMatcher.matches() || !passwordMatcher.matches() || !authentificationEJB.checkCredentials(emailInput, passwordInput)) { 
    FacesMessage msg = new FacesMessage("Pogresan email ili lozinka"); 
    throw new ValidatorException(msg); 
} 

這將顯示錯誤時,電子郵件不匹配,或者密碼不匹配或憑證不匹配。

+0

是的,這是它的錯誤是在條件:)感謝您的幫助:) – sfrj 2011-04-13 18:24:48

+0

不客氣。請注意,這使得另外兩個'if'檢查也是多餘的,我相信。你可以刪除它們。 – BalusC 2011-04-13 18:26:54

+0

是的,我會做一些後者:) – sfrj 2011-04-13 18:29:48

相關問題