2017-07-19 27 views
1

我在netbeans ide中創建了此webservice 我希望在客戶端發出任何請求之前進行基本授權。該服務工作正常,但我如何使用httpconnection類從客戶端傳遞用戶名和密碼。 這是我的web服務。如何使用HttpURLConnection在java web服務中執行身份驗證

import java.util.List; 
import java.util.Map; 
import javax.annotation.Resource; 
import javax.jws.WebService; 
import javax.xml.ws.WebServiceContext; 
import javax.xml.ws.handler.MessageContext; 

@WebService(serviceName = "SampleWs") 
public class SampleWs implements CreateCustomer { 
    @Resource 
    WebServiceContext wsctx; 
    @Override 
    public String createCustomer(Customers customer) { 
     String resp="Access Denied"; 

     MessageContext mctx = wsctx.getMessageContext(); 

     Map http_headers = (Map) mctx.get(MessageContext.HTTP_REQUEST_HEADERS); 
     String username = (String) http_headers.get("username");//should come from the client request 
     String password = (String) http_headers.get("password");//should come from the client request 
     if(username.equals("admin")&&password.equals("pass")) 
     { 
      resp="Authenticated"; 
     } 
     return resp; 

    } 


} 
//interface 
import javax.jws.WebMethod; 

import javax.jws.WebService; 
import javax.jws.soap.SOAPBinding; 
import javax.jws.soap.SOAPBinding.Style; 

@WebService 
@SOAPBinding(style = Style.RPC) 
public interface CreateCustomer { 
    @WebMethod String createCustomer(Customers customer); 
} 
//model class 
public class Customers {  
    private int id; 
    private String fname; 
    private String sname; 
    private String gender; 
    private String email; 

    //getters and setters 
} 

這裏是我的客戶

public class SampleClient { 

    private static final String url_ = "http://localhost:7001/SampleWs/SampleWs"; 



    public static String testAuthorisation() { 
     String varresp = ""; 
     StringBuilder answer = new StringBuilder(); 
     try { 
      String req = getSoapRequestXMl(); 
      String name = "adm"; 
      String password = "pass"; 

      String authString = name + ":" + password; 

      byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());//apache lib for Base64 
      String authStringEnc = new String(authEncBytes); 

      URL url = new URL(url_); 
      HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
      conn.setRequestProperty("Content-Type", "text/xml"); 
      //conn.setRequestProperty ("Authorization", "Basic " + authStringEnc); 

      conn.setDoOutput(true); 
      OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream()); 
      writer.write(req); 
      writer.flush(); 

      BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
      String line; 
      while ((line = reader.readLine()) != null) { 
       answer.append(line); 
      } 
      writer.close(); 
      reader.close(); 
      varresp = answer.toString(); 

     } catch (Exception e) { 
      e.printStackTrace(); 
      varresp = "!" + e; 

     } finally { 
      return varresp; 
     } 

    } 

    private static String getSoapRequestXMl() { 
     String request = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" 
       + " <soap:Header/>\n" 
       + " <soap:Body>\n" 
       + "  <ns1:hello xmlns:ns1=\"http://ws.ecs.co/\">\n" 
       + "   <name>\n" 
       + "    <email>[email protected]</email>\n" 
       + "    <fname>Firsname</fname>\n" 
       + "    <gender>Male</gender>\n" 
       + "    <id>23</id>\n" 
       + "    <sname>Nemuga</sname>\n" 
       + "   </name>\n" 
       + "  </ns1:hello>\n" 
       + " </soap:Body>\n" 
       + "</soap:Envelope>"; 

     return request; 
    } 
} 

回答

2

這在客戶端代碼的行會添加所需頭基本身份驗證

conn.setRequestProperty ("Authorization", "Basic " + authStringEnc); 

在服務器端,你需要閱讀「授權」標題並提取內容

Map<String, List<String>> headers= (Map<String, List<String>>) messageContext 
       .get(MessageContext.HTTP_REQUEST_HEADERS); 

//The header "Basic base64(user:password) 
String authHeader = headers.get("Authorization").get(0); 

//Remove "Basic " 
String authtoken = authorizationHeader.split(" ")[1]; 

//Decode base64 and read username and password 
String token = new String(DatatypeConverter.parseBase64Binary(authtoken)); 
String tokenS[] = token.split(":"); 
String username = tokenS [0]; 
String password = tokenS [1]; 

我沒有測試所有的代碼,但它應該工作

+0

當我運行代碼正在逐漸產生java.io.IOException:網址 –

+0

我的代碼從http提取用戶名和密碼,401:服務器返回的HTTP響應代碼頭,它不會執行授權本身。客戶端是否發送頭文件?服務器是否接收到它?使用authHeader,authToken,用戶名和密碼變量的值顯示服務器日誌 – pedrofb

+0

服務器上根本沒有日誌 –

相關問題