好吧,在我的老问题之后:
和
我正在编写过滤器,但有时(甚至不是在同一点)应用程序NulPointerException
在此行返回:
chain.doFilter(req, res);
我不明白为什么......我的过滤器的完整代码是:
@WebFilter(filterName = "RolesFilter", urlPatterns = {"/*"},
initParams = {
@WebInitParam(name = "param", value = "value")})
public class RolesFilter implements Filter {
@Override
public void init(FilterConfig config) throws ServletException {
// NOOP.
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
HttpSession session = request.getSession(false);
UserBean user = (session.getAttribute("userBean") != null) ? (UserBean) session.getAttribute("userBean") : null;
String uri=request.getRequestURI();
if(!uri.endsWith("index.xhtml") && !uri.endsWith(".css") && !uri.endsWith(".js") && !uri.endsWith("template.xhtml"))
{
if (user == null || user.getCognome() == null)
response.sendRedirect("/cdg/faces/index.xhtml");
else {
String app=uri.substring(0,uri.lastIndexOf("/"));
app=app.substring(app.lastIndexOf("/")+1,app.length());
HashMap> privilegi= (HashMap)user.getPrivilegi();
Long idAppl=((AbstractController)session.getAttribute(app+"Controller")).getIdApplicazione();
Short[] privPoss=((AbstractController)session.getAttribute(app+"Controller")).getPrivilegiPossibili();
//--- if privPoss has only 3 value the application is a classic application with 2 roles so I can use a generic check, if the number is different I must do a specific check.
if(privilegi.get(idAppl)!=null && privPoss.length==3)
{
if((privilegi.get(idAppl)).get(privPoss[0])==null && (uri.endsWith("Edit.xhtml") || uri.endsWith("Create.xhtml")))
response.sendRedirect("/cdg/faces/index.xhtml");
} else {
if(idAppl==35)
{
ProgettiController pc=(session.getAttribute("progettiController")!=null) ? (ProgettiController)session.getAttribute("progettiController") : null;
if(privilegi.get(idAppl).get(privPoss[1])==null && uri.endsWith("progetti/Create.xhtml"))
response.sendRedirect("/cdg/faces/index.xhtml");
else if(uri.endsWith("progetti/Edit.xhtml")
&& (!privilegi.get(idAppl).get(privPoss[5]).equals("0") && !UserBean.isInString(pc.getSelected().getIdAzienda().getId()+"",privilegi.get(idAppl).get(privPoss[5]),"#")))
response.sendRedirect("/cdg/faces/index.xhtml");
else if((uri.endsWith("attivita/Create.xhtml") || uri.endsWith("attivita/Create.xhtml"))
&&(!pc.getSelected().getIdResponsabile().equals(user.getUtente())) )
response.sendRedirect"/cdg/faces/index.xhtml");
else if(uri.endsWith("attivita/Edit.xhtml")
&& (!privilegi.get(idAppl).get(privPoss[2]).equals("0") && !UserBean.isInString(pc.getSelected().getIdAzienda().getId()+"",privilegi.get(idAppl).get(privPoss[2]),"#")))
response.sendRedirect("/cdg/faces/index.xhtml");
else if((uri.endsWith("progettiQualifica/Create.xhtml") || uri.endsWith("progettiQualifica/Create.xhtml"))
&& (!pc.getSelected().getIdResponsabile().equals(user.getUtente())))
response.sendRedirect("/cdg/faces/index.xhtml");
else if((uri.endsWith("progettiSchede/Create.xhtml") || uri.endsWith("attivita/Create.xhtml"))
&& (!pc.getSelected().getIdResponsabile().equals(user.getUtente())))
response.sendRedirect("/cdg/faces/index.xhtml");
}
else if(idAppl==2)
{
RisorseController rc=(session.getAttribute("risorseController")!=null) ? (RisorseController)session.getAttribute("risorseController") : null;
if(privilegi.get(idAppl).get(privPoss[0])==null && (uri.endsWith("Create.xhtml") || uri.endsWith("Edit.xhtml")))
response.sendRedirect("/cdg/faces/index.xhtml");
else if(uri.endsWith("Edit.xhtml")
&& (!rc.getSelected().equals(user.getUtente())))
response.sendRedirect("/cdg/faces/index.xhtml");
}
}
}
}
chain.doFilter(req, res);
}
@Override
public void destroy() {
// NOOP.
}
}
问题可能出在哪里?
好吧,在这个答案之前,我从我的代码中删除了一行,现在过滤器似乎工作正常,这行会导致问题吗?
System.out.println("The uri is:"+request.getRequestURI());
它是在 doFilter 方法中的第一个 if 之后...谢谢您的帮助!