Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

request contains authorization in coming when accessing to get servlet

Avatar

Level 7

http://localhost:8080/content/anything/us/en/jcr:content.txt

this servlet I am hiiting from post man without passing any credentials and it is giving me response . but this is not getting cached ...

 

if I want to cache it what needs to be done ?

 

I am also not doing any authorization check in dispatcher level .

 

note : when i am accessing any page normally then in logs i do not see "request contains authorization"

and it properly get cache or served from cache .

 

why only for servlet it shows "request contains authorization" and no cache happens?

 

 

I do not want to do this -->

/allowAuthorized "1"
1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@AdobeID24 

 

Option-1: Please change the resourceType in your Servlet to "sling/servlet/default". Now access this servlet as http://localhost:8080/content/anything/us/en.txt

 

The Servlet should be accessible for all resourcetypes and cacheable. 

 

Option-2: Rewrite as suggested by @Imran__Khan 


Aanchal Sikka

View solution in original post

13 Replies

Avatar

Level 7

I have seen that response . 

 

I have set up everything ok as per that discussion .

 

in my local I have allowed everything to cache as of now ..

 

/cache--> /rules -->

/0000 {
/glob "*"
/type "allow"
}

Avatar

Community Advisor

@AdobeID24 

 

Please validate the rules defined in /cache -> /rules section of your dispatcher 

https://experienceleague.adobe.com/docs/experience-manager-learn/ams/dispatcher/understanding-cache....

 

A rule like this instructs dispatcher not to cache html files under /libs

/0002 {
 /glob "/libs/*.html"
 /type "deny"
}

 

 

Also, enable the DEBUG logs on dispatcher to have better understanding of the request and how dispatcher is handling it. Set DISP_LOG_LEVEL to debug. Example:

Define DISP_LOG_LEVEL debug

https://techrevel.blog/2023/08/28/navigating-aem-dispatcher-setup-farm-file-resolution-and-variable-...


Aanchal Sikka

Avatar

Level 7

Hi @aanchal-sikka  ,

 

I have dispatcher logs in debug level only  , there only I am getting these two traces :

"request contains authorization" and "cache-action for [/content/something/us/en/jcr:content.txt]: NONE"

 

accessing servlet through postman no credentials required :

AdobeID24_0-1708665613304.png

 

 

if I make below allowAuthorized from 0 to 1 it will start caching servlet response and serve from cache the response . but I do not want to do.

/allowAuthorized "1"

 

my /cache --> /rules is very simple i just allowed everything to cache . 

 

/cache--> /rules -->

/0000 {
/glob "*"
/type "allow"
}

Avatar

Community Advisor

@AdobeID24 
First of all please correct you servlet path using below sample code as it should not have jcr:content as part of servlet path.
Correct  Path: http://localhost:8080/content/anything/us/en.txt or http://localhost:8080/content/anything/us/en.json 

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.ServletResolverConstants;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.service.component.annotations.Component;

@Component(
        immediate = true,
        service = Servlet.class,
        name = "test",
        property = {
        		ServletResolverConstants.SLING_SERVLET_METHODS + "=" + HttpConstants.METHOD_GET,
        		ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES + "=" + CommonConstants.RESOURCE_TYPE_CQ_PAGE,
                ServletResolverConstants.SLING_SERVLET_EXTENSIONS + "=txt"
               }
)
public class DetailsServlet extends SlingSafeMethodsServlet {
	private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {

    }
}

 

Avatar

Level 7

Hi @Imran__Khan ,

 

Thanks for your suggestion, anyhow its local  !!

anyhow this servlet is part of   maven generated  code with latest archetype , here  registered the servlet with resource type and I am hitting the servlet from page jcr content location where resourcetype exist just to hit the servlet .

 

I am not much worried about path  of servlet ...any idea why I am getting these traces 

"request contains authorization" and "cache-action for [/content/something/us/en/jcr:content.txt]: NONE"

 

in my dispatcher logs ..I want to cache the servlet response .

 

In my project as well path is like that only so I am good with the path things ..more curious on caching the servlet .

 

Avatar

Community Advisor

@AdobeID24 I hope your servlet is GET for sure on first place.

Verify the docroot path set to save cache content.Check below blog in detail which talks about to enable and disable cache for servlet. I also talks about selectors and various mechanism.
https://medium.com/@toimrank/enable-or-disable-dispatcher-cache-9ce071810420

Note: Sometime dispatcher creates issue in local and stop cache content. Try and restart the servlet. If didn't working try to get new one configure the same.

Avatar

Level 7

yeah servlet is GET only

Docroot and all set properly I believe because when i make "allowauthorization":1 from 0 it start caching secure content  .

 

Caching is happening on above case . 

 

 

Avatar

Community Advisor

@AdobeID24 I tested this in local and able to replicate this issue. I am able to cache all servlets apart from the one having jcr:content as part of servlet path.
We can not cache servlet having :(colon) as part of it.

Follow below step to resolve this issue and cache servlet:

Resolution 1: Use below rewrite rule will help you to resolve your issue and will be able to cache servlet

RewriteRule /content/anything/us/en/jcr:content.txt /content/anything/us/en/jcr:content_jcr_content.txt [R=301,L]
OR
Resolution 2:
Use below path to access servlet will allow you to cache:

/content/anything/us/en/jcr:content_jcr_content.txt

Imran__Khan_0-1708696602447.png

 

Avatar

Community Advisor

Hi @AdobeID24 
You might be trying in Author instance, thats why you are getting authorize headers in the request.



Arun Patidar

Avatar

Correct answer by
Community Advisor

@AdobeID24 

 

Option-1: Please change the resourceType in your Servlet to "sling/servlet/default". Now access this servlet as http://localhost:8080/content/anything/us/en.txt

 

The Servlet should be accessible for all resourcetypes and cacheable. 

 

Option-2: Rewrite as suggested by @Imran__Khan 


Aanchal Sikka

Avatar

Community Advisor

@aanchal-sikka Thanks, Both the options 1 & 2 suggested above very clearly with the screenshots, required code and POV

cc @AdobeID24

Avatar

Administrator

@AdobeID24  Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni