내가 두 페이지를 가지고있다 톰캣 7필터가 호출되지
를 사용하여 로그인 시스템과 간단한 JSF 웹 응용 프로그램 코드를하려고 해요 : index.xhtml을과 /restricted/welcome.xhtml.
아래의 페이지 "/ 제한/*"사용자가 로그인 만 접근 할 수 있어야합니다. welcome.xhtml에 직접 서핑
는welcome.xhtml이를 우회에 index.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";
}
}
범위 사용자? 쿠키를 삭제하고 액세스를 다시 시도하십시오 –
안녕 Jigar Joshi. 사용자 bean의 세션 범위가 지정됩니다. 삭제 된 쿠키 및 새 브라우저로 테스트되었습니다. – BlackEye
당신은 바로'welcome.xhtml'에 GET 요청을하고 있습니다, 맞습니까? –