Expand my Community achievements bar.

SOLVED

how to invoke a java class (OSGI Bundle) before invoking page

Avatar

Level 4

Hi All,

I have to implement, user authentication/authorization logic (logic in java) for my home page, before hitting. Like Spring controller handles request. How can I create, a controller for website.

Please suggest me how to create a controller like (spring controller which handles request and response) in AEM.

Thanks & Regards

Krishh

1 Accepted Solution

Avatar

Correct answer by
Level 8

You can use a filter for functionality like this.

package com.project.filters; import com.day.cq.wcm.api.WCMMode; import com.project.security.UserHelper; import org.apache.felix.scr.annotations.Reference; import org.apache.felix.scr.annotations.sling.SlingFilter; import org.apache.felix.scr.annotations.sling.SlingFilterScope; import org.apache.jackrabbit.api.security.user.User; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.commons.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.jcr.Session; import javax.jcr.ValueFactory; import javax.servlet.*; import java.io.IOException; import java.util.Map; @SlingFilter(order = -700, scope = SlingFilterScope.REQUEST) public class AuthorizedFilter implements Filter { private final Logger logger = LoggerFactory.getLogger(AuthorizedFilter.class); @Override public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain filterChain) throws IOException, ServletException { final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request; final SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) response; if (WCMMode.fromRequest(request) == WCMMode.DISABLED) { if (slingRequest.getRequestPathInfo().getResourcePath().contains("/content/project")) { String extension = slingRequest.getRequestPathInfo().getExtension(); if (extension != null && !extension.isEmpty() && extension.equals("html")) { if (UserHelper.isLoggedIn(slingRequest)) { slingResponse.sendRedirect("/content/project/page.html"); } } } } filterChain.doFilter(request, response); } @Override public void init(FilterConfig filterConfig) {} @Override public void destroy() {} }
package com.project.security; import org.apache.jackrabbit.api.security.user.User; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.commons.json.JSONArray; import org.apache.sling.commons.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.HashMap; import java.util.Map; public class UserHelper { private static final Logger logger = LoggerFactory.getLogger(UserHelper.class); public static Boolean isLoggedIn(SlingHttpServletRequest request) { User user = request.getResourceResolver().adaptTo(User.class); try { if (user == null || user.getID().equals("anonymous")) { return false; } return true; } catch(Exception e) { return false; } } }

View solution in original post

2 Replies

Avatar

Correct answer by
Level 8

You can use a filter for functionality like this.

package com.project.filters; import com.day.cq.wcm.api.WCMMode; import com.project.security.UserHelper; import org.apache.felix.scr.annotations.Reference; import org.apache.felix.scr.annotations.sling.SlingFilter; import org.apache.felix.scr.annotations.sling.SlingFilterScope; import org.apache.jackrabbit.api.security.user.User; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.commons.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.jcr.Session; import javax.jcr.ValueFactory; import javax.servlet.*; import java.io.IOException; import java.util.Map; @SlingFilter(order = -700, scope = SlingFilterScope.REQUEST) public class AuthorizedFilter implements Filter { private final Logger logger = LoggerFactory.getLogger(AuthorizedFilter.class); @Override public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain filterChain) throws IOException, ServletException { final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request; final SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) response; if (WCMMode.fromRequest(request) == WCMMode.DISABLED) { if (slingRequest.getRequestPathInfo().getResourcePath().contains("/content/project")) { String extension = slingRequest.getRequestPathInfo().getExtension(); if (extension != null && !extension.isEmpty() && extension.equals("html")) { if (UserHelper.isLoggedIn(slingRequest)) { slingResponse.sendRedirect("/content/project/page.html"); } } } } filterChain.doFilter(request, response); } @Override public void init(FilterConfig filterConfig) {} @Override public void destroy() {} }
package com.project.security; import org.apache.jackrabbit.api.security.user.User; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.commons.json.JSONArray; import org.apache.sling.commons.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.HashMap; import java.util.Map; public class UserHelper { private static final Logger logger = LoggerFactory.getLogger(UserHelper.class); public static Boolean isLoggedIn(SlingHttpServletRequest request) { User user = request.getResourceResolver().adaptTo(User.class); try { if (user == null || user.getID().equals("anonymous")) { return false; } return true; } catch(Exception e) { return false; } } }

Avatar

Level 9

Hi Krish,

Your problem is very abstract in a way. However, I think, you are looking for a preProcessor kind of service which gets executed before the final response.

Going with my assumptions, A possible solution could be a Sling Filter. These filters are like steps in a workflow. You can create authorization & authentication filter and this filter will get executed before hitting the home page.

Sample Code for filter

https://github.com/Adobe-Consulting-Services/acs-aem-samples/blob/master/bundle/src/main/java/com/ad...

---Jitendra