2015-06-02 50 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