REST API,OAuth2和身份验证错误

时间:2014-03-31 14:34:54

标签: mysql node.js rest oauth-2.0 passport.js

首先,感谢您的回复。 我尝试使用nodeJS创建REST API,我认为,oAuth2是根据我的系统对用户进行身份验证的最佳方式。

用户存储在LDAP中,api密钥将保留在MySQL引擎上。客户帐户代码也将存储在MySQL中。 所有进程都正常,用户已通过身份验证,API令牌已保存在MySQL中,但当我尝试使用此令牌时,护照拒绝验证我的请求并返回:

Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (http.js:691:11)
    at ServerResponse.res.setHeader (/home/lolostates/Developpement/nodejs/oauth2/test/node_modules/express/node_modules/connect/lib/patch.js:62:20)
    at ServerResponse.res.header (/home/lolostates/Developpement/nodejs/oauth2/test/node_modules/express/lib/response.js:280:8)
    at ServerResponse.res.json (/home/lolostates/Developpement/nodejs/oauth2/test/node_modules/express/lib/response.js:135:8)
    at exports.info (/home/lolostates/Developpement/nodejs/oauth2/test/user.js:13:9)
    at callbacks (/home/lolostates/Developpement/nodejs/oauth2/test/node_modules/express/lib/router/index.js:272:11)
    at complete (/home/lolostates/Developpement/nodejs/oauth2/test/node_modules/passport/lib/passport/middleware/authenticate.js:218:13)
    at /home/lolostates/Developpement/nodejs/oauth2/test/node_modules/passport/lib/passport/middleware/authenticate.js:200:15
    at pass (/home/lolostates/Developpement/nodejs/oauth2/test/node_modules/passport/lib/passport/index.js:399:14)
    at Passport.transformAuthInfo (/home/lolostates/Developpement/nodejs/oauth2/test/node_modules/passport/lib/passport/index.js:415:5)

请求由curl发送:

curl -H "Authorization: Bearer NoulKM889Aksf60rQONcUJwMuZHI3PDqzeXfkX3Li2BohsxNVsOrd2LLdvJAGZuE168IukUCPbviazhvBjt7VDfLFUMJRIY1fa95kGXQQKzE7etFhocsnYvbLSixbHRmCwXNx5FKj6v83Ci9f9xLqRinEKwaAUIjs03hhq8dCWIp7S0Cbi5jdkxlzwfpZxuShoAZYaFInlf4ymG5oyzQe0WJ2POXOaMarGLO7NkjyIMJXWh7s0Y" http://localhost:3000/api/userinfo

我使用了passport-http-bearer,oAuth2orize以及OAuth2orize示例中提供的所有示例函数。

你能解释一下为什么吗?

1 个答案:

答案 0 :(得分:0)

您似乎尝试在回复响应后设置响应标头。在尝试设置标题之前,请检查以确保没有调用res.end()(或类似的方法)。