简体   繁体   English

HTTP Servlet请求重定向到另一个URL并在Java中自动登录

[英]HTTP Servlet request redirect to another URL and login automatically in java

I'm developing a HTTP filter that redirects to another URL when some image is requested, but the other URL that the new image is, needs authentication. 我正在开发一个HTTP过滤器,当请求某些图像时,该过滤器将重定向到另一个URL,但是新图像所在的另一个URL需要身份验证。 I'm using AEM DAM to store all images. 我正在使用AEM DAM存储所有图像。 So when the code requests an image locally, the code needs to redirect to DAM and log in, but I can't log in using the code as it is implemented: 因此,当代码在本地请求图像时,代码需要重定向到DAM并登录,但是我无法使用已实现的代码登录:

@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);
    }
}

AEM supports basic authentication. AEM 支持基本身份验证。 So, while calling DAM asset URL, then pass username/password. 因此,在调用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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM