Answer the question
In order to leave comments, you need to log in
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
}
}
Answer the question
In order to leave comments, you need to log in
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);
}
}
}
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 questionAsk a Question
731 491 924 answers to any question