简体   繁体   English

如何使用Express为Android应用程序在node.js中实现服务器端会话?

[英]How to implement server side sessions in node.js with express for an android app?

Hello all i am making an android app in whiich i have multiple account login at a time now my question is that i for multiple logins i should use sessions to verify every account user that is logged in. Now i am using express on the server side i have read a lot of documentation on storing sessions in node.js 您好,我正在制作一个android应用,一次我有多个帐户登录,我的问题是我要进行多个登录,我应该使用会话来验证每个登录的帐户用户。现在,我在服务器端使用express我已经阅读了很多有关在session.js中存储会话的文档

  1. Express-session (Though it is only good for development but not for production but not for my app) Express会话(尽管这仅对开发有利,但对生产不利,对我的应用程序不利)
  2. Cookie-session Cookie的会话
  3. connect-Redis 连接-的Redis
  4. connect-mongo 连接 - 蒙戈

I have also heard about json web tokens where i can generate unique tokens and then i can pass the tokens to the client using res.json({user_id:"user1", token: "generated_token here"}) I have also heard about passport but dont know how it is going to do this also as in passport i use express-session by default will it be good for production or not ?? 我也听说过json网络令牌,我可以在其中生成唯一的令牌,然后可以使用res.json({user_id:“ user1”,令牌:“ generated_token here”}}将令牌传递给客户端,我也听说过护照但是不知道怎么做, 因为在护照上我默认使用express-session会不会对生产有好处?

Now my first question is i have read all of there docs and nowhere it is mentioned where i am creating unique tokens for every user that is signing up. 现在,我的第一个问题是我已经阅读了所有文档,却没有提到我在为每个注册用户创建唯一令牌的位置。 Second question as i am using my server for android app there will be no use of cookie i will be sending user token as in parameter req.body.token now how to cmpare this with current user_id. 第二个问题,因为我将服务器用于android应用程序,因此不会使用cookie,我将按照参数req.body.token发送用户令牌,现在该如何将其与当前user_id进行比较。

Actually i dont get the flow of control i mean how everything is going on in session in node.js. 实际上,我没有控制流,我的意思是在node.js的会话中所有事情都是如何进行的。 Also what is this secret is this thing generating unique tokens or what. 同样,这个秘密是产生唯一令牌的东西。 Also i mean about 100000 of users are registered for my app now please tell me accordingly which way should i use for my app. 另外,我的意思是现在大约有100000个用户注册了我的应用程序,请相应地告诉我应该使用哪种方式使用我的应用程序。

I have asked this question previously but there i did not mention that as i am not making a website how to do this(As in my case there will be no use of tokens) 我之前曾问过这个问题,但我没有提到,因为我没有建立网站的操作方法(就我而言,将不会使用令牌)

I know this question i am asking is very vague but please bear with me i just want to understand how sessions are used in node.js 我知道我要问的这个问题非常模糊,但请忍受我只是想了解如何在node.js中使用会话

Thanks Anways 感谢Anways

I'll try to answer this, but it is vague (as you pointed out). 我会尽力回答这个问题,但这很模糊(如您所指出的)。 I'm going to make an assumption that your Android app is a native Android app and is going to be connecting to some sort of NodeJS backend in the cloud that is based on ExpressJS. 我将假设您的Android应用程序是本机Android应用程序,并将连接到基于ExpressJS的云中的某种NodeJS后端。 If that's not the case, please clarify your thoughts in an update to your question. 如果不是这种情况,请在更新您的问题时阐明您的想法。

The best idea for this specific scenario is to look to the cloud provide. 对于这种特定情况,最好的主意是看云提供的服务。 Azure App Service Mobile Apps, for example, allows you to implement authentication - it eventually returns a JSON Web Token ( http://jwt.io ) to authenticate each request. 例如,Azure App Service移动应用程序允许您实施身份验证-它最终返回JSON Web令牌( http://jwt.io )以对每个请求进行身份验证。

If you don't want to be beholden to a cloud provider, but want to run it yourself, you are going to have to implement the token generation and checking yourself. 如果您不想迷恋云提供商,而是想自己运行它,则必须实现令牌生成并检查自己。 This generally follows the form: 通常采用以下形式:

  • Set up a WebAPI endpoint (maybe /signin) which takes whatever token the identity provider gives you, verifies the information and returns a JWT - there is an NPM module (jsonwebtoken) for producing the JWT. 设置一个WebAPI端点(可能是/ signin),该端点将使用身份提供者提供给您的任何令牌,验证信息并返回JWT-有一个用于生成JWT的NPM模块(jsonwebtoken)。 Ensure the JWT includes the identity of your user. 确保JWT包含您的用户身份。 I tend to use email address for the identity. 我倾向于使用电子邮件地址作为身份。
  • Your Android application will do a WebAPI request to your backend with an Authorization header, the value of which is "Bearer " 您的Android应用程序将使用授权标头向您的后端发出WebAPI请求,该标头的值为“ Bearer”
  • Your NodeJS API will use JWT authorization to validate the JWT and extract the user identity so you can use it in your API logic. 您的NodeJS API将使用JWT授权来验证JWT并提取用户身份,以便您可以在API逻辑中使用它。

The important thing to note in this specific scenario is that your backend code is implementing a WebAPI - there are no cookies nor sessions in the API. 在此特定情况下要注意的重要一点是,您的后端代码正在实现WebAPI-API中没有cookie或会话。 The only thing that is linking the user from the client code to the backend code is the JWT. 将用户从客户端代码链接到后端代码的唯一方法是JWT。

As a short piece of code, here is how you verify a JWT: 作为一小段代码,这是验证JWT的方法:

var express = require('express');
var app = express();
var jwt = require('express-jwt');

var jwtCheck = jwt({
  secret: new Buffer('your-jwt-secret', 'base64'),
  audience: 'your-jwt-audience'
});

app.get('/api/protected', jwtCheck, (req, res) => {
// Your code here
});

app.listen(process.env.PORT || 3000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在node.js中实现没有express server的Android Deeplink - How to implement Android Deeplink without express server in node.js 如何使用android应用和节点服务器实现用户会话? - How can I implement user sessions with android app and node server? Appfog上的Node.js Express服务器:在android上使用Chrome时,页面刷新时丢失的会话 - Node.js express server on appfog: sessions lost on page refresh when using chrome on android Java Android应用程序和Node.js + Express.js API - Java Android app and Node.js + Express.js API 如何从Node.js服务器向Android应用发送消息? - How to send message from node.js server to Android app? 从Android应用发送整数流并在Node.js Express服务器上接收它 - Send a stream of integers from Android app and receive it on Node.js express server TLS-Node.js服务器到Android应用 - TLS - Node.js server to Android app 如何使用node.js从目录获取图像并将其发送到客户端以在Android应用程序上显示 - How to get image from directory with node.js and send it to the client side to display on android app Android 应用程序与 Node.Js 服务器和 MongoDB 通信 - Android app communicate with Node.Js server and MongoDB 如何将 node.js 应用程序变成 android 应用程序? - How to turn node.js app into android app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM