ниже мой код;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package racms;
import java.io.IOException;
import javax.faces.application.NavigationHandler;
import javax.faces.context.FacesContext;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebFilter("/faces/*")
public class AuthenticationFilter implements Filter {
@Override
public void init(FilterConfig config) throws ServletException {
// If you have any <init-param> in web.xml, then you could get them
// here by config.getInitParameter("name") and assign it as field.
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
HttpSession session = request.getSession(false);
String pageRequested = request.getRequestURI().toString();
//try{
//FacesContext fctx = FacesContext.getCurrentInstance();
//NavigationHandler myNav = fctx.getApplication().getNavigationHandler();
if(session==null){
session = request.getSession(true); // will create a new session
response.sendRedirect("Login.xhtml");
//myNav.handleNavigation(fctx, null, "Login");
}else if(session==null && pageRequested.contains("Login.xhtml")){
// session.getAttribute("user");
chain.doFilter(request, response); // continue filtering
}else if((session.getAttribute("user")== null) && (!pageRequested.contains("Login.xhtml"))){
response.sendRedirect("Login.xhtml");
//myNav.handleNavigation(fctx, null, "Login");
}else {
chain.doFilter(request, response);
}
//}catch(Exception e){
// System.out.println("Error :"+ e);
//}
/*if ((((HttpServletRequest) req).getSession().getAttribute("user") == null)) {
FacesContext fctx = FacesContext.getCurrentInstance();
NavigationHandler myNav = fctx.getApplication().getNavigationHandler();
myNav.handleNavigation(fctx, null, "Login");
//response.sendRedirect(request.getContextPath() + "/Login.xhtml"); // No logged-in user found, so redirect to login page.
} else {
chain.doFilter(req, res); // Logged-in user found, so just continue request.
}*/
}
@Override
public void destroy() {
// If you have assigned any expensive resources as field of
// this Filter class, then you could clean/close them here.
}
}
Если я использую FacesContext.getCurrentInstance(), возникает исключение java.lang.Nullpointer; Если я использую response.sendRedirect("Login.xhtml"); он показывает пустую страницу, если я просматриваю источник, я вижу источник Login.xhtml в jsf. он не отображается в html..
что я хочу сделать, это: если пользователь не вошел в систему и не обращается к какой-либо странице, отправьте его на Login.xhtml, если пользователь находится на Login.xhtml, то покажите ему страницу чтобы залогиниться.
пожалуйста, помогите..