Я пытался создать безопасную страницу входа с помощью jsf, и я использовал эти фрагменты кода в качестве решения, найденные в этом вопрос. Моя проблема в том, что я могу получить доступ к /restricted/secret.xhtml без входа в систему, перенаправления нет, как будто фильтр не применяется, потому что, если я перейду непосредственно к /restricted/secret.xhtml, #{user.loggedIn } оценивается как false, и я все еще могу просматривать страницу. Вот мой код:
AuthFilter.java
public class AuthFilter implements Filter {
private FilterConfig config;
@Override
public void destroy() {
this.config = null;
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain ch) throws IOException, ServletException {
HttpSession s = ((HttpServletRequest) req).getSession();
if (s.getAttribute(UserBean.CREDENTIAL)==null)
{
((HttpServletResponse) resp).sendRedirect("/login.faces");
}else
{
ch.doFilter(req, resp);
}
}
@Override
public void init(FilterConfig config) throws ServletException {
this.config = config;
}
}
UserBean.java
@ManagedBean(name="user")
@SessionScoped
public class UserBean implements Serializable {
private String name;
private String password;
protected static final String CREDENTIAL = "ontherun";
private static final long serialVersionUID = 1L;
public String getName()
{
return this.name;
}
public void setName(String newName)
{
this.name = newName;
}
public String getPassword()
{
return this.password;
}
public void setPassword(String newPassword)
{
this.password = newPassword;
}
public boolean isLoggedIn()
{
return FacesContext.getCurrentInstance().getExternalContext()
.getSessionMap().get(CREDENTIAL) != null;
}
public String logout() {
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(CREDENTIAL);
return null;
}
public String login()
{
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(CREDENTIAL, this.name);
return "secret";
}
}
Вот мой login.xhtml; страница работает корректно, так что проблем с файлом шаблона нет.
IGNORED
#{msgs.window_title}
#{msgs.namePrompt}
#{msgs.passwordPrompt}
You are logged in : #{user.loggedIn}
Вот в чем секрет.xhtml, который должен быть ограничен:
IGNORED
#{msgs.window_title}
You are #{user.loggedIn}
А вот мои файлы конфигурации: web.xml
OnTheRun
Faces Servlet
javax.faces.webapp.FacesServlet
1
Faces Servlet
/faces/*
faces/index.xhtml
AuthFilter
on.run.AuthFilter
AuthFilter
/restricted/*
javax.faces.PROJECT_STAGE
Development
и Faces-config.xml
on.run.messages
msgs
/index.xhtml
login
/profile.xhtml
/login.xhtml
secret
/restricted/secret.xhtml
Моя структура каталогов выглядит следующим образом: dirStruct2