LoginFilter nie działa.

0

Witam. Mam filtr, który ma za zadanie sprawdzić sesję użytkownika. Jeśli wygasła ma nas przekierować na stronę home.xhtml:

Filtr:

public class FilterLogin implements Filter{

        FilterConfig fc;

        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            fc = filterConfig;
        }

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

            HttpServletRequest req = (HttpServletRequest) request;
            HttpServletResponse resp = (HttpServletResponse) response;
            HttpSession session = req.getSession(true);

            if (session.getAttribute("loginMB") == null) {
                String contextPath = ((HttpServletRequest)request).getContextPath();
                System.out.println(contextPath + "/home.xhtml");
                resp.sendRedirect(contextPath + "/home.xhtml");
            } else {
                chain.doFilter(request, response);
            }  
        }

        @Override
        public void destroy() {
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

    }

web.xml:

 <filter>
    <filter-name>filter</filter-name>
    <filter-class>pl.project.filter.FilterLogin</filter-class>
</filter>
<filter-mapping>
    <filter-name>filter</filter-name>
    <url-pattern>/protected/*</url-pattern>
</filter-mapping>

Niestety

resp.sendRedirect(contextPath + "/home.xhtml");

nie przekierowuje mnie na stronę w momencie wygaśnięcia. Kiedy próbuję po wygaśnięciu sesji nawigować po stornie, to nie ma żadnej rekacji. Cały czas zostaję na tej samej stronie i nie mogę nic zrobić, a powinienem zostać w takim wypadku zostać przekierowany na home.xhtml.

Na pewno wchodzę w ifa, ponieważ w warunku

(session.getAttribute("loginMB") == null)

dałem System.out.println(contextPath + "/home.xhtml");

 i wypisuje mi to w logach serwera, ale potem ten resp nie działa najwyraźniej: <code class="java"><code class="java">resp.sendRedirect(contextPath + "/home.xhtml");
0
response.setStatus(HttpServletResponse. SC_MOVED_TEMPORARILY);
response.setHeader("Location", "http://somewhere/");

SC_MOVED_TEMPORARILY zwraca HTTP 302

0

Dodałem to do filtra, ale efekt nadal jest taki sam.

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
         
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        HttpSession session = req.getSession(true);
        
        if (session.getAttribute("loginMB") == null) {
            String contextPath = ((HttpServletRequest)request).getContextPath();
            System.out.println("wwwww");
            resp.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
            resp.setHeader("Location", "/home.xhtml");
        } else {
            chain.doFilter(request, response);
        }  
    }
0

Edit: Do usunięcia problem rozwiązany.

1 użytkowników online, w tym zalogowanych: 0, gości: 1