Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用AngularJS和JWT的登录流_Angularjs_Http Headers_Jwt - Fatal编程技术网

使用AngularJS和JWT的登录流

使用AngularJS和JWT的登录流,angularjs,http-headers,jwt,Angularjs,Http Headers,Jwt,在我的webapp中,当未登录用户请求主页时,我会显示一个登录页面(仅包含一个登录表单)。然后用户输入其userid和pwd,AngularJS控制器向/connection发出HTTP POST请求,在接受凭据的情况下返回JWT AUTITIfication令牌 然后我希望AngularJS控制器在标题中发出一个GET on/passing令牌,这样下一个加载的页面就是连接的用户可以开始玩的主webapp页面 我怎样才能用AngularJS做到这一点。我应该使用$document.locati

在我的webapp中,当未登录用户请求主页时,我会显示一个登录页面(仅包含一个登录表单)。然后用户输入其userid和pwd,AngularJS控制器向/connection发出HTTP POST请求,在接受凭据的情况下返回JWT AUTITIfication令牌

然后我希望AngularJS控制器在标题中发出一个GET on/passing令牌,这样下一个加载的页面就是连接的用户可以开始玩的主webapp页面

我怎样才能用AngularJS做到这一点。我应该使用
$document.location.href
?如果是,如何将令牌设置到HTTP头中


谢谢

您可以通过两种方式完成:

1-对所有发起的请求使用自定义头(不管get、post等),在这里,我们的朋友发布了许多不同的方法

2-覆盖请求的标头,如下所示:

$http.get(/*The Call URL*/'http://myawesomeurl/jsondata',/*Configuration Object*/
{/*Overriding the header of the request*/
    headers: {
        'Content-Type': 'application/json',//request content type
        'Authorization': 'token d2VudHdvcnRobWFuOkNoYW5nZV9tZQ=='//Token parameter
        //,'myAwesomeHeaderParam':'the header is all yours, add or remove anything'
    }
})
.success(function (data, status, headers, config) {  
    console.log(data);//Received data
})
.error(function (data, status, headers, config) {
    console.log('Error Status:'+status);
    console.log(data);//Any Server Response values in the case of error
});

//Another Way for initiating the request just for your info
/* var reqInfo = {
        method: 'GET',//POST, PUT
        url: 'http://myawesomeurl/jsondata',
        headers: {
            'Content-Type': 'application/json',//request content type (can be not used)
            'Authorization': 'token d2VudHdvcnRobWFuOkNoYW5nZV9tZQ=='//Token parameter
        }
//,data:{'myAwesomeParameter':'in case of post or put, etc..'} //Request body 
    };
$http(reqInfo).success(....*/

您可以了解更多关于

的信息,谢谢,但这是“AJAX”风格,意味着您将停留在同一页面上。不我需要做的是请求另一个完整的页面,如DOM document.location.href。在客户端,您不需要特殊的标题来导航到另一个路由/url,因为您没有执行http请求,您正在操作DOM本身,因此您可以将令牌保存在内存中(rootScope、localStorage、angular service等)一旦用户登录并在路由之前执行if检查,例如:
if(JSON.stringify(localStorage.getItem('your_saved_token'))!==“null”))document.location.href='yoursecuredpageurl';else document.location.href='yourloginpageurl'谢谢Mustafa。请求页面“yoursecuredpageurl”时是否会包含jet令牌?如果令牌是url的一部分,则为“是”,否则为“否”?不…,因为您没有启动请求只有两种方式可以导航到“yoursecuredpageurl”,1-从服务器端,在本例中,您将仅使用令牌启动请求,然后服务器将执行重定向2-或者您在客户端执行重定向,如前所述,在本例中,您不需要启动请求,这意味着您只需要在客户端使用令牌,当客户端登录时,您已经拥有它。但是在执行
document.location.href='yoursecuredpageurl'时,不需要令牌因为您没有启动请求