Java 如何从j_安全检查中获取登录详细信息?
我正在为我的登录使用jsecurity check,我想保存登录的用户以及他们登录的时间 我怎么从j_安全检查处拿到这些 我的web xml:Java 如何从j_安全检查中获取登录详细信息?,java,jsf,jsf-2,Java,Jsf,Jsf 2,我正在为我的登录使用jsecurity check,我想保存登录的用户以及他们登录的时间 我怎么从j_安全检查处拿到这些 我的web xml: <login-config> <auth-method>FORM</auth-method> <realm-name>user-realm</realm-name> <form-login-config> <form-login-
<login-config>
<auth-method>FORM</auth-method>
<realm-name>user-realm</realm-name>
<form-login-config>
<form-login-page>/Pages/Login.xhtml</form-login-page>
<form-error-page>/Pages/LoginError.xhtml</form-error-page>
</form-login-config>
</login-config>
<security-role>
<description/>
<role-name>admin</role-name>
</security-role>
<security-role>
<description/>
<role-name>users</role-name>
</security-role>
形式
用户领域
/Pages/Login.xhtml
/Pages/LoginError.xhtml
管理
使用者
我的login.xhtml页面:
<form role="form" action="j_security_check" method="POST">
<h2>Please sign in</h2>
<input name="j_username" type="text" placeholder="Username" required="true"> </input>
<input name="j_password" type="password" placeholder="Password" required="true"> </input>
<button type="submit" value="Login">Sign in</button>
</form>
请登录
登录
我只是想知道我是否能找到登录到我的jsf应用程序的用户,并保存该用户以及他们登录数据库的时间。我在这个博客上找到了答案: 将j_security_check登录页面替换为带有支持bean的登录页面 Login.xhtml:
<h:form>
<h:panelGrid columns="2">
<h:outputLabel for="username" value="Username:" />
<h:inputText id="username" value="#{authBean.username}" />
<h:outputLabel for="password" value="Password:" />
<h:inputSecret id="password" value="#{authBean.password}" />
<h:commandButton id="loginButton" value="Login" action="#{authBean.login()}" />
</h:panelGrid>
</h:form>
你的密码在哪里?在问题中张贴您的代码,请参阅。
private String username;
private String password;
public String login(){
FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) context
.getExternalContext().getRequest();
try {
request.login(username, password);
} catch (ServletException e) {
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Login failed!", null));
return "login";
}
//database action to be done here
Principal principal = request.getUserPrincipal();
if(request.isUserInRole("admin")) {
return "/Pages/AdminLanding.xhtml";
} else {
return "/Pages/UserLanding.xhtml";
}
} //getter and setter for username and password