Java 无法添加sip授权标头
我对jain sip库的身份验证有疑问。我正在使用jain sip向Asterisk服务器注册sip帐户。 当我尝试添加Java 无法添加sip授权标头,java,asterisk,jain-sip,Java,Asterisk,Jain Sip,我对jain sip库的身份验证有疑问。我正在使用jain sip向Asterisk服务器注册sip帐户。 当我尝试添加AuthorizationHeader时,出现以下错误消息: 类型Utils的方法makeAuthHeader(HeaderFactory、Response、Request、String、String)未定义 以下是代码片段: AuthorizationHeader authHeader = Utils.makeAuthHeader( heade
AuthorizationHeader
时,出现以下错误消息:
类型Utils的方法makeAuthHeader(HeaderFactory、Response、Request、String、String)未定义
以下是代码片段:
AuthorizationHeader authHeader = Utils.makeAuthHeader(
headerFactory, response, request, userId, password);
request.addHeader(authHeader);
似乎找不到方法makeAuthHeader()
代码SipClientServiceImpl.java:
@Service("clientService")
public class SipClientServiceImpl implements SipClientService, SipListener {
SipFactory sipFactory; // Used to access the SIP API.
SipStack sipStack; // The SIP stack.
SipProvider sipProvider; // Used to send SIP messages.
MessageFactory messageFactory; // Used to create SIP message factory.
HeaderFactory headerFactory; // Used to create SIP headers.
AddressFactory addressFactory; // Used to create SIP URIs.
ListeningPoint listeningPoint; // SIP listening IP address/port.
Properties properties; // Other properties.
// Objects keeping local configuration.
String ip; // The local IP address.
int port = 6060; // The local port.
String protocol = "udp"; // The local protocol (UDP).
int tag = (new Random()).nextInt(); // The local tag.
Address contactAddress; // The contact address.
ContactHeader contactHeader;
String asteriskServer = "10.0.0.0.0";
int asteriksPort = 5060;
String sipPasswordForAllAccounts = "1234";
Request request = null;
Response response = null;
private ClientTransaction inviteTid;
long invco = 1;
public void registerToAsterisk(String userId) throws ParseException,
InvalidArgumentException, PeerUnavailableException,
TransportNotSupportedException, ObjectInUseException,
TooManyListenersException {
init();
try {
// Get the destination address from the text field.
Address addressTo = addressFactory.createAddress("sip:" + userId
+ '@' + asteriskServer + ":" + asteriksPort);
// Create the request URI for the SIP message.
javax.sip.address.URI requestURI = addressTo.getURI();
// Create the SIP message headers.
// The "Via" headers.
ArrayList viaHeaders = new ArrayList();
ViaHeader viaHeader = this.headerFactory.createViaHeader(this.ip,
this.port, "udp", null);
viaHeaders.add(viaHeader);
// The "Max-Forwards" header.
MaxForwardsHeader maxForwardsHeader = this.headerFactory
.createMaxForwardsHeader(70);
// The "Call-Id" header.
CallIdHeader callIdHeader = this.sipProvider.getNewCallId();
// The "CSeq" header.
CSeqHeader cSeqHeader = this.headerFactory.createCSeqHeader(1L,
"REGISTER");
// The "From" header.
FromHeader fromHeader = this.headerFactory.createFromHeader(
this.contactAddress, String.valueOf(this.tag));
// The "To" header.
ToHeader toHeader = this.headerFactory.createToHeader(addressTo,
null);
// AuthorizationHeader authHeader = Utils.makeAuthHeader(
// headerFactory, response, request, userId,
// sipPasswordForAllAccounts);
// Create the REGISTER request.
Request request = this.messageFactory.createRequest(requestURI,
"REGISTER", callIdHeader, cSeqHeader, fromHeader, toHeader,
viaHeaders, maxForwardsHeader);
// Add the "Contact" header to the request.
request.addHeader(contactHeader);
// request.addHeader(authHeader);
// Send the request statelessly through the SIP provider.
this.sipProvider.sendRequest(request);
} catch (Exception e) {
// TODO: handle exception
}
// TODO Auto-generated method stub
}
public void processRequest(RequestEvent requestEvent) {
}
public void processResponse(ResponseEvent responseEvent) {
Response response = responseEvent.getResponse();
System.out.println(response);
ClientTransaction tid = responseEvent.getClientTransaction();
CSeqHeader cseq = (CSeqHeader) response.getHeader(CSeqHeader.NAME);
System.out.println("Response received : Status Code = "
+ response.getStatusCode() + " " + cseq);
// if (tid == null) {
// System.out.println("Stray response -- dropping ");
// return;
// }
try {
if (response.getStatusCode() == Response.PROXY_AUTHENTICATION_REQUIRED
|| response.getStatusCode() == Response.UNAUTHORIZED) {
AuthenticationHelper authenticationHelper = ((SipStackExt) sipStack)
.getAuthenticationHelper(new AccountManagerImpl(),
headerFactory);
inviteTid = authenticationHelper.handleChallenge(response, tid,
sipProvider, 5, false);
inviteTid.sendRequest();
invco++;
}
} catch (Exception ex) {
ex.printStackTrace();
System.exit(0);
}
}
public void processTimeout(TimeoutEvent timeoutEvent) {
// TODO Auto-generated method stub
}
public void processIOException(IOExceptionEvent exceptionEvent) {
// TODO Auto-generated method stub
}
public void processTransactionTerminated(
TransactionTerminatedEvent transactionTerminatedEvent) {
// TODO Auto-generated method stub
}
public void processDialogTerminated(
DialogTerminatedEvent dialogTerminatedEvent) {
// TODO Auto-generated method stub
}
public void init() {
// Get the local IP address.
try {
this.ip = InetAddress.getLocalHost().getHostAddress();
// Create the SIP factory and set the path name.
this.sipFactory = SipFactory.getInstance();
this.sipFactory.setPathName("gov.nist");
// Create and set the SIP stack properties.
this.properties = new Properties();
this.properties.setProperty("javax.sip.STACK_NAME", "stack");
// Create the SIP stack.
this.sipStack = this.sipFactory.createSipStack(this.properties);
// Create the SIP message factory.
this.messageFactory = this.sipFactory.createMessageFactory();
// Create the SIP header factory.
this.headerFactory = this.sipFactory.createHeaderFactory();
// Create the SIP address factory.
this.addressFactory = this.sipFactory.createAddressFactory();
// Create the SIP listening point and bind it to the local IP
// address, port and protocol.
this.listeningPoint = this.sipStack.createListeningPoint(this.ip,
this.port, this.protocol);
// Create the SIP provider.
this.sipProvider = this.sipStack
.createSipProvider(this.listeningPoint);
// Add our application as a SIP listener.
this.sipProvider.addSipListener(this);
// Create the contact address used for all SIP messages.
this.contactAddress = this.addressFactory.createAddress("sip:"
+ this.ip + ":" + this.port);
// Create the contact header used for all SIP messages.
this.contactHeader = this.headerFactory
.createContactHeader(contactAddress);
} catch (Exception e) {
e.printStackTrace();
}
}
}
堆栈跟踪:
javax.sip.SipException: Unexpected exception
at gov.nist.javax.sip.clientauthutils.AuthenticationHelperImpl.handleChallenge(AuthenticationHelperImpl.java:298)
at com.musala.ving.voip.SipClientServiceImpl.processResponse(SipClientServiceImpl.java:152)
at gov.nist.javax.sip.EventScanner.deliverEvent(EventScanner.java:296)
at gov.nist.javax.sip.EventScanner.run(EventScanner.java:519)
at java.lang.Thread.run(Unknown Source)
原因:java.lang.NullPointerException
访问gov.nist.javax.sip.clientauthutils.AuthenticationHelperImpl.handleChallenge(AuthenticationHelperImpl.java:149)
... 4更多
任何建议都将不胜感激
谢谢大家! 类Utils不拥有此方法,您可以在此处看到:
你必须使用一些不同的东西,但我找不到关于使用什么类/方法的任何信息。好吧,Utils不是API的一部分,它根本没有你试图使用的方法。进行客户端身份验证的最佳方法是使用
这是相关部分:
if (response.getStatusCode() == Response.PROXY_AUTHENTICATION_REQUIRED
|| response.getStatusCode() == Response.UNAUTHORIZED) {
AuthenticationHelper authenticationHelper =
((SipStackExt) sipStack).getAuthenticationHelper(new AccountManagerImpl(), headerFactory);
inviteTid = authenticationHelper.handleChallenge(response, tid, sipProvider, 5);
inviteTid.sendRequest();
invco++;
}
很难说到底发生了什么,因为Utils并不是真正的API。这个例子应该对您有所帮助。否则发布完整的代码和日志,感谢您的回复!我注意到了。这就是在这里发布问题的原因:)谢谢你的回答我是否需要在public void processResponse中添加以下代码(ResponseEvent ResponseEvent
方法?出于某种原因,即使在执行代码段中的步骤后,它也不允许我进行注册。:/很可能是的。但这取决于情况。如果您正在进行注册,请确保调整代码,使其注册事务,而不是INVITE。下面的行似乎是问题所在inviteTid=authenticationHelper.handleChallenge(响应,tid,sipProvider,5,false);
It trows SIPException tid为空。您应该在客户端事务的上下文中发送请求以获取非空tid,但即使这样,也有更可靠的方法来实现。使用类似的方法可以使ClientTransaction ct=sipProvider.getNewClientTransaction(yourRegisterRequest);ct.sendRequest()工作;