2012-06-25 38 views
3

我试图代码使用Tomcat 7过滤器映射着调度员在执行JSF导航,则不会调用

我有两个页面的登录系统一个简单的JSF Web应用程序:和的index.xhtml /restricted/welcome.xhtml。

下面的页面“/限制/ *”应是唯一的访问用户登录。

直接冲浪的welcome.xhtml导致执行我的过滤器,从的index.xhtml转发到welcome.xhtml绕过过滤。我无法想象为什么过滤器没有执行。

RestrictedAreaFilter.java:

@WebFilter(value = { "/restricted/*" }, dispatcherTypes = { DispatcherType.FORWARD, DispatcherType.REQUEST, DispatcherType.ASYNC, DispatcherType.ERROR, DispatcherType.INCLUDE }) 
public class RestrictedAreaFilter implements Filter { 

@Override 
public void destroy() { 

} 

@Override 
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 
    HttpServletRequest httpReq = (HttpServletRequest) request; 
    User user = (User) httpReq.getSession().getAttribute("user"); 

    if (user != null && user.isLoggedIn()) { 
     chain.doFilter(request, response); 
    } else { 
     httpReq.getRequestDispatcher("/access_denied.xhtml").forward(request, response); 
    } 
} 

@Override 
public void init(FilterConfig arg0) throws ServletException { 

} 

} 
<!DOCTYPE html 
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
xmlns:f="http://java.sun.com/jsf/core" 
xmlns:h="http://java.sun.com/jsf/html"> 
<head> 
<title>Login</title> 
</head> 
<body> 
<h:form id="form"> 
    <h:panelGrid id="grid" columns="2"> 
     <h:outputLabel value="Benutzername" for="username" /> 
     <h:inputText id="username" value="#{login.username}" /> 
     <h:outputLabel value="Passwort:" for="password" /> 
     <h:inputSecret id="password" value="#{login.password}"> 
      <f:validateLength minimum="4" maximum="16" /> 
     </h:inputSecret> 
     <h:message style="color: red" for="password" /> 

    </h:panelGrid> 
    <h:commandButton id="login" value="Login" action="#{login.proceed}" /> 
</h:form> 
</body> 
</html> 
@ManagedBean(name = "login") 
@RequestScoped 
public class LoginBean { 

@ManagedProperty(value = "#{user}") 
private User user; 

private String username; 
private String password; 

public void setUser(User user) { 
    this.user = user; 
} 

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 proceed() { 
    user.setLoggedIn(true); 

    return "restricted/welcome.xhtml"; 
} 
} 
+0

bean的作用域User?尝试删除cookies并重试访问 –

+0

嗨Jigar Joshi。 用户bean是会话作用域。用删除的Cookie和新的浏览器对其进行测试。 – BlackEye

+0

您正在向'welcome.xhtml'直接发出GET请求,对吧? –

回答

1

RequestDispatcher#forward()是被在webapp的代码某处调用的FORWARD调度时才会触发。标准的JSF导航处理程序不会那样做。它只是调用ViewHandler#createView()并将其设置为FacesContext#setViewRoot()的当前视图。

发送重定向来代替:

public String proceed() { 
    user.setLoggedIn(true); 

    return "restricted/welcome.xhtml?faces-redirect=true"; 
} 

这是顺便也推荐的做法。现在它是一个可加入书签的页面(URL更改现在反映在浏览器的地址栏中),并且刷新页面不会导致POST不必要地重新执行,并且按下后退按钮不会导致意外。

如果你真的坚持在调用使用JSF的FORWARD调度员,您可以随时使用ExternalContext#dispatch()方法,但这并不推荐的方式。

public void proceed() throws IOException { 
    user.setLoggedIn(true); 

    FacesContext.getCurrentInstance().getExternalContext().dispatch("restricted/welcome.xhtml") 
} 
+0

当OP使得直接得到没有做任何POST,OP能够查看该页面,看问题 –

+0

@Jigar:这是一个不同的问题。 – BalusC

+0

谢谢:) BalusC – BlackEye