Httpclient HTTP-based authentication scheme

Source: Internet
Author: User

HttpClientHTTP-based authentication scheme

 

Introduction

 

HttpClient supports three different forms of HTTP Protocol Verification: Basic, Digest, and NTLM. These methods can be used to authenticate the same http server or proxy server.

 

Authentication Server

 

The HttpClient processing server authentication is almost transparent. The sender only needs to do one thing and provide a valid login certificate. This certificate is saved to the HttpState instance and can be set and obtained using the setCredentials (AuthScopeauthscope, Credentials cred) and getCredentials (AuthScope authscope) methods.

 

The built-in automatic authentication mechanism of HttpClient can be discarded by using the setDoAuthentication (boolean doAuthenticaiton) method in the HttpMethod class. This will only affect the method Instance.

 

Preemptive Authentication (Preemptive Authentication)

 

Use HttpClient to implement Preemptive authenticaiton. In this way, HttpClient will send basic authentication Response. In some cases, it will be sent before the server returns an unauthorized Response, thus reducing the connection overhead. You can use it like below

Client. getParams (). setAuthenticationPreemptive (true );

In this way, you must set the default certificate for the target server or proxy server. If the default certificate is not provided, the Preemptive Authentication method will be invalid.

Credentials defacreds = new UsernamePasswordCredentials ("username", "passowrd ");

Client. getState (). setCredentials (new AuthScope ("myhost", 80, AuthScope. ANY_REALM), defaultcreds );

 

Security aspects of server authentication (Security server verification)

 

When using the default certificate to develop an application, it may communicate with a secure web site or a common web site. When Preemtive authentication is activated or the certificate does not provide a specific authentication domain and host, HttpClient tries to use the default certificate and the target site for verification. If you want to avoid sending certificates to an untrusted site, try to narrow the certificate scope as much as possible: always specify the host.

 

Do not use AuthScope. ANY to set certificates in official products. In this way, all certificates are sent.

This setting method should only be used in the debugging stage.

// To be avoided unless in debug mode

Credentials defacreds = new UsernamePasswordCredentials ("username", "password ");

Client. getState (). setCredentials (AuthScope. ANY, defaultcreds );

 

Proxy Authentication)

 

Basic

 

Basic authentication is the most primitive and harmonious authentication solution. Unfortunately, this is also the most insecure because the user name and password it sends to the server are not encrypted. Basic authentication requires an instance of UsernamePasswordCredentials to implement, whether it is a specific domain or a default certificate.

 

Digest

 

Disgest authectication is added to the HTTP1.1 protocol. This scheme is not widely used like Basic authectication. Digest authentication is much safer than Basic authentication because it transfers encrypted user names and passwords.

 

Digest authentication requires an instance of UsernamePasswordCredentials.

 

NTLM

 

NTLM is the most complex authentication protocol supported by HttpClient. This is a patent agreement designed by Microsoft and has not published its standards. Earlier NTLM versions have poor security compared with Digest authentication due to design errors. However, this BUG was fixed in Windows NT 4 and is now considered safer than Digest authenticaiton.

Reprinted: http://blog.csdn.net/mgoann/article/details/4054747

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.