How to get a SessionScoped CDI bean from inside a filter?

This question is related to the previous one in writing the session timeout handler .

The response in this thread included access to managed beans using a servlet. The recommendation (as seen here ) is to do this in a filter:

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

Presumably this selects a user class bean session. The problem is that this does not work.

It is not good that beans are in the session attributes, but they are wrapped in Weld. I wrote the doFilter () method as follows:

 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; String sp = req.getServletPath(); System.out.println("------------------------"); System.out.println("doFilter(): " + sp); if (!sp.startsWith("/javax")) { // eliminates many requests HttpSession session = req.getSession(); Enumeration<String> en = session.getAttributeNames(); int count = 0; while (en.hasMoreElements()) { String e = en.nextElement(); System.out.println("Attribute " + ++count + ": " + e); } } chain.doFilter(request, response); } 

When this unloads the session attributes, I usually get something like this:

 INFO: ------------------------ INFO: doFilter(): /Display.xhtml INFO: Attribute 1: org.jboss.weld.context.http.HttpSessionContext#org.jboss.weld.bean-WEB-INF/lib/myfaces-extcdi-bundle-jsf20-1.0.1-ManagedBean-class org.apache.myfaces.extensions.cdi.jsf.impl.scope.conversation.EditableWindowContextManagerProxy INFO: Attribute 2: org.jboss.weld.context.http.HttpSessionContext#org.jboss.weld.bean-MyApp5-ManagedBean-class com.app.Login INFO: Attribute 3: org.jboss.weld.context.conversation.ConversationIdGenerator INFO: Attribute 4: com.sun.faces.renderkit.ServerSideStateHelper.LogicalViewMap INFO: Attribute 5: org.jboss.weld.context.ConversationContext.conversations INFO: Attribute 6: facelets.ui.DebugOutput INFO: Attribute 7: javax.faces.request.charset INFO: Attribute 8: org.apache.myfaces.extensions.cdi.core.api.scope.conversation.WindowContext:EXISTING_WINDOW_ID_LIST 

Attribute # 2 seems to represent the bean I want. Needless to say, calling session.getAttribute ("login") does not work.

Can anyone tell me how to access a basic managed bean? I would prefer to do it in a way that was not tied to Weld, but that might not be possible.

+8
jsf-2 servlet-filters cdi
source share
2 answers

This approach only works for JSF with @ManagedBean session @ManagedBean , not for CDI @Named bean.

You need @Inject as a filter property.

 @Inject private User user; 
+14
source share
 import org.jboss.weld.context.SerializableContextualInstanceImpl; HttpSession httpSession = (HttpSession) facesContext.getExternalContext().getSession(false); Enumeration<String> attribs = httpSession.getAttributeNames(); String attrib = null; while (attribs.hasMoreElements()) { attrib = attribs.nextElement(); Object obj = httpSession.getAttribute(attrib); if(obj instanceof SerializableContextualInstanceImpl){ SerializableContextualInstanceImpl impl = (SerializableContextualInstanceImpl)obj; //here: MyObject myObj= (MyObject)impl.getInstance(); } } 
+4
source share

All Articles