`
hao861002
  • 浏览: 84703 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

HttpClient认证(3)

阅读更多

清单 2. HttpClient 中自定义 SSL 示例

                // 1
SecureProtocolSocketFactory sampleSSLSocketFactory = new SampleSSLSocketFactory();
               // 2
Protocol httpsProtocol = new Protocol("https", sampleSSLSocketFactory, 443);
 // 3
Protocol.registerProtocol("https", httpsProtocol);
 HttpClient client = new HttpClient();
 client.getState().setCredentials(
    new AuthScope("www.sample.com", 80, AuthScope.ANY_REALM),
    new UsernamePasswordCredentials("username", "password")
);
         // Request the protected resource via SSL
GetMethod get = new GetMethod("https://www.sample.com/protected.html");
 get.setDoAuthentication( true );
 try {
    int status = client.executeMethod( get );
     // process the content from the response
    
} finally {
    get.releaseConnection();
}

 

使用 Apache HttpClient 通过 Form-Based 认证

Form-Based 认证相对 HTTP Basic 认证而言过程较为复杂,需要开发者记录下相关的 cookie 信息和部分 header 字段并多次向站点发出请求。它的大致原理如下:

注意:不同的应用可能有不同的配置方式,开发者可以先在浏览器中手动访问受保护资源,获取 login.jsp。进行分析后即可获知对应的认证服务资源 j_security_check 的位置以及对应的用户名与密码在表单中的字段。

假定我们需要访问的受保护资源为 http://www.sample.com/sampleApp/sample.rss。首先我们需要向此保护资源发出请求。而由 Form-Based 认证原理一节中可知,J2EE 服务器会将此请求重定向至 login.jsp。如果仔细分析 login.jsp 我们能发现它仅仅是一个 HTML 表单,其中有两个字段 j_username j_password 分别记录用户名和密码,而提交的目标则是 j_security_check。通常情况下,J2EE 构架会在每个站点应用的根节点定义一个 j_security_check 的资源。而我们的站点的应用程序根(Application Root)为 sampleApp。因而,通过将用户名,密码以及相关 cookie header 字段以 POST 方式发送至 http://www.sample.com/sampleApp/j_security_check 即可通过站点认证。在通过站点认证后,服务器端将给出一个新的重定向,通常它将指向了用户最初试图访问的受保护资源(本例中也就是 http://www.sample.com/sampleApp/sample.rss)。我们只需要再次创建访问对象向此资源发出请求即可获得其内容。

以下给出一个示例:


清单 3. Form-Based 认证示例

HttpClient client = new HttpClient();
client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY);
 // 1
GetMethod authget = new GetMethod("httpwww.sample.comsampleAppsample.rss");
try {
    client.executeMethod(authget);
}
catch (HttpException httpe) {
    httpe.printStackTrace();
}
catch (IOException ioe) {
    ioe.printStackTrace();
}
 // 2
NameValuePair[] data = new NameValuePair[2];
data[0] = new NameValuePair("j_username", username);
data[1] = new NameValuePair("j_password", password);
 PostMethod authpost = new PostMethod("http://www.sample.com/sampleApp/j_security_check");
authpost.setRequestBody(data);
 // 3
Header hCookie = authget.getRequestHeader("Cookie");
Header hHost = authget.getRequestHeader("Host");
Header hUserAgent = authget.getRequestHeader("User-Agent");
if (hCookie == null || hHost == null || hUserAgent == null) {
    return null;
}
 authpost.setRequestHeader(hCookie);
authpost.setRequestHeader(hHost);
authpost.setRequestHeader(hUserAgent);
 authget.releaseConnection();
 try {
    client.executeMethod(authpost);
     // 4
    Header header = authpost.getResponseHeader("location");
    if (header != null) {
        String newuri = header.getValue(); 
        GetMethod redirect = new GetMethod(newuri);
         client.executeMethod(redirect); 
        // process the content from the response
        redirect.releaseConnection();            
    }
} catch (HttpException httpe) {
    httpe.printStackTrace();
    return null;
} catch (IOException ioe) {
    ioe.printStackTrace();
    return null;
}
authpost.releaseConnection();

 

其中各个步骤解释如下:

1.                   使用 GET 方式请求 sample.rss。服务器收到连接后将在响应中给出连接信息,HttpClient 在接收到响应后会将其保存至 cookie 中。

2.                   准备第二次对 j_security_check 的连接,将用户名和密码填入新的 POST 请求的正文。

3.                   cookie 和部分 header 字段拷贝至新请求的报头中,并发送请求。

4.                   从认证成功的响应中获取重定向,并对重定向指向的资源发出请求,获取并处理内容。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics