Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

how to get response from Author instance through a standalone code

Avatar

Level 1

I am trying to hit URL in cq Author instance from my standalone code. 
URL looks like - http://<somehost>:<someport>//libs/dam/gui/content/reports/export.json
Below is the code - 

         URL url = new URL(newPath);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setRequestMethod("GET");
            connection.setReadTimeout(15 * 10000);
            connection.connect();
            reader = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            stringBuilder = new StringBuilder();

But I got error 401 which is expected as I'm not passing any authentication information. hence Sling says that "getAnonymousResolver: Anonymous access not allowed by configuration - requesting credentials"

Please advice how to proceed on this.

1 Accepted Solution

Avatar

Correct answer by
Level 6

Add the below code - 

  String authString = "admin" + ":" + "admin";
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setRequestProperty  ("Authorization", "Basic " + authStringEnc );

View solution in original post

6 Replies

Avatar

Correct answer by
Level 6

Add the below code - 

  String authString = "admin" + ":" + "admin";
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setRequestProperty  ("Authorization", "Basic " + authStringEnc );

Avatar

Level 9

As per the slingAuthentication logic it doesn't allow aonymous login, try setting username and password.

https://svn.apache.org/repos/asf/sling/tags/org.apache.sling.auth.core-1.1.0/src/main/java/org/apach... // If we get here, anonymous access is not allowed: redirect // to the login servlet log.info("getAnonymousResolver: Anonymous access not allowed by configuration - requesting credentials"); doLogin(request, response);

Avatar

Level 10

You can also code a Sling Servlet so that authentication is not required by using a Property annotation.

@Property(name = "sling.auth.requirements", value = "-/bin/upfile")

http://helpx.adobe.com/experience-manager/using/post_files.html

Note this this article:

Although this development shows you how to configure a Sling Servlet so that authentication is not required, it is strongly recommended that you use this only when required. There are some use cases for this. For example, when you need to create a Sling Servlet that collects data from anonymous users. In most use cases, it is strongly recommended that you code your Sling Servlets so that authentication is required. 

Avatar

Employee Advisor

By default Sling supports basic authentication.

Kind regards,
Jörg

Avatar

Employee

Hi,

This works, but it would be significantly easier to user a higher-level HTTP client API which supports basic authentication.

Justin

Avatar

Level 6

justin_at_adobe wrote...

Hi,

This works, but it would be significantly easier to user a higher-level HTTP client API which supports basic authentication.

Justin

 

Thanks Justin, will keep eye on it