我有一个实体类 User
包含用户名、名字、姓氏和密码等信息,并且我设置了 GlassFish 3.1 服务器来执行身份验证。到现在为止还挺好。在容器对用户进行身份验证后,我需要某种方式将主体绑定(bind)到实际的用户实体。毕竟,GlassFish 告诉我的是用户“laurens”已通过身份验证,而不是给我相应的 User
实体。
为此,我编写了一个 JSF 托管 bean UserController
.我想知道的是,这是否是查看实际实体的正确方法,以及是否有任何我没有看到的明显陷阱。UserController
具有以下字段:
@EJB
private UserFacade userFacade;
private User user;
userFacade
是一个无状态 session bean,用于持久化和查找 User
实例。 user
JSF 页面使用字段来获取和设置用户的属性。我使用以下方法来执行绑定(bind)以及两个辅助方法:
@PostConstruct
private void init() {
try {
user = userFacade.find(getUserPrincipal().getName());
} catch (NullPointerException ex) {
// Intentionally left empty -- User is not logged in.
}
}
private HttpServletRequest getHttpServletRequest() {
return (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
}
private Principal getUserPrincipal() {
return getHttpServletRequest().getUserPrincipal();
}
JSF 页面使用以下方法来确定要显示哪些组件(如果用户已经通过身份验证,则无需显示登录表单),如果单击“登录”按钮,则对用户进行身份验证,或注册为单击“注册”按钮时的新用户。
public boolean isAuthenticated() {
return getUserPrincipal() != null;
}
public void authenticate() {
try {
getHttpServletRequest().login(user.getEmailAddress(), user.getPassword());
} catch (Exception ex) {
// TODO: Handle failed login attempt
}
}
public void register() {
userFacade.create(user);
}
这是正确的方法吗?
谢谢!
编辑:
感谢您的输入!我考虑了一下,虽然我认为将密码移动到不同的表对我来说目前处理有点太多,但我确实认为我可以通过分离
UserController
来解决一些问题。在 @RequestScoped
AuthenticationController
和一个精简的@SessionScoped
UserController
.AuthenticationController
会有 emailAddress
和 password
字段,由网页的 emailAddress 和密码字段绑定(bind)。它还包含 public void authenticate()
对用户进行身份验证并在之后丢弃凭据。 @SessionScoped
UserController
然后可以绑定(bind)到适当的 User
实体而无需知道密码。事实上,我相信我可以从 User
中删除密码字段。共。
最佳答案
你提出的方法有一些粗糙的边缘,但在大多数情况下它是很好的。
如果您打算存储对 User
的引用实体,那么最好在 SessionScoped
中这样做托管 bean 。这有利有弊。明显的优势在于
User
实体在整个应用程序流程中,跨所有页面都可用。这意味着您需要绑定(bind) Principal
到 User
实体一次 session 。如果需要,您可以在所有页面中重复使用绑定(bind)值。 不那么明显的缺点是
password
字段将在内存中存储很长时间。充其量,您应该尝试在身份验证尝试后使实体的密码字段无效(无论是否成功,无论该字段包含明文密码还是散列密码)。此外,定义 password
也很有意义。延迟获取的字段( FetchType
的 LAZY
),而不是预先获取的默认值( FetchType
的 EAGER
)。如果你实现了这个(特别是密码字段的无效化),你需要注意涉及在 User
上进行的合并操作的问题。实体;在这种情况下,最好有一个单独的实体来存储用户的密码(非常不幸,但在某些应用程序中,您必须弯腰保护密码及其哈希值)。 话虽如此,还需要确保以下几点:
User
时。实体而不是 Principal
对象以强制执行访问控制检查。 更新
这是基于编辑过的问题。如果您实现
authenticate
按照建议的方法,您可以实现您的 User
绑定(bind)方案。实体到 Principal
只有在认证成功后。以下是我的应用程序中类似实现的复制:
public String authenticate()
{
String result = null;
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
try
{
request.login(userId, password);
result = "/private/MainPage.xhtml?faces-redirect=true";
}
catch (ServletException ex)
{
logger.error("Failed to authenticate user.", ex);
FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, Messages.getString("Login.InvalidIdOrPasswordMessage"), null);
FacesContext.getCurrentInstance().addMessage(null, facesMessage);
}
return result;
}
这是从facelet调用的:
<h:form id="LoginForm" acceptcharset="UTF-8">
<p>
<h:outputLabel for="userid" value="#{msg['Login.userid.label']}" />
<h:inputText id="userid" value="#{loginBean.userId}" />
</p>
<p>
<h:outputLabel for="password" value="#{msg['Login.password.label']}" />
<h:inputSecret id="password" value="#{loginBean.password}" />
</p>
<h:commandButton id="submit" value="#{msg['Login.submit.label']}"
action="#{loginBean.authenticate}" />
</h:form>
请注意在身份验证成功的情况下使用 POST-REDIRECT-GET 模式。我留下了一些与重定向前当前 session 失效相关的代码,以防止 session 固定攻击。
User
的绑定(bind)实体到 Principal
将在新 session 中完成,只要它是在 session 范围的 bean 中完成的。
关于java - 绑定(bind)用户实体和 GlassFish 主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6819793/