2017-07-29 88 views
0

我正在開發一個HTTP過濾器,它在請求某個圖像時重定向到另一個URL,但新圖像所在的另一個URL需要身份驗證。我使用AEM DAM來存儲所有圖像。所以,當本地代碼請求圖象,代碼需要重定向到大壩和登錄,但我無法登錄使用的代碼,因爲它是實現:HTTP Servlet請求重定向到另一個URL並在java中自動登錄

@Override 
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
     throws IOException, ServletException { 

    HttpServletRequest req = (HttpServletRequest) request; 
    // ==> /images/path/my-image.png 
    // ==> /path1/path2/image.pngs 
    String servletPath = req.getServletPath(); 

    // The path to the root directory of the webapp (WebContent) 
    //String realRootPath = request.getServletContext().getRealPath(""); 
    String realRootPath = "http://localhost:4502/crx/de/index.jsp#/content/dam/"; 

    // Real path of Image. 
    String imageRealPath = realRootPath + servletPath; 

    System.out.println("imageRealPath = " + imageRealPath); 

    File file = new File(imageRealPath); 

    // Check image exists. 
    if (file.exists()) { 
     // Go to the next element (filter or servlet) in chain 
     chain.doFilter(request, response); 
    } else if (!servletPath.equals(this.notFoundImage)) { 
     // Redirect to 'image not found' image. 
     HttpServletResponse resp = (HttpServletResponse) response; 
     // ==> /ServletFilterTutorial + /images/image-not-found.png 
     resp.sendRedirect(req.getContextPath()+ this.notFoundImage); 
    } 
} 
+0

_crx/de/index.jsp#_在_realRootPath_中看起來不正確。您是在AEM或單獨的應用程序中開發過濾器嗎?對於第一種情況,您可以使用具有適當權限的服務資源解析器編寫SlingFilter,以讀取DAM資產並返回圖像數據作爲響應。對於第二種情況,AEM提供了可以使用[cURL](http://www.aemcq5tutorials.com/tutorials/adobe-cq5-aem-curl-commands/)和相應的[Java實現](https: //stackoverflow.com/questions/3283234/http-basic-authentication-in-java-using-httpclient)訪問您的資產。 –

回答

0

AEM supports基本身份驗證。因此,在調用DAM資產URL時,請傳遞用戶名/密碼。

byte[] authEncBytes = Base64.encodeBase64("admin:admin".getBytes()); 
String authStringEnc = new String(authEncBytes);   
URL url = new URL("http://localhost:4502/content/dam/myassets/test.jpg"); 
     URLConnection urlConnection = url.openConnection(); 
     urlConnection.setRequestProperty("Authorization", "Basic " + 
     authStringEnc); 
     InputStream is = urlConnection.getInputStream(); 
相關問題