May 3, 2020
Estimated Post Reading Time ~

Check Authentication in CQ

AuthCheckerServlet.java

import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
@SlingServlet(paths = "/bin/permissioncheck/html", generateComponent = true, generateService = true)
public class AuthCheckerServlet extends SlingSafeMethodsServlet {
    public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {       
        String uri = request.getParameter("uri");       
        Resource resource = request.getResourceResolver().resolve(uri);       
        if(ResourceUtil.isNonExistingResource(resource)) {           
            response.setStatus(SlingHttpServletResponse.SC_FORBIDDEN);           
            } else {           
            response.setStatus(SlingHttpServletResponse.SC_OK);           
        }       
    }   


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.