Java Authenticator «Сервер перенаправлен слишком много раз» от Tomcat9 / JDK8 в контейнере Docker - PullRequest
0 голосов
/ 13 июля 2020
• 1000 , то его передающий Сервер перенаправлялся слишком много раз (20). Ниже представлен сегмент кода:
String attachtmentname = "https://<full url>";
String file_name = "<file>.pdf";
String Userid = "<some id>";
String password = "<some password>";
response.setHeader("Content-type", "application/pdf");
response.setHeader("ReportPDF", "content-description");
response.setHeader("Content-Disposition", "inline; filename=" + file_name);
response.setHeader("Cache-Control", "cache,must-revalidate");
System.setProperty("http.proxyUser", Userid);
System.setProperty("http.proxyPassword", password);
Authenticator.setDefault(new Authenticator() {
            
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(Userid, password.toCharArray());
                }
            });
            CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
BufferedInputStream in = new BufferedInputStream(new URL(attachtmentname).openStream());
<rest of the code for reading pdf file>

Ошибка «Сервер перенаправлен слишком много раз (20)»

java.net.ProtocolException: Server redirected too many  times (20)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1908)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1498)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:268)
    at java.net.URL.openStream(URL.java:1068)

1 Ответ

0 голосов
/ 18 июля 2020

Получил работу, используя Java библиотеку nio. Ниже показан тестовый код, если он кому-то поможет.

String attachtmentname = "https://full url/somefile.pdf";
String Userid = "domain\\username";
String password = "password"; 

        Authenticator.setDefault(new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(Userid, password.toCharArray());
                }
            });
URL url = new URL(attachtmentname);
try (InputStream in = url.openStream()) {
    Path pwd = Paths.get("Somepath\\someFile1.pdf").toAbsolutePath();
    out.println(pwd);
    
  Files.copy(in, pwd, StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
   e.printStackTrace();
}
...