开发者

Include some page that returns 401

I've some web page deployed at some server say:

http://myhost/some_secured_file.html

when I access this file within a browser it returns 401 askin开发者_高级运维g me to authorize myself.

The problem is that I am trying to include this page inside some JSP page using the c:import tag.

The app server returns :

javax.servlet.jsp.JspException: Problem accessing the absolute URL "http://myhost/some_secured_file.html". java
    .io.IOException: Server returned HTTP response code: 401 for URL: http://myhost/some_secured_file.html

How I can accomplish the include!?


Consider proxying the request through another jsp page or servlet. Then you let the proxy perform an authenticating request, e.g., using Apache HTTPClient, and have the contents of that response written to the page. Then you can simply import the url of your proxy on your jsp page.

Alright, consider the following pseudo code as clarification:

class Proxy extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws   ServletException, IOException {
        // Perform a new request to get contents from secured page
        HttpClient client = new HttpClient();
        Credentials credentials = new UsernamePasswordCredentials("user", "pass");
        client.getState().setCredentials(authScope, credentials);
        GetMethod method = new GetMethod("/secure_page.jsp");
        client.executeMethod(client.getHostConfiguration();, method);

        // write result to the outputstream
        resp.getWriter().write( method.getResponseBodyAsString() );
    }
}

What this servlet does is fetch the secured page for you. You need to hook this servlet up in your web descriptor. This is necessary to map e.g., /proxy.jsp request to it. What you then can do in your jsp page is something like <c:import value="proxy.jsp"/>.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜