N
N
Nikolay Baranenko2016-07-06 15:32:14
JavaScript
Nikolay Baranenko, 2016-07-06 15:32:14

Why does the servlet - authentication filter on the username and password form cut off CSS, JS, pictures, etc.?

Hello.

I'm trying to finish the servlet - the authentication filter.
I must say right away that I used the idea with

It turned out somewhere like this

public class AuthenticationFilter implements Filter {

    private ServletContext context;

    public void init(FilterConfig fConfig) throws ServletException {
        this.context = fConfig.getServletContext();
        this.context.log("AuthenticationFilter initialized");
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;

        String uri = req.getRequestURI();
        this.context.log("Requested Resource::" + uri);

        HttpSession session = req.getSession(false);
        Object user_o = req.getSession().getAttribute("username");
        this.context.log("Фильтр аутентификации, пользователь::" + user_o);

        if (user_o == null && !(uri.endsWith("index.jsp") || uri.endsWith("LoginUser"))) {
            this.context.log("Неавторизованный запрос");
            res.sendRedirect("index.jsp");

        } else {
            // pass the request along the filter chain
            this.context.log("Авторизованный запрос, сессия:: " + session);
            chain.doFilter(request, response);
        }
        
    }

    public void destroy() {
        //close any resources here
    }
}


The problem is that when the index.jsp user validation form opens, neither CSS nor JS works on it, pictures are not displayed.
Login validation servlet - LoginUser
What could be the problem?

With the filter turned off, everything is displayed normally.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikolay Baranenko, 2016-07-07
@drno-reg

The problem was solved like this

@WebServlet(
        name = "AuthenticationFilter",
        description = "Аутентификационный фильтр",
        urlPatterns = "/AuthenticationFilter"
)
@WebFilter("*.jsp")
public class AuthenticationFilter implements Filter {

    private ServletContext context;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.context = filterConfig.getServletContext();
        this.context.log("AuthenticationFilter initialized");
    }

    @Override
    public void destroy() {
        //close any resources here
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;

        String uri = req.getRequestURI();
        this.context.log("Requested Resource::" + uri);

        HttpSession session = req.getSession(false);
        Object user_o = req.getSession().getAttribute("username");
        this.context.log("Фильтр аутентификации, пользователь::" + user_o);

        if (user_o == null && !(uri.endsWith("index.jsp") || uri.endsWith("LoginUser"))) {
            this.context.log("Неавторизованный запрос");
            res.sendRedirect("index.jsp");

        } else {
            // pass the request along the filter chain
            this.context.log("Авторизованный запрос, сессия:: " + session);
            chain.doFilter(request, response);
        }
        
    }

    }

A
Alexander Kosarev, 2016-07-07
@jaxtr

And on what way the filter is hung up? And what are the paths of static resources? Most likely, the paths of static resources fall under the action of a filter that redirects requests to index.jsp.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question