2011-12-24 264 views
0

我用一个小面JSF2的FacesMessage登录表单为春季安全:春季安全badcredentials

<h:messages globalOnly="true" layout="table" /> 
<h:form id="formLogin" prependId="false"> 
     <h:outputLabel for="j_username" value="Usuario:" /> 
     <h:inputText id="j_username" value="#{autenticacionController.administrador.login}" /> 
     <h:outputLabel for="j_password" value="Contraseña:" /> 
     <h:inputSecret id="j_password" value="#{autenticacionController.administrador.password}" /> 
     <h:commandButton value="Entrar" action="#{autenticacionController.loginAction}" /> 
     <h:commandButton value="Cancelar" immediate="true" action="#{autenticacionController.cancelarAction}" /> 
</h:form>` 

根据loginAction方法转发这个请求:

FacesContext.getCurrentInstance().getExternalContext().dispatch("/j_spring_security_check") 

它工作正常,但怎么也如果Spring Security引发BadCredentials异常,我在h:messages标记中显示facesmessage?

我知道它可以用阶段侦听器完成,但我不喜欢那种方式(处理侦听器中的异常)。

我尝试另一种方式,配置Spring安全性是这样的:

authentication-failure-url="/faces/paginas/autenticacion/login.xhtml?error=1 

然后在登录页面,赶上GET参数“错误”。但是我怎样才能以这种方式显示脸部信息呢?

我试过的另一种方法是重写Spring Security的消息属性文件(覆盖关键字“badcredentials”的消息),但它也没有工作(我不知道如何显示消息)。

任何人都知道该怎么做?

非常感谢您提前。

回答

1

And then in the login page, catch the GET param "error". But how can I show the facesmessage in this way?

这样:

<f:metadata> 
    <f:viewParam name="error" validator="#{auth.checkErrors}" /> 
</f:metadata> 
<h:messages /> 

public void checkErrors(FacesContext context, UIComponent component, Object value) { 
    if ("1".equals(value)) { 
     throw new ValidatorException(new FacesMessage("Invalid credentials")); 
    } 
} 

也许这样:

<f:metadata> 
    <f:viewParam name="error" value="#{auth.error}" /> 
    <f:event type="preRenderView" listener="#{auth.checkErrors}" /> 
</f:metadata> 
<h:messages /> 

private int error; 

public void checkErrors() { 
    if (error == 1) { 
     FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Invalid credentials")); 
    } 
} 

无论哪种方式,这感觉很hacky :)

+0

我不认为这是哈克。我更喜欢它比阶段监听器更好。不过,我宁愿从Spfing安全中过滤消息属性文件,但我不知道该怎么做。我尝试过,但没有奏效。 – choquero70 2011-12-25 16:13:03

+0

随着hacky我的意思更多,错误页面是书签和可操作的,可能会导致最终用户混淆。 – BalusC 2011-12-25 16:14:51

+0

你说得对,谢谢。顺便说一下,你知道如何使用Spring Security的消息属性文件来完成它吗?我的意思是覆盖其中的“badCredentials”消息,并显示facemesage。我看到http://www.mkyong.com/spring-security/display-custom-error-message-in-spring-security/,但我认为这是在演示级使用Spring,而不是JSF。 – choquero70 2012-01-03 19:33:02