使用摘要身份验证对用户名和密码进行身份验证时,在Java脚本中出错
在这里,我想验证用户名和密码。在这里,我在控制台(ValuesUser)中成功地获得了用户名和密码。
我尝试了以下代码,
Ext.define('Form.controller.Digestauth', {
extend: 'Ext.app.Controller',
requires:['RegisterForm.view.Login','RegisterForm.view.Main'],
config:{
refs:{
loginform: '#loginform'
},
control: {
'#loginButton': {
tap: function(){
var valuesUser = this.getLoginform().getValues();
console.log(valuesUser);
console.log(valuesUser.userName);
$.Class("pl.arrowgroup.DigestAuthentication",{
MAX_ATTEMPTS : 1,
AUTHORIZATION_HEADER : "Authorization",
WWW_AUTHENTICATE_HEADER : 'WWW-Authenticate',
NC : "00000001", //currently nc value is fixed it is not incremented
HTTP_METHOD : "GET",
/**
* settings json:
* - onSuccess - on success callback
* - onFailure - on failure callback
* - username - user name
* - password - user password
* - cnonce - client nonce
*/
init : function(settings) {
this.settings = settings;
},
setCredentials: function(username, password){
this.settings.username = valuesUser.username;
this.settings.password = valuesUser.userPassword;
},
call : function(uri){
this.attempts = 0;
this.invokeCall(uri);
},
invokeCall: function(uri,authorizationHeader){
var digestAuth = this;
$.ajax({
url: uri,
uri:'http://localhost/sencha2011/RegisterForm/newuser.php?action=check',
type: this.HTTP_METHOD,
beforeSend: function(request){
if(typeof authorizationHeader != 'undefined'){
request.setRequestHeader(digestAuth.AUTHORIZATION_HEADER, authorizationHeader);
}
},
success: function(response) {
digestAuth.settings.onSuccess(response);
},
error: function(response) {
if(digestAuth.attempts == digestAuth.MAX_ATTEMPTS){
digestAuth.settings.onFailure(response);
return;
}
var paramParser = new pl.arrowgroup.HeaderParamsParser(response.getResponseHeader(digestAuth.WWW_AUTHENTICATE_HEADER));
var nonce = paramParser.getParam("nonce");
var realm = paramParser.getParam("realm");
var qop = paramParser.getParam("qop");
var response = digestAuth.calculateResponse(uri, nonce, realm, qop);
var authorizationHeaderValue = digestAuth.generateAuthorizationHeader(paramParser.headerValue, response, uri);
digestAuth.attempts++;
digestAuth.invokeCall(uri, authorizationHeaderValue);
}
});
},
calculateResponse : function(uri, nonce, realm, qop){
var a2 = this.HTTP_METHOD + ":" + uri;
var a2Md5 = hex_md5(a2);
var a1Md5 = hex_md5(this.settings.username + ":" + realm + ":" + this.settings.password);
var digest = a1Md5 + ":" + nonce + ":" + this.NC + ":" + this.settings.cnonce + ":" + qop + ":" +a2Md5;
return hex_md5(digest);
},
generateAuthorizationHeader : function(wwwAuthenticationHeader, response, uri){
return wwwAuthenticationHeader+', username="'+this.settings.username+'", uri="'+
uri+'", response="'+response+'", nc='+
this.NC+', cnonce="'+this.settings.cnonce+'"';
}
});
$.Class("pl.arrowgroup.HeaderParamsParser",{
init : function(headerValue) {
this.headerValue = headerValue;
this.headerParams = this.headerValue.split(",");
},
getParam: function(paramName){
var paramVal = null;
$.each(this.headerParams, function(index, value){
if(value.indexOf(paramName)>0){
paramVal = value.split(paramName+"=")[1];
paramVal = paramVal.substring(1, paramVal.length-1);
}
});
return paramVal;
}
});
}
}
}
}
});
要编写上述代码,我使用了"http://marcin-michalski.pl/2012/11/01/javascript-digest-authentication-restful-webservice-spring-security-javascript-ajax/"链接摘要-auth.js文件
我收到的错误是:未定义引用错误:$未定义。是否有人可以帮助我完成此操作。提前谢谢
解决方案
需要包含jQuery。它定义了一个全局$
变量,这就是您的错误所抱怨的。可能来自对$.ajax
的调用。
查看此处的入门部分:http://docs.jquery.com/
相关文章