【问题标题】:jsf2 facesmessage for spring-security badcredentialsjsf2 facesmessage for spring-security badcredentials
【发布时间】:2011-12-24 02:14:23
【问题描述】:

我为 Spring Security 使用 facelets 登录表单:

<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 标记中显示 facemessage?

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

我正在尝试另一种方式,像这样配置 Spring Security:

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

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

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

有人知道怎么做吗?

非常感谢您。

【问题讨论】:

    标签: exception jsf-2 spring-security message


    【解决方案1】:

    然后在登录页面中,捕获 GET 参数“错误”。但是我怎样才能以这种方式显示面部消息?

    这边:

    <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 :)

    【讨论】:

    • 我不认为这是 hacky。我比相位监听器方式更喜欢它。但是,我更喜欢覆盖 Spfing Security 的消息属性文件,但我不知道该怎么做。我试过了,但没有用。
    • 我的意思是更多的hacky,错误页面是可收藏和可操作的,最终可能导致最终用户混淆。
    • 你说得对,谢谢。顺便说一句,你知道如何使用 Spring Security 中的消息属性文件来做到这一点吗?我的意思是覆盖其中的“badCredentials”消息,并显示一个面孔消息。我看到了mkyong.com/spring-security/…,但我认为这是在表示级别使用 Spring,而不是 JSF。
    • 我也不是,只是想知道您是否知道如何显示覆盖来自 Spring Security 的消息文件的 facesmessage。我对如何显示错误的凭据消息有疑问...我不喜欢使用阶段侦听器(因为它会为每个呈现的页面调用,而不仅仅是登录页面)和 GET 参数方式是hacky(如你所说)。我认为它不能通过托管 bean 方法(如 PostConstruct)来完成,因为当时 Spring Security 还没有将 BadCredentials Exception 对象放在 HttpSession 中。我真的很乱,我想我会使用GET参数。
    • 您也可以在视图中选择非facesmessage 方式。类似&lt;c:if test="#{not empty sessionScope['some.spring.specific.message.key']}"&gt;Error: #{sessionScope['some.spring.specific.message.key']}&lt;/c:if&gt;
    猜你喜欢
    • 1970-01-01
    • 2015-07-28
    • 2017-10-30
    • 2014-11-18
    • 1970-01-01
    • 2011-12-29
    • 1970-01-01
    • 2017-05-13
    • 2014-05-28
    相关资源
    最近更新 更多