2014-03-01 34 views
0

任何人都可以向我解釋爲什麼基本身份驗證正在工作,摘要不起作用或無法正常顯示在服務器上的http頭中。澤西安全摘要與基本身份驗證

public String login(UserDTO user) 
{ 
     ClientConfig clientConfig = new DefaultClientConfig(); 
     clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); 
     Client client = Client.create(clientConfig); 
     // client.addFilter(new HTTPBasicAuthFilter(user.getUsername(), user.getPassword())); 
     client.addFilter(new HTTPDigestAuthFilter(user.getUsername(), user.getPassword())); 
     ClientResponse response = client.resource(url + "user/login").accept("application*json").type("application/json").get(ClientResponse.class); 

     System.out.println(response.toString()); 

     return null; 
} 

如果我使用:

client.addFilter(new HTTPBasicAuthFilter(user.getUsername(), user.getPassword())); 

我得到一個授權頭在服務器上:

USER LOGIN REQUEST 
request:uri: /StambomenWebAPI/rest/user/login 
method: GET 
QueryString: null 
Parameters: 
Headers: 
Name: accept Value: application*json 
Name: content-type Value: application/json 
Name: authorization Value: Basic QXhsOkxvbA== 
Name: user-agent Value: Java/1.7.0_51 
Name: host Value: localhost:8084 
Name: connection Value: keep-alive 
USER AND PASS[XXXXX, XXXXX] 

但是當我使用

client.addFilter(new HTTPDigestAuthFilter(user.getUsername(), user.getPassword())); 

我沒有得到一個授權標題字段...:s?

使用的球衣和Tomcat V7

我的問候和THX提前任何幫助

回答

2

你沒有得到,因爲摘要式身份驗證工作流程的授權頭字段。見here更多的細節,但基本上是:

  1. 客戶端發出沒有Authorization
  2. 服務器使用401個狀態和WWW-Authenticate頭,看起來像響應的請求:

    Digest realm="[email protected]", 
    qop="auth,auth-int", 
    nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", 
    opaque="5ccc069c403ebaf9f0171e9517f40e41" 
    
  3. 客戶端現在重複請求,並在正確的Authorization標頭中顯示它具有來自服務器的摘要信息

從客戶端,這一切都由澤西島HTTPDigestAuthFilter處理。因此,過濾器首先不發送Authorization標頭,並且您的服務器應返回401狀態,WWW-Authenticate標頭具有必要的摘要信息。然後,篩選器使用正確的Authorization標題重複請求,並且您的服務器應進行身份驗證並返回內容。

在這次初始握手之後,HTTPDigestAuthFilter會記住必要的摘要信息,因此對於首次請求後的所有請求,將包含Authorization標頭。