在j_security_check JSF 2.0中获取j_username [英] get j_username in j_security_check JSF 2.0
本文介绍了在j_security_check JSF 2.0中获取j_username的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的页面,我想检索j_username
以将其保存为会话中的登录用户,我无法从表单中获取它.服务器在此代码中自行执行身份验证
I have simple page, I want to retrieve j_username
to save it in session as a logged in user, i can't fetch it from the form. here Server it self perform authentication in this code
<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>
推荐答案
ExternalContext#getRemoteUser()
.
因此,这应该在视图中执行:
So, this should do either in the view:
<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
废话,而只需使用普通的<form>
而不是<h:form>
.
Unrelated to the concrete problem: it's better to get rid of that onsubmit
nonsense and just use plain <form>
instead of <h:form>
.
<form id="login" action="j_security_check">
这样,它也可以在禁用JS的客户端上使用.
This way it'll also work on JS-disabled clients.
- 执行使用j_security_check 在Java EE/JSF中进行用户身份验证
- Performing user authentication in Java EE / JSF using j_security_check
这篇关于在j_security_check JSF 2.0中获取j_username的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文