Reputation:
I have a project with this structure (I can't change it):
Web Pages
META-INF
WEB-INF
assets (javascript, css and images)
includes (top.xhtml, bottom.xhtml, etc)
templates (master.xhtml)
views
fornecedor
-home.xhtml
-user.xhtml
-login.xhtml
franqueador
-home.xhtml
-user.xhtml
-login.xhtml
O have a login.xhtml
for each folder for a reason, I can't change it, it was passed by the project manager.
This is my SessionFilter:
@WebFilter("/views/*")
public class SessionFilter implements Filter {
@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);
LoginController loginController = (LoginController) ((boolean) (session != null) ? session.getAttribute("loginController") : null);
if (!request.getRequestURI().endsWith("/fornecedor/index.xhtml")) {
if (loginController == null || !loginController.getIsLoggedIn()) {
response.sendRedirect(request.getContextPath()
+ "/views/index.xhtml");
} else {
chain.doFilter(req, res);
}
}
}
}
And it doesn't work, returns a blank page with no html code and when I change the .xhtml
to .html
, it gives me a redirect loop.
I need to avoid all my login.xhtml
pages and the index.xhtml
on views
folder. I have debugged the if
on my filter but it always returns false
.
EDIT
Following BalusC answer I came to this:
if (!request.getRequestURI().endsWith("/views/index.html")
&& !request.getRequestURI().endsWith("/views/fornecedor/login.html")
&& !request.getRequestURI().endsWith("/views/franqueado/login.html")
&& (loginController == null || !loginController.getIsLoggedIn())) {
response.sendRedirect(request.getContextPath() + "/views/index.html");
} else {
chain.doFilter(req, res);
}
It is working but there is another problem, if I have 10 folder, I'll need to add them on this if
statement...I need to make it automatic.
Upvotes: 2
Views: 1203
Reputation: 1108642
Your first if
condition has no else
. That explains the blank page.
You need to evaluate the URL and logged-in conditions in a single if
instead.
if (!request.getRequestURI().endsWith("/fornecedor/index.xhtml") && (loginController == null || !loginController.getIsLoggedIn()) {
response.sendRedirect(request.getContextPath() + "/views/index.xhtml");
} else {
chain.doFilter(req, res);
}
As to the redirect loop, that's because your FacesServlet
is mapped on an URL pattern of *.html
instead of *.xhtml
for some unclear reason. So the request URL does never match. Fix the URLs in the filter accordingly. To generalize it for all login.html
and index.html
requests, just do as follows:
if (!request.getRequestURI().endsWith("/index.html")
&& !request.getRequestURI().endsWith("/login.html")
&& (loginController == null || !loginController.getIsLoggedIn())
{
response.sendRedirect(request.getContextPath() + "/views/index.html");
} else {
chain.doFilter(req, res);
}
Upvotes: 2