У меня есть встроенные функции входа / выхода в моем приложении, но фильтр, вероятно, не работает, так как я могу видеть страницы после выхода из системы, когда я указываю их в адресной строке браузера. Вот мое действие для входа в систему: -
this.currentUser = new User(); // initiate currentUser
FacesContext facesContext = FacesContext.getCurrentInstance();
facesContext.getApplication().createValueBinding("#{" + Constants.VISIT_KEY_SCOPE +
Constants.VISIT_KEY + "}").setValue(facesContext, currentUser);
FacesUtils.putIntoSession(Constants.VISIT_KEY, currentUser);
Выход из системы: -
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession)facesContext.getExternalContext().getSession(false);
session.removeAttribute(Constants.VISIT_KEY_SCOPE + Constants.VISIT_KEY);
if (session != null)
{
session.invalidate();
}
Константы Класс: -
public class Constants
{
// Backing bean keys
public final static String VISIT_KEY_SCOPE = "sessionScope.";
public final static String VISIT_KEY = "currentUser";
// Model object keys
public final static String PROJECT_COORDINATOR_SCOPE = "applicationScope.";
public final static String ORIGINAL_VIEW_SCOPE = "sessionScope";
public final static String ORIGINAL_VIEW_KEY = "originalTreeId";
}
Web.xml: -
<filter>
<filter-name>AuthorizationFilter</filter-name>
<filter-class>org.AuthorizationFilter.AuthorizationFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AuthorizationFilter</filter-name>
<url-pattern>/faces/pages/*</url-pattern>
</filter-mapping>
и, наконец, фильтр авторизации выглядит следующим образом: -
public class AuthorizationFilter implements Filter
{
FilterConfig config = null;
ServletContext servletContext = null;
public AuthorizationFilter()
{
}
public void init(FilterConfig filterConfig) throws ServletException
{
config = filterConfig;
servletContext = config.getServletContext();
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException
{
HttpServletRequest httpRequest = (HttpServletRequest)request;
HttpServletResponse httpResponse = (HttpServletResponse)response;
HttpSession session = httpRequest.getSession();
User currentUser = (User)session.getAttribute("currentUser");
if (session == null || currentUser == null || currentUser.getUserName() == null)
{
session.setAttribute(Constants.ORIGINAL_VIEW_KEY, httpRequest.getPathInfo());
httpResponse.sendRedirect(httpRequest.getContextPath() + "/faces/pages
/login.jsp");
}
else
{
session.removeAttribute(Constants.ORIGINAL_VIEW_KEY);
chain.doFilter(request, response);
}
}
public void destroy()
{
}
}
Огромное спасибо за терпение и помощь.