Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Servlets 从servlet过滤器重定向到jsf将返回未呈现为html的实际jsf代码_Servlets_Jsf 2_Login_Glassfish 3_Servlet Filters - Fatal编程技术网

Servlets 从servlet过滤器重定向到jsf将返回未呈现为html的实际jsf代码

Servlets 从servlet过滤器重定向到jsf将返回未呈现为html的实际jsf代码,servlets,jsf-2,login,glassfish-3,servlet-filters,Servlets,Jsf 2,Login,Glassfish 3,Servlet Filters,下面是我的代码 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package racms; import java.io.IOException; import javax.faces.application.NavigationHandler; import javax.faces.context.FacesContext; imp

下面是我的代码

    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package racms;

import java.io.IOException;
import javax.faces.application.NavigationHandler;
import javax.faces.context.FacesContext;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@WebFilter("/faces/*")
public class AuthenticationFilter implements Filter {

    @Override
    public void init(FilterConfig config) throws ServletException {
        // If you have any <init-param> in web.xml, then you could get them
        // here by config.getInitParameter("name") and assign it as field.
    }

    @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);

        String pageRequested = request.getRequestURI().toString(); 
        //try{
        //FacesContext fctx = FacesContext.getCurrentInstance();
        //NavigationHandler myNav = fctx.getApplication().getNavigationHandler();



         if(session==null){       
               session = request.getSession(true); // will create a new session     
               response.sendRedirect("Login.xhtml");       
               //myNav.handleNavigation(fctx, null, "Login");
         }else if(session==null && pageRequested.contains("Login.xhtml")){       
                //  session.getAttribute("user");     
                chain.doFilter(request, response); // continue filtering       
         }else if((session.getAttribute("user")== null) && (!pageRequested.contains("Login.xhtml"))){          
             response.sendRedirect("Login.xhtml");
             //myNav.handleNavigation(fctx, null, "Login");
         }else {
             chain.doFilter(request, response);
         }
        //}catch(Exception e){
        //    System.out.println("Error :"+ e);
        //}
        /*if ((((HttpServletRequest) req).getSession().getAttribute("user") == null)) {
            FacesContext fctx = FacesContext.getCurrentInstance();
            NavigationHandler myNav = fctx.getApplication().getNavigationHandler();
            myNav.handleNavigation(fctx, null, "Login");
            //response.sendRedirect(request.getContextPath() + "/Login.xhtml"); // No logged-in user found, so redirect to login page.
        } else {
            chain.doFilter(req, res); // Logged-in user found, so just continue request.
        }*/
    }

    @Override
    public void destroy() {
        // If you have assigned any expensive resources as field of
        // this Filter class, then you could clean/close them here.
    }

}
/*
*要更改此模板,请选择工具|模板
*然后在编辑器中打开模板。
*/
包装racms;
导入java.io.IOException;
导入javax.faces.application.NavigationHandler;
导入javax.faces.context.FacesContext;
导入javax.servlet.*;
导入javax.servlet.annotation.WebFilter;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpServletResponse;
导入javax.servlet.http.HttpSession;
@WebFilter(“/faces/*”)
公共类AuthenticationFilter实现筛选器{
@凌驾
public void init(FilterConfig config)抛出ServletException{
//如果您在web.xml中有任何文档,那么您可以获取它们
//这里使用config.getInitParameter(“name”)并将其指定为字段。
}
@凌驾
public void doFilter(ServletRequest-req、ServletResponse-res、FilterChain-chain)抛出IOException、ServletException{
HttpServletRequest请求=(HttpServletRequest)请求;
HttpServletResponse=(HttpServletResponse)res;
HttpSession session=request.getSession(false);
字符串pagerequest=request.getRequestURI().toString();
//试一试{
//FacesContext fctx=FacesContext.getCurrentInstance();
//NavigationHandler myNav=fctx.getApplication().getNavigationHandler();
如果(会话==null){
session=request.getSession(true);//将创建一个新会话
sendRedirect(“Login.xhtml”);
//myNav.handleNavigation(fctx,空,“登录”);
}如果(session==null&&pageRequested.contains(“Login.xhtml”){
//session.getAttribute(“用户”);
chain.doFilter(请求、响应);//继续筛选
}如果((session.getAttribute(“user”)==null)和(!pageRequested.contains(“Login.xhtml”)){
sendRedirect(“Login.xhtml”);
//myNav.handleNavigation(fctx,空,“登录”);
}否则{
链式过滤器(请求、响应);
}
//}捕获(例外e){
//System.out.println(“错误:+e”);
//}
/*if((((HttpServletRequest)req.getSession().getAttribute(“用户”)==null)){
FacesContext fctx=FacesContext.getCurrentInstance();
NavigationHandler myNav=fctx.getApplication().getNavigationHandler();
myNav.handleNavigation(fctx,空,“登录”);
//response.sendRedirect(request.getContextPath()+“/Login.xhtml”);//找不到登录用户,因此重定向到登录页面。
}否则{
chain.doFilter(req,res);//找到登录用户,所以只需继续请求即可。
}*/
}
@凌驾
公共空间销毁(){
//如果您已将任何昂贵的资源分配为
//此筛选器类,然后您可以在此处清理/关闭它们。
}
}
如果使用FacesContext.getCurrentInstance(),则会发生java.lang.Nullpointer异常; 如果我使用response.sendRedirect(“Login.xhtml”);它显示一个空白页面,如果我查看源代码,我可以在jsf中看到Login.xhtml的源代码。它不会呈现为html

我想做的是:如果用户未登录并访问任何页面,则将其发送到Login.xhtml;如果用户位于Login.xhtml,则向其显示要登录的页面


请帮助..

重定向到与
FacesServlet
映射匹配的URL。显然,它被映射到
/faces/*
而不是
*.xhtml
。然后重定向到
faces/Login.xhtml

这里有一个重写,简化了逻辑:

@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);
    User user = (session != null) ? (User) session.getAttribute("user") : null;
    String loginURL = request.getContextPath() + "/faces/Login.xhtml"; 

    if (user == null && !request.getRequestURI().equals(loginURL)) {       
        response.sendRedirect(loginURL);
    } else {
        chain.doFilter(request, response);
    }
}

您是否能够在没有筛选器的情况下呈现登录页面?我首先要确保在没有过滤器的情况下login.xhtml呈现正确。如果没有筛选器,login.xhtml也无法呈现,那么,最有可能的是faces-config.xml中的url映射出现了问题。感谢BalusC,它成功了,请您帮助我理解;“/faces/*”和“*.xhtml”之间有什么区别?这是应该调用
FacesServlet
的URL模式。如果它是
/faces/*
,则仅在匹配
/faces/*
的URL上调用它。如果它是
*.xhtml
,那么它只能在匹配
*.xhtml
的URL上调用。使用
*.xhtml
不会产生不必要的额外路径,也不需要到处摆弄这个虚拟路径。我觉得这很有道理吧?