我正在使用JAIN-SIP注册到Asterisk服务器并使用其他SIP软电话发起呼叫。对于Asterisk,我正在运行http://www.raspberry-asterisk.org/的默认配置,并添加了一些扩展(SIP用户)。
当我向Asterisk发送REGISTER消息时,(Asterisk)服务器会按预期发回验证质询。我的问题是,一旦我回应这个挑战,Asterisk再次请求相同的身份验证。在第二次认证响应之后,它最终被接受。我不明白为什么它会这样运作,所以我认为我必须有一些非常基本的错误。
示例代码:
package sip.test.example;
import gov.nist.javax.sip.SipStackExt;
import gov.nist.javax.sip.clientauthutils.*;
import javax.sip.*;
import javax.sip.address.*;
import javax.sip.header.*;
import javax.sip.message.*;
import java.util.*;
public class WhyDoesThisAuthenticateTwice {
private SipStackExt sipStack;
private HeaderFactory header;
private SipProvider udp;
private final String sipId = "2";
private final String myIp = "192.168.1.152";
private final int myPort = 5060;
private final String myPw = "password22";
private final String realm = "asterisk";
private final String asteriskIp = "192.168.1.171";
private final int asteriskPort = 5060;
private final String tag = "fgdfdf";
public static void main(String[] args) throws Exception {
new WhyDoesThisAuthenticateTwice().register();
}
public void register() throws Exception {
SipFactory sipFactory = SipFactory.getInstance();
sipFactory.setPathName("gov.nist");
Properties properties = new Properties();
properties.setProperty("javax.sip.STACK_NAME", "test-phone");
properties.setProperty("javax.sip.OUTBOUND_PROXY", asteriskIp+":"+asteriskPort+"/udp");
this.sipStack = (SipStackExt) sipFactory.createSipStack(properties);
header = sipFactory.createHeaderFactory();
AddressFactory address = sipFactory.createAddressFactory();
MessageFactory message = sipFactory.createMessageFactory();
ListeningPoint udpPoint = sipStack.createListeningPoint(myIp, myPort, "udp");
MySIPListener listener = new MySIPListener();
udp = sipStack.createSipProvider(udpPoint);
udp.addSipListener(listener);
SipURI myRealmURI = address.createSipURI(sipId, realm);
Address fromAddress = address.createAddress(myRealmURI);
fromAddress.setDisplayName(sipId);
FromHeader fromHeader = header.createFromHeader(fromAddress, tag);
SipURI myURI = address.createSipURI(sipId, myIp);
myURI.setPort(myPort);
Address contactAddress = address.createAddress(myURI);
contactAddress.setDisplayName(sipId);
ContactHeader contactHeader = header.createContactHeader(contactAddress);
MaxForwardsHeader maxForwards = header.createMaxForwardsHeader(5);
List<ViaHeader> viaHeaders = new ArrayList<>();
CallIdHeader callIdHeader = udp.getNewCallId();
long seq = 1;
CSeqHeader cSeqHeader = header.createCSeqHeader(seq++, Request.REGISTER);
ToHeader toHeader = header.createToHeader(fromAddress, null);
URI requestURI = address.createURI("sip:"+asteriskIp+":"+asteriskPort);
Request request = message.createRequest(requestURI, Request.REGISTER, callIdHeader,
cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwards);
request.addHeader(contactHeader);
ExpiresHeader eh = header.createExpiresHeader(300);
request.addHeader(eh);
ClientTransaction transaction = udp.getNewClientTransaction(request);
transaction.sendRequest();
System.out.println("Sent request:");
System.out.println(request);
}
private class MySIPListener implements SipListener {
@Override
public void processRequest(RequestEvent requestEvent) {}
@Override
public void processResponse(ResponseEvent event) {
try {
Response response = event.getResponse();
System.out.println("Response received:");
System.out.println(response);
if (response.getStatusCode() != 401) return;
ClientTransaction tid = event.getClientTransaction();
AccountManagerImpl manager = new AccountManagerImpl();
AuthenticationHelper helper = sipStack.getAuthenticationHelper(manager, header);
ClientTransaction transaction = helper.handleChallenge(response, tid, udp, 5);
transaction.sendRequest();
Request request = transaction.getRequest();
System.out.println("Sent request with authentication info:");
System.out.println(request);
} catch (SipException e) { e.printStackTrace(); }
}
@Override
public void processTimeout(TimeoutEvent timeoutEvent) {}
@Override
public void processIOException(IOExceptionEvent exceptionEvent) {}
@Override
public void processTransactionTerminated(TransactionTerminatedEvent transactionTerminatedEvent) {}
@Override
public void processDialogTerminated(DialogTerminatedEvent dialogTerminatedEvent) {}
}
private class AccountManagerImpl implements AccountManager {
@Override
public UserCredentials getCredentials(ClientTransaction clientTransaction, String s) {
return new UserCredentials() {
@Override
public String getUserName() { return sipId; }
@Override
public String getPassword() { return myPw; }
@Override
public String getSipDomain() { return realm; }
};
}
}
}
当我运行它时,这就是我得到的:
Sent request:
REGISTER sip:192.168.1.171:5060 SIP/2.0
Call-ID: 6a1cb516446a3d66806d8750334b8d50@192.168.1.152
CSeq: 1 REGISTER
From: "2" <sip:2@asterisk>;tag=fgdfdf
To: "2" <sip:2@asterisk>
Max-Forwards: 5
Contact: "2" <sip:2@192.168.1.152:5060>
Expires: 300
Via: SIP/2.0/UDP 192.168.1.152:5060;branch=z9hG4bK-373634-2421c1e13c920e5a75f69f9a3f80f8d8
Content-Length: 0
Response received:
SIP/2.0 401 Unauthorized
Via: SIP/2.0/UDP 192.168.1.152:5060;branch=z9hG4bK-373634-2421c1e13c920e5a75f69f9a3f80f8d8;received=192.168.1.152
From: "2" <sip:2@asterisk>;tag=fgdfdf
To: "2" <sip:2@asterisk>;tag=as42a729fa
Call-ID: 6a1cb516446a3d66806d8750334b8d50@192.168.1.152
CSeq: 1 REGISTER
Server: FPBX-2.11.0(11.6.0)
Allow: INVITE,ACK,CANCEL,OPTIONS,BYE,REFER,SUBSCRIBE,NOTIFY,INFO,PUBLISH
Supported: replaces,timer
WWW-Authenticate: Digest algorithm=MD5,realm="asterisk",nonce="5bdc425c"
Content-Length: 0
Sent request with authentication info:
REGISTER sip:192.168.1.171:5060;maddr=192.168.1.171 SIP/2.0
Call-ID: 6a1cb516446a3d66806d8750334b8d50@192.168.1.152
CSeq: 2 REGISTER
From: "2" <sip:2@asterisk>;tag=fgdfdf
To: "2" <sip:2@asterisk>
Max-Forwards: 5
Contact: "2" <sip:2@192.168.1.152:5060>
Expires: 300
Via: SIP/2.0/UDP 192.168.1.152:5060;branch=z9hG4bK-373634-18fc265c4d5854f9a970d7545facd464
Authorization: Digest username="2",realm="asterisk",nonce="5bdc425c",uri="sip:192.168.1.171:5060;maddr=192.168.1.171",response="6dc54ff9338920a9b6d7d8755ce719cc",algorithm=MD5
Content-Length: 0
Response received:
SIP/2.0 401 Unauthorized
Via: SIP/2.0/UDP 192.168.1.152:5060;branch=z9hG4bK-373634-18fc265c4d5854f9a970d7545facd464;received=192.168.1.152
From: "2" <sip:2@asterisk>;tag=fgdfdf
To: "2" <sip:2@asterisk>;tag=as46d30f80
Call-ID: 6a1cb516446a3d66806d8750334b8d50@192.168.1.152
CSeq: 2 REGISTER
Server: FPBX-2.11.0(11.6.0)
Allow: INVITE,ACK,CANCEL,OPTIONS,BYE,REFER,SUBSCRIBE,NOTIFY,INFO,PUBLISH
Supported: replaces,timer
WWW-Authenticate: Digest algorithm=MD5,realm="asterisk",nonce="75ac799b"
Content-Length: 0
Sent request with authentication info:
REGISTER sip:192.168.1.171:5060;maddr=192.168.1.171 SIP/2.0
Call-ID: 6a1cb516446a3d66806d8750334b8d50@192.168.1.152
CSeq: 3 REGISTER
From: "2" <sip:2@asterisk>;tag=fgdfdf
To: "2" <sip:2@asterisk>
Max-Forwards: 5
Contact: "2" <sip:2@192.168.1.152:5060>
Expires: 300
Via: SIP/2.0/UDP 192.168.1.152:5060;branch=z9hG4bK-373634-64f022399c1ad437b6fffa21782c9376
Authorization: Digest username="2",realm="asterisk",nonce="75ac799b",uri="sip:192.168.1.171:5060;maddr=192.168.1.171",response="cd18746b954cfca05a65c04a23be4e77",algorithm=MD5
Content-Length: 0
Response received:
SIP/2.0 200 OK
Via: SIP/2.0/UDP 192.168.1.152:5060;branch=z9hG4bK-373634-64f022399c1ad437b6fffa21782c9376;received=192.168.1.152
From: "2" <sip:2@asterisk>;tag=fgdfdf
To: "2" <sip:2@asterisk>;tag=as46d30f80
Call-ID: 6a1cb516446a3d66806d8750334b8d50@192.168.1.152
CSeq: 3 REGISTER
Server: FPBX-2.11.0(11.6.0)
Allow: INVITE,ACK,CANCEL,OPTIONS,BYE,REFER,SUBSCRIBE,NOTIFY,INFO,PUBLISH
Supported: replaces,timer
Expires: 300
Contact: <sip:2@192.168.1.152:5060>;expires=300
Date: Mon, 17 Feb 2014 20:27:02 GMT
Content-Length: 0
正如你所看到的,我两次获得401 Unauthorized。两次重新发送REGISTER并包含认证响应。
所以我的问题是,为什么第一次不接受身份验证,但第二次验证?它由完全相同的代码执行。
我发送INVITE消息时遇到了类似的问题。成功注册后,当我发送INVITE时,Asterisk要求我再次进行身份验证。上面的MySIPListener代码也处理这个代码并生成带有身份验证响应的所需INVITE。但是,我在这上面找到的所有示例总是显示直接邀请工作,因为我认为,您应该已经过身份验证。也许与同一问题有关?
那么我做错了什么?
答案 0 :(得分:0)
在您提交基于sip:192.168.1.171:5060
的质询响应时,第一次尝试的请求URI为sip:192.168.1.171:5060;maddr=192.168.1.171
,并且仅在下次尝试时更新。