如何从过滤器内部获取SessionScoped CDI bean? [英] How do I get a SessionScoped CDI bean from inside a Filter?
问题描述
此问题与编写一个上一个问题有关会话超时处理程序.
该线程的答案涉及从Servlet访问各种会话范围的托管Bean.建议(如此处所示)是在过滤器:
The answer in that thread involved accessing various session-scoped managed beans from the servlet. The recommendation (as seen here) is to do this in the filter:
HttpSession session = request.getSession(false);
User user = (session != null) ? (User) session.getAttribute("user") : null;
大概这将获取类 User 的会话bean.问题是这不起作用.
Presumably this fetches a session bean of class User. The problem is this doesn't work.
出问题的是,会话属性中存在bean,但是它们由Weld设施包装.我编写了 doFilter()方法,如下所示:
What goes wrong are that the beans are there in the session attributes, but they are wrapped by Weld facilities. 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 dumps out the session attributes, I typically 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
属性2似乎代表了我想要的bean.不用说,对 session.getAttribute("login")的调用是行不通的.
Attribute #2 seems to represent the bean that I want. Needless to say a call to session.getAttribute("login") doesn't work.
谁能说出如何访问底层托管bean?我宁愿以一种与Weld无关的方式来做到这一点,但这可能是不可能的.
Can anyone say how to access the underlying managed bean? I would prefer to do it in a way that was not tied to Weld, but that may not be possible.
推荐答案
此方法仅适用于会话范围内的JSF @ManagedBean
,不适用于CDI @Named
bean.
This approach works for session scoped JSF @ManagedBean
only, not for CDI @Named
bean.
您需要 @Inject
过滤器的属性.
You need to @Inject
it as a property of the filter.
@Inject
private User user;
这篇关于如何从过滤器内部获取SessionScoped CDI bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!