我正在尝试开发一个通用的SOAP客户端Web服务。我被困在 HTTP基本身份验证部分,我有一个需要此类身份验证的服务,我使用 SOAP UI API 。
我的问题是如何使用SOAP UI API在HTTP标头中为基本类型注入用户名和密码。
我尝试使用此代码,但我遇到此错误: soap:mustUnderstand = 1
Web Service身份验证的主要类:
public class BasicAuthAuthorizationInterceptor extends SoapHeaderInterceptor {
@SuppressWarnings("unchecked")
@Override
public void handleMessage(Message message) throws Fault {
Map<String, List<String>> requestHeaders = (Map<String,
List<String>>) message
.get(Message.PROTOCOL_HEADERS);
List<String> authHeaderList = requestHeaders.get("Authorization");
if (authHeaderList != null && authHeaderList.size() > 0) {
if ((authHeaderList.get(0)).equals("Basic c2FicmluZTpzYWJyaW5lcGFzcw==")){
System.out.println("The user is known +++++++");
} else {
sendErrorResponse(message, HttpURLConnection.HTTP_FORBIDDEN);
System.out.println("The user is unknown -------");
}
} else {
sendErrorResponse(message, HttpURLConnection.HTTP_UNAUTHORIZED);
// System.out.println("No user ************");
return;
}
}
Web服务cLient用于进行身份验证的方法:
public List<SoapOutput> execute(String user, String pass) throws IOException {
if ((user!=null && pass!=null) && (!user.equals("") && !pass.equals("")))
{
// Add WSSecurity parameters
wsdlRequest.setWssPasswordType("PasswordText");
wsdlRequest.setUsername(user);
wsdlRequest.setPassword(pass);
wsdlRequest.setWssTimeToLive("10000");
}
Map<String, List<String>> requestHeaders = wsdlRequest.getRequestHeaders();
requestHeaders.put("username", Collections.singletonList(user));
requestHeaders.put("Password", Collections.singletonList(pass));
System.out.println("requestHeaders is: " + requestHeaders);
....
答案 0 :(得分:0)
尝试以下代码(groovy)
import com.eviware.soapui.support.types.StringToStringsMap
def headers = new StringToStringsMap()
headers.put(testSuite.getPropertyValue("ExampleHeaderName"),testSuite.getPropertyValue("ExampleHeaderValue"))
testStep.getTestRequest().setRequestHeaders(headers)
有关此方法的更多详细信息已在http://forum.soapui.org/viewtopic.php?t=3746或http://www.techinterviewpuzzles.com/2013/07/soapui-add-header-to-all-requests-using.html
中进行了讨论答案 1 :(得分:0)
您可以在上面的客户端中尝试以下代码:
if ((user!=null && password!=null ) && (!user.equals("") && !password.equals(""))) {
StringToStringsMap requestHeaders = wsdlRequest.getRequestHeaders();
String basicAuthentication = getBasicAuthentication(user, password);
requestHeaders .put("Authorization", basicAuthentication);
wsdlRequest.setRequestHeaders(requestHeaders );
}
private String getBasicAuthentication(String user, String password) {
String token = user + ":" + password;
try {
return "Basic "
+ DatatypeConverter.printBase64Binary(token
.getBytes("UTF-8"));
} catch (UnsupportedEncodingException ex) {
throw new IllegalStateException("Cannot encode with UTF-8", ex);
}
}