JAX-WS请求使用基本身份验证

3

我试图使用基本授权和处理程序调用SOAP Webservice,但是API以401未经授权的方式响应。

@Override
public boolean handleMessage(SOAPMessageContext context) {
    Boolean outboundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    if (outboundProperty.booleanValue()) {
        String authString = parameter.getUser() + ":" + parameter.getPassword();
        try {
             Map<String, List<String>> headers = (Map<String, List<String>>)
                     context.get(MessageContext.HTTP_REQUEST_HEADERS);

             if (null == headers) {
                 headers = new HashMap<String, List<String>>();
             }

             headers.put("Authorization", Collections.singletonList(
                 "Basic " + new String(Base64.encode(authString.getBytes()))));
        } catch(Exception e) {
            log4j.error(e.getMessage(), e);
        }
    }
    return outboundProperty;
}

当我使用SOAP UI并手动添加Authorization Header(来自调试代码的值)时,我可以从端点接收到响应,但是现在使用代码会失败。
任何提示都将非常有帮助。谢谢。

你试过我的更新答案了吗,兄弟? - Mohsen
@Spara,使用 sun.misc 中的 BASE64Encoder() 并没有帮助我。 - lesnar
你尝试过返回 true 而不是返回 outboundProperty 吗? - Mohsen
发生了什么事,兄弟?你解决问题了吗?赏金将在2天内消失。 - Mohsen
请查看以下两个Stack Overflow链接:https://dev59.com/7Gw05IYBdhLWcg3wuUAL https://dev59.com/u3E95IYBdhLWcg3wadKq#36847739 我相信您可以找到问题。 - Mohsen
1个回答

2
您需要将您的代码更改为以下内容:

您需要将您的代码更改为以下内容:

@Override
public boolean handleMessage(SOAPMessageContext context) {
    Boolean outboundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    if(outboundProperty.booleanValue()){
        try{
            String authString = parameter.getUser() + ":" + parameter.getPassword();
            SOAPMessage soapMessage =context.getMessage();
            String authorization = new sun.misc.BASE64Encoder().encode(authString.getBytes());
            soapMessage.getMimeHeaders().addHeader("Authorization","Basic " + authorization);   
            soapMessage.saveChanges(); 
        }catch(Exception e){
            log4j.error(e.getMessage(), e);
        }
    }
    return true;
}

更新:

此处所解释的那样,您应该使用sun.misc.BASE64Encoder()中的Base64Coder来编码authString

此外,您应该始终从此方法返回true,否则将通过返回false来阻止处理处理程序请求链。


2
你能突出实际的变化并提供解释吗? - Glains

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接