Filter not work for /crx/de/index.jsp
Hi,
I have written a filter to check whether user is login, if not, redirect it to the login page. I am referring to this filter code mentioned in this article http://aemfaq.blogspot.sg/2013/05/blocking-anonymous-access-to-crx-in-non.html
But when I tested it, the filter seems not work for the URL: http://localhost:4502/crx/de/index.jsp
I checked the log, the filter seems not go into the doFilter method. Here is my filter code:
import javax.servlet.*; import java.io.IOException; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Cookie; import javax.servlet.RequestDispatcher; import org.apache.felix.scr.annotations.Activate; import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.sling.SlingFilter; import org.apache.felix.scr.annotations.sling.SlingFilterScope; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @SlingFilter(generateComponent = false, generateService = true, order = -50001, scope = SlingFilterScope.REQUEST) @Component(immediate = true, metatype = false) public class CrxLoginFilter implements Filter { protected static final Logger log = LoggerFactory.getLogger(CrxLoginFilter.class); public void init(FilterConfig config) throws ServletException { log.info("Init with config [" + config + "]"); } @Activate protected void activate(final Map<String, Object> props) { log.info("***** activate *****"); } public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { log.info("inside doFilter "); if ( req instanceof HttpServletRequest && res instanceof HttpServletResponse ) { final HttpServletRequest request = (HttpServletRequest)req; final HttpServletResponse response = (HttpServletResponse)res; String pathInfo = request.getPathInfo() ; boolean crxdeAuthenticated = false; boolean crxAuthenticated = false; log.info("============ pathInfo " + pathInfo); if(pathInfo != null){ Cookie[] cookies = request.getCookies(); if(cookies!=null){ for (int i = 0; i < cookies.length; i++) { String name = cookies[i].getName(); String value = cookies[i].getValue(); if(name!=null && name.equals("login-workspace") && value!=null){ crxAuthenticated = true; } if(name!=null && name.equals("login-token") && value!=null){ crxdeAuthenticated = true; } } } log.info("============== ?? pathInfo " + pathInfo + ", crxAuthenticated " + crxAuthenticated); if(pathInfo.startsWith("/crx/explorer/crx_main_files/admin.css")){ //Do nothing log.info("======================== 1 ======================"); }else if ( !pathInfo.startsWith("/crx/explorer/login.jsp") && pathInfo.startsWith("/crx/explorer") && !crxAuthenticated ){ response.sendRedirect("/crx/explorer/login.jsp"); log.info("======================== 2 ======================"); return; }else if( ( pathInfo.startsWith("/crxde") || pathInfo.startsWith("/crx/de") ) && !crxdeAuthenticated ){ RequestDispatcher rd = request.getRequestDispatcher("/libs/granite/core/content/login.html"); log.info("======================== 3 ======================"); rd.forward(request, response); return; } } } chain.doFilter(req, res); } public void destroy() { log.info("Destroyed filter"); } }Please help to advice what is wrong with the filter. I am using the AEM 6.1 SP1.
Thanks in advance!