2015-06-02 47 views
0

我想要打包含xml結構的頁面。對於我正在使用此代碼CQ5中從存儲庫訪問頁面時的授權問題。

  @Reference 
      private SlingRepository repository; 

      adminSession = repository.loginAdministrative(repository.getDefaultWorkspace()); 
      String pageUrl = "http://localhost:4504"+page+".abc.htm"; 
      conn = (HttpURLConnection)new URL(pageUrl).openConnection(); 
      conn.setRequestProperty("Accept-Charset", charset); 
      conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401"); // Do as if you'rusing Firefox 3.6.3 
      urlResponse = new BufferedInputStream(conn.getInputStream()); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(urlResponse)); 

雖然accesing我得到這個問題

org.apache.sling.auth.core.impl.SlingAuthenticator getAnonymousResolver: `Anonymous access not allowed by configuration - requesting credentials` 

我登錄的管理員,在每當我直接打這個urlfrom瀏覽器的頁面它正在工作正常bt而訪問它thriugh我的代碼我得到這個錯誤。

有什麼建議嗎?

+1

您嘗試在同一個cq實例上訪問的頁面是否?如果是這樣,你爲什麼要做一個http請求?您可以通過[ResourceResolverFactory](https://docs.adobe.com/docs/en/aem/6-0/develop/ref/javadoc/org/apache/sling/api/resource/ResourceResolverFactory)訪問存儲庫。 HTML),或者如果您真的需要執行 – d33t

回答

0

如果你試圖調用一個作者的情況下的URL,下面的方法我在我的項目之一使用可能會幫助(使用Apache Commons的HttpClient):

private InputStream getContent(final String url) 
    HttpClient httpClient = new HttpClient(); 
    httpClient.getParams().setAuthenticationPreemptive(true); 
    httpClient.getState().setCredentials(new AuthScope(null, -1, null), 
     new UsernamePasswordCredentials("admin", "admin")); 
    try { 
     GetMethod get = new GetMethod(url); 
     httpClient.executeMethod(get); 
     if (get.getStatusCode() == HttpStatus.SC_OK) { 
      return get.getResponseBodyAsStream(); 
     } else { 
      LOGGER.error("HTTP Error: ", get.getStatusCode()); 
     } 
    } catch (HttpException e) { 
     LOGGER.error("HttpException: ", e); 
    } catch (IOException e) { 
     LOGGER.error("IOException: ", e); 
    } 
} 

雖然它是使用admin:管理它只適用於本地開發實例,如果你在一個生產環境,我不會把管理員密碼以明文,即使它是onyl代碼...

+0

謝謝托馬斯,它可以使用吊索內部請求解析過程。但如果我的網址是發佈實例,我不想通過crendentials(發佈它不需要),那麼我可以如何編碼它。我只需要編寫httpClient.getState();或者是其他東西 ? – user2142786

+0

如果您正在訪問發佈服務器,則根本不需要設置任何憑證。所以一個簡單的GetMethod就足夠了。所以我首先看不到你的問題。也許該頁面在發佈時不存在? – Thomas

0

你在混合吊索憑證& http證書。當您在sling-repository上登錄時,http會話不知道任何驗證信息!

+0

但相同的代碼工作正常的作者實例的另一個網址。 – user2142786