Access session scoped JSF managed bean in web filter

ⅰ亾dé卋堺 提交于 2019-11-30 11:21:04
BalusC

Under the covers, JSF stores session scoped managed beans as an attribute of the HttpSession with the managed bean name as key.

So, provided that you've a @ManagedBean @SessionScoped public class User {}, just this should do inside the doFilter() method:

HttpSession session = ((HttpServletRequest) request).getSession(false);
User user = (session != null) ? (User) session.getAttribute("user") : null;

if (user != null && user.isLoggedIn()) {
    // Logged in.
}

Or, if you're actually using CDI instead of JSF to manage beans, then just use @Inject directly in the filter.

See also:

As an alternative you can use CDI-beans and inject your sessionbean normally.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!