Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Exception 用于spring安全认证的jsf2 facesmessage_Exception_Jsf 2_Spring Security_Message - Fatal编程技术网

Exception 用于spring安全认证的jsf2 facesmessage

Exception 用于spring安全认证的jsf2 facesmessage,exception,jsf-2,spring-security,message,Exception,Jsf 2,Spring Security,Message,我使用facelets登录表单实现Spring安全: <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="#{autent

我使用facelets登录表单实现Spring安全:

<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>`
它工作得很好,但是如果Spring Security引发BadCredentials异常,我如何在我的h:messages标记中显示facesmessage

我知道可以用一个阶段监听器来完成,但我不喜欢这种方式(处理监听器中的异常)

我正在尝试另一种方式,如下配置Spring安全性:

authentication-failure-url="/faces/paginas/autenticacion/login.xhtml?error=1
然后在登录页面中,捕获GET参数“error”。但是我怎样才能以这种方式显示facesmessage

我尝试的另一种方法是覆盖Spring安全性的消息属性文件(覆盖密钥“badcredentials”的消息),但它也不起作用(我不知道如何显示消息)

有人知道怎么做吗

事先非常感谢

然后在登录页面中,捕获GET参数“error”。但是我怎样才能以这种方式显示facesmessage

这样:

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

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

无论哪种方式,这感觉都很粗糙:)

我不认为这很粗糙。我更喜欢它,而不是相位侦听器方式。然而,我更喜欢从Spfing安全性中隐藏消息属性文件,但我不知道如何做到这一点。我试过了,但没有成功。使用hacky,我的意思是更多,错误页面是可书签的,并且可以操作,这可能会导致最终用户的困惑。你是对的,谢谢。顺便问一下,您知道如何使用SpringSecurity中的消息属性文件来完成这项工作吗?我的意思是覆盖消息中的“badCredentials”,并显示一个FacesMessage。我看到了,但我认为这是为了在表示层使用Spring,而不是JSF。我也不是,只是想知道您是否知道如何显示覆盖Spring安全性中消息文件的facesmessage。我对如何显示错误的凭证消息有疑问……我不喜欢使用阶段侦听器(因为它会被调用到每个呈现的页面,而不仅仅是登录页面),而且GET参数的方式很粗糙(如您所说)。我认为这也不能通过托管bean方法(比如PostConstruct)来实现,因为当时Spring安全性还没有将BadCredentials异常对象放在HttpSession中。我真的搞砸了,我想我会使用GET参数。你也可以在视图中使用非facesmessage方式。类似于
错误:#{sessionScope['some.spring.specific.message.key']}
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"));
    }
}