gpt4 book ai didi

java - 执行 JSF 导航时,不会调用映射到前向调度程序的过滤器

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:13:01 24 4
gpt4 key购买 nike

我正在尝试使用 Tomcat 7 编写一个带有登录系统的简单 JSF Web 应用程序。

我有两个页面:index.xhtml 和/restricted/welcome.xhtml。

“/restricted/*”下的页面仅供登录用户访问。

直接浏览 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";
}
}

最佳答案

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

改为发送重定向:

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

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

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

如果你真的坚持使用 JSF 调用 FORWARD 调度程序,你总是可以使用 ExternalContext#dispatch()方法,但这不是推荐的方法。

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

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

关于java - 执行 JSF 导航时,不会调用映射到前向调度程序的过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11187934/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com