How could I read a JSF session bean from a filter?

大憨熊 提交于 2019-12-23 23:22:13

问题


I'm searching but I can't find the answer, I need secure resources based on permissions, I can't use a filter because FacesContext is not initialized before and I need load the permissions in my session bean. Some solution avoiding use a filter? PhaseListener, ViewHandler and ResourceHandler can't capture an URL resource request, for example I need denied this direct access: http://127.0.0.1:8080/test/resources/images/image.jpg

Thx in advance...


回答1:


JSF stores session scoped managed beans as an attribute of the HttpSession, which in turn is just available in a Filter by HttpServletRequest#getSession().

HttpSession session = ((HttpServletRequest) request).getSession();
SessionBean sessionBean = session.getAttribute("sessionBean");
// ...

Update: as per the comment you seem to be actually using CDI:

my filter is triggered before than JSF, I always get a null value when I use getAttribute. I'm using CDI with 'Named' and 'SessionScoped' annotations on my Bean because I need use a interceptor to implement security

I understood that you were using JSF's own @ManagedBean and the initial answer only applies to that. If your bean is already managed by CDI's @Named, then just use CDI's own @Inject the usual way in the Filter.

@Inject
private SessionBean sessionBean;

In case of JSF @ManagedBean you should just add a if (sessionBean != null) check. It's irrelevant whether the filter is invoked before JSF servlet or not. Once the session bean has been created by JSF, it won't be null in the filter.



来源:https://stackoverflow.com/questions/10985528/how-could-i-read-a-jsf-session-bean-from-a-filter

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