我之前问过我的问题,但我不清楚。所以我再次问我的问题,希望有人能理解我。我想为我的应用程序创建一个用户会话。因此,用户必须登录才能访问我的主页。为此,我创建了一个 Bean 和一个过滤器。我使用了JSF HTTP Session Login这个问题的答案来做到这一点。但userManager
过滤器中的始终为空。我不知道如何将已连接用户的值放入此行。
UserManager userManager = (UserManager) req.getSession().getAttribute("userManager");
这里是过滤器。
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
UserManager userManager = (UserManager) req.getSession().getAttribute("userManager");
String loginURL = req.getContextPath() + "/index.jsf";
if (userManager == null && !userManager.isLoggedIn() && !req.getRequestURI().equals(loginURL)) {
res.sendRedirect(loginURL);
} else {
res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
res.setHeader("Pragma", "no-cache"); // HTTP 1.0.
res.setDateHeader("Expires", 0); // Proxies.
chain.doFilter(request, response);
}
当我把这个条件放在 if 中时!userManager.isLoggedIn()
,我有一个标记,上面写着:
空指针访问:变量 userManager 在此位置只能为空,1 个快速修复可用,将 @SuppressWarnings 'null' 添加到 'doFilter()'
我试图添加它并执行,但出现错误java.lang.NullPointerException
。我是JSF的初学者。拜托我需要你的帮忙。
ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/jsf-formulaire].[Faces Servlet]] "Servlet.service()" pour la servlet Faces Servlet a généré une exception: java.lang.NullPointerException
at com.citech.pointage.fe.bean.filters.SessionFilter.doFilter(SessionFilter.java:61) [:]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:274) [:6.1.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242) [:6.1.0.Final]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [:6.1.0.Final]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [:6.1.0.Final]
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:181) [:6.1.0.Final]
at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.event(CatalinaContext.java:285) [:1.1.0.Final]
at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.invoke(CatalinaContext.java:261) [:1.1.0.Final]
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88) [:6.1.0.Final]
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100) [:6.1.0.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:159) [:6.1.0.Final]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [:6.1.0.Final]
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158) [:6.1.0.Final]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [:6.1.0.Final]
at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53) [:6.1.0.Final]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [:6.1.0.Final]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [:6.1.0.Final]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654) [:6.1.0.Final]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951) [:6.1.0.Final]
at java.lang.Thread.run(Unknown Source) [:1.6.0_03]