2017-05-24 62 views
1

我有一个登录表单来验证用户是否在使用LDAP的活动目录中。我使用Apache Shiro来做这件事(我相信这不是重要的事情,但我最好提一下,因为它可能会影响我尝试工作的内容)。如果用户输入错误的密码,则会抛出异常,我希望在我的表单所在的同一页面上显示错误消息。因此,我按照建议的地方写了一个自定义异常处理程序,以指定错误消息下面是代码的相关部分:在同一页面上显示错误消息

的index.xhtml(我的登录表单):

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:f="http://xmlns.jcp.org/jsf/core" 
     xmlns:h="http://xmlns.jcp.org/jsf/html" 
> 

<h:form> 
    <h:messages globalOnly="false"/> 
    <h:panelGrid columns="2"> 
     <h:outputLabel for="username">Username:</h:outputLabel> 
     <h:inputText id="username" value="#{user.userName}"/> 
     <h:outputLabel for="password">Password:</h:outputLabel> 
     <h:inputSecret id="password" value="#{user.password}"/> 
     <h:message for="username" infoStyle="color:darkgreen" errorStyle="color:red"/> 
     <h:message for="password" infoStyle="color:darkgreen" errorStyle="color:red"/> 
    </h:panelGrid> 
    <h:commandButton value="Login" action="#{loginController.login}" rendered="#{!loginController.loggedIn}"/> 
    <h:commandButton value="Logout" action="#{loginController.logout}" rendered="#{loginController.loggedIn}"/> 
</h:form> 
</html> 

登录方法从LoginController中类:

public String login() { 
     currentUser = ldapUserProvider.provide(); //gets Apache Shiro's Subject which is the user being authenticated. 
     if (!currentUser.isAuthenticated()) { 
      CustomUsernamePasswordToken token = new CustomUsernamePasswordToken(DOMAIN, user.getUserName(), user.getPassword()); 
      token.setRememberMe(true); 
      try { 
       currentUser.login(token); //this one causes exception if the password is wrong, for example 
       loggedIn = true; 
       return "success.xhtml"; 
      } catch (Exception e) { 
       return "index.xhtml"; //in this case I'd like to show the error message and prompt to login again 
      } 
     } 
     return "index.xhtml"; 
    } 

定制的ExceptionHandler:

package com.flyeralarm.tools.apss; 

import javax.faces.application.FacesMessage; 
import javax.faces.context.ExceptionHandler; 
import javax.faces.context.ExceptionHandlerWrapper; 
import javax.faces.context.FacesContext; 
import javax.faces.context.Flash; 
import javax.faces.event.ExceptionQueuedEvent; 
import javax.faces.event.ExceptionQueuedEventContext; 
import java.util.Iterator; 

public class AuthenticationExceptionHanlder extends ExceptionHandlerWrapper { 
    private ExceptionHandler wrappedExceptionhandler; 

    public AuthenticationExceptionHanlder(final ExceptionHandler wrappedExceptionhandler) { 
     this.wrappedExceptionhandler = wrappedExceptionhandler; 
    } 

    @Override 
    public ExceptionHandler getWrapped() { 
     return wrappedExceptionhandler; 
    } 

    @Override 
    public void handle() { 
     Iterator<ExceptionQueuedEvent> eventIterator = getUnhandledExceptionQueuedEvents().iterator(); 
     while (eventIterator.hasNext()) { 
      ExceptionQueuedEvent event = eventIterator.next(); 
      ExceptionQueuedEventContext eventContext = (ExceptionQueuedEventContext) event.getSource(); 
      Throwable exception = eventContext.getException(); 
      handleException(exception, eventContext); 
      eventIterator.remove(); 
      getWrapped().handle(); 
     } 
    } 

    private void handleException(Throwable exception, ExceptionQueuedEventContext context) { 
     FacesMessage exceptionMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Login failed.", exception.getMessage()); 
     FacesContext facesContext = FacesContext.getCurrentInstance(); 
     Flash flash = facesContext.getExternalContext().getFlash(); //I read that putting the message into flash would do the work but it didn't 
     flash.put("errorDetails", exception.getMessage()); 
     facesContext.addMessage(null, exceptionMessage); 
     facesContext.getApplication().getNavigationHandler().handleNavigation(facesContext, null, "index?faces-redirect=true"); 
    } 
} 

我也尝试过不同的组合,只是全局= true和false,但页面仍然存在l不显示我在自定义异常处理程序中定义的错误消息。我在做什么?错误?

重复的问题免责声明:我也搜索了SO网站寻找可能的解决方案,并尝试了不同答案中提出的事情,但没有任何帮助。

+0

了解“AJAX” – Kukeltje

+0

我开始在这个题目移动为好。拉胡尔的解决方案出了什么问题?发现异常时,您没有添加任何消息。 – Al1

+0

我会接受拉胡尔的回答,因为这条线实际上是做这项工作的。我的代码中的问题是行''''''''''''''''''''''''''''''删除行显示错误消息。 –

回答

1

如果您在支持bean中正确添加消息,globalOnly开关应该可以工作。客户端识别必须在FacesContext.addMessage(...)为空:

FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(message)); 
0

试着改变你的渔获物return "index.xhtml";返回return null;这可能导致被完全重新加载你的页面,让错误状态迷路,或者你可以在你AuthenticationExceptionHanlder#handleException方法

facesContext.getCurrentInstance().validationFailed(); 

尝试addind此行应该向jsf表明存在验证错误,防止刷新发生。

+0

不幸的是,两人都没有帮助。 –

相关问题