如何添加一个JSON网络令牌每头? [英] How do I add a json web token to each header?

查看:478
本文介绍了如何添加一个JSON网络令牌每头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图用JSON网络令牌身份验证和我在努力试图找出如何将其连接到一个头,并送他们的请求。

我试图用 https://github.com/auth0/angular2-jwt 但我无法得到它与角工作,放弃了,想通我可以弄清楚如何可以送智威汤逊在每一个请求或在头发送(preferably头)。它刚刚有点困难比我想象的那样。

下面是我的登录

  submitLogin(用户名,密码){
        的console.log(用户名);
        的console.log(密码);
        让身体= {用户名,密码};
        this._loginService.authenticate(体).subscribe(
            响应=> {
                的console.log(响应);
                localStorage.setItem(智威汤逊,响应);
                this.router.navigate(['UserList的']);
            }
        );    }

和我login.service

 进行身份验证(form_body){
        返回this.http.post('/登录,JSON.stringify(form_body){标题:标题})
                .MAP((响应= GT; response.json()));
    }

我知道这些都不是真正需要的,但也许它会帮!一旦此令牌被创建和存储我的话,我希望做两件事情,在头发送,并提取了我把这个到期日期。

有些Node.js的登录$ C $的C

  VAR智威汤逊=要求('jsonwebtoken');
功能createToken(用户){
  返回jwt.sign(用户,超级秘密,{expiresIn:60 * 5});
}

现在我只是想通过角服务,该服务将它传递回节点。

  getUsers(JWT){
        headers.append('授权',智威汤逊);
        返回this.http.get('/ API /用户/',{标题:标题})
            .MAP((响应=方式> response.json()的数据));
    }

智威汤逊是我的,我通过我的组件,以服务本地存储webtoken。

我没有得到任何错误,任何地方,但,当它到达我的节点服务器我从来没有收到它的头。

 内容类型:应用/ JSON,
 接受:* / *,
 引用者:HTTP://本地主机:3000 /',
 接受编码':'gzip的,放气,SDCH',
 接受语言:EN-US,EN; Q = 0.8',
 饼干:connect.sid = S%3Alh2I8i7DIugrasdfatcPEEybzK8ZJla92IUvt.aTUQ9U17MBLLfZlEET9E1gXySRQYvjOE157DZuAC15I',
 如果 - 无 - 匹配:W /38B-jS9aafagadfasdhnN17vamSnTYDT6TvQ'}


解决方案

我不是一个节点的家伙。但有一次,我在看的Youtube 视频,我觉得它可能会帮助你,因为它有 智威汤逊的实施不是核心实现,但basic.The GitHub库 他使用可能会帮助您与的Node.js

你管视频的观测基本的了解(这视频有一个基本的JWT执行与NODE API)

您可以直接开始观看在 14:00 ,如果你不喜欢看整个视频。

注:此视频只是理解/学习的目的,但它在 Github上回购这可能会帮助你。搜索结果
祝你好运!

So I am trying to use JSON web tokens for authentication and am struggling trying to figure out how to attach them to a header and send them on a request.

I was trying to use https://github.com/auth0/angular2-jwt but I could not get it working with Angular and gave up, and figured I could just figure out how to either send the JWT in every request or send it in the header(preferably the header). It's just been a little bit harder than I thought it would be.

Here is my Login

submitLogin(username, password){
        console.log(username);
        console.log(password);
        let body = {username, password};
        this._loginService.authenticate(body).subscribe(
            response => {
                console.log(response);
                localStorage.setItem('jwt', response);
                this.router.navigate(['UserList']);
            }
        );

    }

and my login.service

authenticate(form_body){
        return this.http.post('/login', JSON.stringify(form_body), {headers: headers})
                .map((response => response.json()));
    }

I know these are not really needed but maybe it'd help! Once this token gets created and I store it, I would like to do 2 things, send it in the header and extract the expiration date that I put in with this.

Some of the Node.js login code

var jwt = require('jsonwebtoken');
function createToken(user) {
  return jwt.sign(user, "SUPER-SECRET", { expiresIn: 60*5 });
}

Now I am just trying to pass it via an angular service back to node with this service.

getUsers(jwt){
        headers.append('Authorization', jwt);
        return this.http.get('/api/users/', {headers: headers})
            .map((response => response.json().data));
    }

JWT is my webtoken in local storage that I pass through my component to the service.

I get no errors anywhere but when it gets to my node server I never receive it in the header.

'content-type': 'application/json',
 accept: '*/*',
 referer: 'http://localhost:3000/',
 'accept-encoding': 'gzip, deflate, sdch',
 'accept-language': 'en-US,en;q=0.8',
 cookie: 'connect.sid=s%3Alh2I8i7DIugrasdfatcPEEybzK8ZJla92IUvt.aTUQ9U17MBLLfZlEET9E1gXySRQYvjOE157DZuAC15I',
 'if-none-match': 'W/"38b-jS9aafagadfasdhnN17vamSnTYDT6TvQ"' }

解决方案

I'm not a NODE guy. But once, I was watching Youtube video and I feel like it might help you as it has JWT implementation not core implementation but basic.The Github Repo which he uses might help you with NODE.js.

You Tube Video for basic understanding of observables (This video has a basic implementation of JWT with NODE Api)

You can directly start watching at 14:00 if you don't like to see whole video.

Note: This video is just for understanding/learning purpose but It has a repo at Github which might help you.

Good Luck !

这篇关于如何添加一个JSON网络令牌每头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆