开发者

Best approach to store logged in User Id

I have small application using JSF 1.1. It is using NTLM for authentication.

What is the best approach to store the logged in user_id, so that could be used in application across all java classes, user_id would be used in 开发者_高级运维almost all jsf pages corresponding java classes.

Any help is highly appreciable.

Thanks


store it in the web session :

session=FacesContext.getExternalContext().getSession();
session.setAttribute("user_id",user_id);


This would depend on how you are performing authentication. If you are delegating the authentication process to the container, by using FORM, BASIC or DIGEST authentication, then you can obtain the Principal object associated with the current request using the following snippet:

Principal user = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();

The name of the user in the realm can be obtained using the user.getName() method. Under the hood, the container stores the Principal object in the HttpSession instance, but this is not accessible as an attribute using the HttpSession.getAttribute(...) method, in most containers.

The Principal object is also automatically propagated by the Servlet container to the EJB container, when the business method of an EJB is invoked, and you can access it using the SessionContext interface. In an EJB, you can perform the following, to obtain a reference to the Principal:

@Resource
private SessionContext ctx;

public void businessMethod()
{
    Principal user = ctx.getCallerPrincipal();
    ...
}

If you are not delegating authentication to the container, and instead, you are performing authentication using your own scheme, then you must store the reference to the identity of the user in the HttpSession object upon successful authentication, and you must clear this upon session invalidation. Storing it in other scopes is not recommended, as only a HttpSession object has the same lifetime as a user's session with the application.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜