JSF筛选器在初始重定向后未重定向

JSF筛选器在初始重定向后未重定向,jsf,authentication,redirect,servlet-filters,Jsf,Authentication,Redirect,Servlet Filters,我正在尝试设置webfilter,需要一些帮助。我的过滤器在初次登录时工作正常,但当会话超时,我单击任何链接时,它会触发我的重定向语句,但浏览器中的网页永远不会被重定向。有人能帮助解决这个问题吗?非常感谢 滤器 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package src; import java.io.IOException; im

我正在尝试设置webfilter,需要一些帮助。我的过滤器在初次登录时工作正常,但当会话超时,我单击任何链接时,它会触发我的重定向语句,但浏览器中的网页永远不会被重定向。有人能帮助解决这个问题吗?非常感谢

滤器

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

import java.io.IOException;
import javax.faces.application.NavigationHandler;
import javax.faces.context.FacesContext;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Bernard
 */
@WebFilter(filterName = "LoginFilter", urlPatterns = {"/*"})
public class LoginFilter implements Filter {

    //FilterConfig fc;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        //fc = filterConfig;
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        HttpSession session = req.getSession(true);
        String pageRequested = req.getRequestURL().toString();
        Boolean authenticated = (Boolean) session.getAttribute("authenticated");

        if (authenticated == null) {
            authenticated = false;
        }
        if (!authenticated && !pageRequested.contains("login")) {     
            res.setStatus(301);
            res.sendRedirect(req.getContextPath() + "/login/login.xhtml");                        
        } else {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {
        //fc = null;
    }
}
Faces-config.xml

<?xml version='1.0' encoding='UTF-8'?>

<!-- =========== FULL CONFIGURATION FILE ================================== -->

<faces-config version="2.1"
              xmlns="http://java.sun.com/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_1.xsd">

    <navigation-rule>
        <from-view-id>/*</from-view-id>

        <navigation-case>
            <from-outcome>success</from-outcome>
            <to-view-id>/index.xhtml</to-view-id>
            <redirect/>
        </navigation-case>

        <navigation-case>
            <from-outcome>failure</from-outcome>
            <to-view-id>/login/login.xhtml</to-view-id>
            <redirect/>
        </navigation-case>
    </navigation-rule>
</faces-config>

/*
成功
/index.xhtml
失败
/login/login.xhtml
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <servlet>
        <servlet-name>FacesServlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>FacesServlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>/index.xhtml</welcome-file>
    </welcome-file-list>
    <filter>
        <filter-name>restrict</filter-name>
        <filter-class>src.LoginFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>restrict</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>  
</web-app>

javax.faces.PROJECT_阶段
发展
小脸蛋
javax.faces.webapp.FacesServlet
1.
小脸蛋
*.xhtml
30
/index.xhtml
限制
src.LoginFilter
限制
/*
验证Bean

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

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Bernard
 */
@ManagedBean
@SessionScoped
public class Authenticator {

    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String authenticateUser(ServletRequest request) {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpSession session = req.getSession(true);
        session.setMaxInactiveInterval(30);
        Boolean authenticated = (Boolean) session.getAttribute("authenticated");

        Database pgDatabase = new Database();
        Admin foundAdmin = null;
        try {
            foundAdmin = (Admin) pgDatabase.findAdminByUsername(username);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Authenticator.class.getName()).log(Level.SEVERE, null, ex);
        }

        Admin currentAdmin = new Admin();
        currentAdmin.userName = username;
        currentAdmin.password = this.hashPassword((password));
        if (authenticated != null && authenticated != true) {
            if (foundAdmin != null) {
                if (currentAdmin.equals(foundAdmin)) {
                    authenticated = true;
                    session.setAttribute("authenticated", true);
                    return "success";
                } else {
                    authenticated = false;
                    session.setAttribute("authenticated", false);
                    return "failure";
                }
            } else {
                authenticated = false;
                session.setAttribute("authenticated", false);
                return "failure";
            }
        } else {
            session.setAttribute("authenticated", true);
            authenticated = true;
            return "success";
        }
    }

    public String logOut() {
        FacesContext ctx = FacesContext.getCurrentInstance();
        ExternalContext extCtx = ctx.getExternalContext();
        Map<String, Object> sessionMap = extCtx.getSessionMap();
        sessionMap.put("authenticated", false);
        return "failure";
    }

    public String hashPassword(String passwordToHash) {
        String hashword = null;
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(password.getBytes());
            BigInteger hash = new BigInteger(1, md5.digest());
            hashword = hash.toString(16);
        } catch (NoSuchAlgorithmException nsae) {
        }
        return hashword;
    }
}
/*
*要更改此模板,请选择工具|模板
*然后在编辑器中打开模板。
*/
包src;
导入java.math.biginger;
导入java.security.MessageDigest;
导入java.security.NoSuchAlgorithmException;
导入java.util.Map;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入javax.faces.bean.ManagedBean;
导入javax.faces.bean.SessionScoped;
导入javax.faces.context.ExternalContext;
导入javax.faces.context.FacesContext;
导入javax.servlet.ServletRequest;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpSession;
/**
*
*@作者伯纳德
*/
@ManagedBean
@会议范围
公共类验证器{
私有字符串用户名;
私有字符串密码;
公共字符串getUsername(){
返回用户名;
}
public void setUsername(字符串用户名){
this.username=用户名;
}
公共字符串getPassword(){
返回密码;
}
public void setPassword(字符串密码){
this.password=密码;
}
公共字符串验证器(ServletRequest){
HttpServletRequest req=(HttpServletRequest)请求;
HttpSession session=req.getSession(true);
session.setMaxInactivativeInterval(30);
Boolean authenticated=(Boolean)session.getAttribute(“authenticated”);
Database pgDatabase=新数据库();
Admin foundAdmin=null;
试一试{
foundAdmin=(Admin)pgDatabase.findAdminByUsername(username);
}捕获(ClassNotFoundException ex){
Logger.getLogger(Authenticator.class.getName()).log(Level.SEVERE,null,ex);
}
Admin currentAdmin=new Admin();
currentAdmin.userName=用户名;
currentAdmin.password=this.hashPassword((密码));
if(authenticated!=null&&authenticated!=true){
if(foundAdmin!=null){
如果(当前管理等于(foundAdmin)){
已验证=真;
session.setAttribute(“已验证”,true);
返回“成功”;
}否则{
认证=假;
session.setAttribute(“已验证”,false);
返回“失败”;
}
}否则{
认证=假;
session.setAttribute(“已验证”,false);
返回“失败”;
}
}否则{
session.setAttribute(“已验证”,true);
已验证=真;
返回“成功”;
}
}
公共字符串注销(){
FacesContext ctx=FacesContext.getCurrentInstance();
ExternalContext extCtx=ctx.getExternalContext();
Map sessionMap=extCtx.getSessionMap();
sessionMap.put(“已验证”,false);
返回“失败”;
}
公共字符串hashPassword(字符串passwordToHash){
字符串hashword=null;
试一试{
MessageDigest md5=MessageDigest.getInstance(“md5”);
md5.update(password.getBytes());
BigInteger哈希=新的BigInteger(1,md5.digest());
hashword=hash.toString(16);
}捕获(无算法异常nsae){
}
返回hashword;
}
}
您的过滤器看起来很好(除了非常弱的
url.contains(“登录”)
测试以及在2种方式下错误地尝试将响应状态设置为301,以及检查登录用户的方式有点糟糕之外)

我认为您的具体问题是由ajax链接而不是普通链接执行navigaiton造成的。您不能以这种方式在ajax响应上发送重定向。JSF ajax引擎和webbrowser都不遵循JSF ajax响应上的302重定向。客户端最终得到一个完全被忽略的ajax响应

相反,您应该发送一个特殊的XML响应,指示JSF ajax引擎发送重定向。确切地说,在JSF上下文中发送的XML响应是在ajax请求期间使用的

<?xml version="1.0" encoding="UTF-8"?>
<partial-response>
    <redirect url="/contextpath/login/login.xhtml"></redirect>
</partial-response>
因此,总而言之,您的
doFilter()
现在应该是这样的:

String loginURL = req.getContextPath() + "/login/login.xhtml";

if (!authenticated && !req.getRequestURI().equals(loginURL)) {
    if ("partial/ajax".equals(request.getHeader("Faces-Request"))) {
        res.setContentType("text/xml");
        res.getWriter()
            .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
            .printf("<partial-response><redirect url=\"%s\"></redirect></partial-response>", loginURL);
    } else {
        res.sendRedirect(loginURL);
    }
} else {
    chain.doFilter(request, response);
}
String loginURL=req.getContextPath()+“/login/login.xhtml”;
如果(!authenticated&&!req.getRequestURI().equals(loginURL)){
if(“partial/ajax.equals”(request.getHeader(“Faces request”)){
res.setContentType(“text/xml”);
res.getWriter()
.附加(“”)
.printf(“,loginURL”);
}否则{
res.sendRedirect(loginURL);
}
}否则{
链式过滤器(请求、响应);
}

您的日志文件中有ViewExpiredException吗?如果没有人回答,我会在几个小时内详细阅读它。我也有同样的问题。下面的解决方案工作正常。巴卢斯克,非常感谢。这似乎是完美的工作。这是我第一次尝试Java应用程序,也是第一次尝试整体编码,所以我不太热衷于最佳实践。如果你能
String loginURL = req.getContextPath() + "/login/login.xhtml";

if (!authenticated && !req.getRequestURI().equals(loginURL)) {
    if ("partial/ajax".equals(request.getHeader("Faces-Request"))) {
        res.setContentType("text/xml");
        res.getWriter()
            .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
            .printf("<partial-response><redirect url=\"%s\"></redirect></partial-response>", loginURL);
    } else {
        res.sendRedirect(loginURL);
    }
} else {
    chain.doFilter(request, response);
}