【问题标题】:get j_username in j_security_check JSF 2.0在 j_security_check JSF 2.0 中获取 j_username
【发布时间】:2014-02-28 01:40:14
【问题描述】:

我有一个简单的页面,我想检索j_username 以将其作为登录用户保存在会话中,我无法从表单中获取它。这里服务器在这段代码中自行执行身份验证

<h:form id="login" onsubmit="document.getElementById('login').action='j_security_check';" prependId="false">
                <h:panelGrid columns="2">
                    <p:outputLabel for="j_username" value="Username" />
                    <p:inputText id="j_username" name="j_username"/>            
                    <p:outputLabel for="j_password" value="Password" />
                    <p:password id="j_password" name="j_password" value=""/>
                    <p:commandButton style="font-size:15px" type="submit"  value="Login" ajax="false"/>
                    <p:commandButton style="font-size:15px" type="clear" value="Clear" ajax="false"/>
                </h:panelGrid>
            </h:form>

【问题讨论】:

    标签: jsf-2 j-security-check


    【解决方案1】:

    它由HttpServletRequest#getRemoteUser() 提供,它在由ExternalContext#getRemoteUser() 委托的JSF 上下文中。

    所以,这应该在视图中执行:

    <ui:fragment rendered="#{empty request.remoteUser}">
        <p>This is only rendered when someone's NOT logged in.</p>
    </ui:fragment>
    <ui:fragment rendered="#{not empty request.remoteUser}">
        <p>This is only rendered when someone's been logged in.</p>
        <p>Welcome, you're logged in as #{request.remoteUser}!</p>
    </ui:fragment>
    

    或在托管 bean 中:

    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    String username = ec.getRemoteUser();
    // ...
    

    与具体问题无关:最好摆脱onsubmit 的废话,只使用普通的&lt;form&gt; 而不是&lt;h:form&gt;

    <form id="login" action="j_security_check">
    

    这样它也可以在禁用 JS 的客户端上工作。

    另见:

    【讨论】:

    • 由于本地主机上的代理设置为空,在服务器上工作正常。感谢 BalusC 的回复和帮助
    猜你喜欢
    • 2011-06-29
    • 2011-01-29
    • 1970-01-01
    • 2014-02-25
    • 2011-02-01
    • 2011-02-28
    • 2011-09-25
    • 2011-11-07
    • 1970-01-01
    相关资源
    最近更新 更多