首頁 > web前端 > js教程 > 用均值堆棧的用戶身份驗證

用均值堆棧的用戶身份驗證

Jennifer Aniston
發布: 2025-02-15 11:35:12
原創
427 人瀏覽過

>本文探討了均值堆棧應用程序中的用戶身份驗證,採用通用體系結構:與node.js,express.js和基於mongodb的and appi相互作用的角度單頁應用程序。 我們將介紹安全用戶管理的關鍵方面。

User Authentication with the MEAN Stack

核心身份驗證挑戰:

這個過程需要解決幾個關鍵點:

  1. 用戶註冊。
  2. 安全的數據存儲(密碼從未直接存儲)。 >
  3. >用戶登錄。
  4. >在頁面訪問中維護主動用戶會話。
  5. >僅限制對身份驗證的用戶對特定頁面的訪問。 >
  6. >基於登錄狀態動態調整用戶界面(例如,顯示“登錄”或“我的個人資料”按鈕)。
高級身份驗證概述:

>在進行代碼之前,讓我們檢查高級身份驗證流:>

數據存儲:
    用戶數據,包括哈希密碼,位於mongodb中。 api(express.js):客戶端互動(Angular):> jwts: jwts,更換傳統cookie,是依靠靜止API的單頁應用程序的理想選擇。 它們在成功的註冊或登錄時生成。 >
  • >會話管理:> 使用Node.js's
  • 模塊將密碼
  • 安全:密碼。 Passport.js,在Express中實現身份驗證策略(特別是用於用戶名/密碼驗證的本地策略)。
  • >示例應用程序結構: 完整的代碼可在GitHub上找到。 先決條件包括Node.js,MongoDB和Angular Cli。 crypto
  • Angular應用程序:
>

Angular應用具有四個基本頁面:User Authentication with the MEAN Stack

主頁 寄存器

登錄

profile(僅登錄用戶僅訪問)

  1. > REST API(node.js,express.js,mongodb):
  2. API包含三個核心路線:
    1. /api/register(post):用戶註冊。
    2. >
    3. >/api/login(post):用戶登錄。
    4. >/api/profile/:USERID>(get):檢索用戶配置文件詳細信息(受保護)。
    5. >

    > mongodb schema(mongoose):>

    中的簡單用戶架構定義/api/models/users.jsemailnamehash>字段。 salt字段是唯一的。 email

    var userSchema = new mongoose.Schema({
      email: { type: String, unique: true, required: true },
      name: { type: String, required: true },
      hash: String,
      salt: String
    });
    登入後複製

    密碼哈希和鹽鹽:

    setPassword>方法,利用node.js的validPassword模塊,處理安全密碼管理而無需直接存儲密碼。 crypto>

    userSchema.methods.setPassword = function(password) {
      this.salt = crypto.randomBytes(16).toString('hex');
      this.hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64, 'sha512').toString('hex');
    };
    
    userSchema.methods.validPassword = function(password) {
      var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64, 'sha512').toString('hex');
      return this.hash === hash;
    };
    登入後複製

    jwt生成:

    方法使用

    >模塊來創建JWT。 generateJwt記住,請記住將您的秘密安全地存儲為環境變量,而不是直接在代碼中。 jsonwebtoken PASSPORT.JS配置:

    Passport.js
    userSchema.methods.generateJwt = function() {
      var expiry = new Date();
      expiry.setDate(expiry.getDate() + 7);
    
      return jwt.sign({
        _id: this._id,
        email: this.email,
        name: this.name,
        exp: parseInt(expiry.getTime() / 1000),
      }, "MY_SECRET");
    };
    登入後複製
    passport.js簡化了Express中的身份驗證。

    文件定義了本地策略:

    api端點和身份驗證:/api/config/passport.js

    >
    passport.use(new LocalStrategy({ usernameField: 'email' }, function(username, password, done) {
      User.findOne({ email: username }, function(err, user) {
        // ... (error handling and password verification logic) ...
      });
    }));
    登入後複製

    文件定義了API路由,包括使用的JWT身份驗證的中間件:

    /api/routes/index.js角身份驗證服務:express-jwt

    > Angular Service(
    var auth = jwt({ secret: 'MY_SECRET', userProperty: 'payload' });
    router.get('/profile', auth, ctrlProfile.profileRead);
    登入後複製
    )管理JWT存儲(LocalStorage),檢索,刪除,API調用,登錄狀態檢查和用戶詳細信息從JWT。

    角路線保護:

    > Angular Routar Guard(

    )保護authentication.service.ts路線,確保只登錄的用戶才能訪問它。

    結論: 本綜合指南詳細說明了在平均堆棧應用程序中構建安全身份驗證系統。 切記始終優先考慮安全密碼處理和JWT管理。 提供的示例為在您自己的項目中實現強大的用戶身份驗證提供了堅實的基礎。

    >

    常見問題(常見問題解答):auth-guard.service.ts(原始常見問題解答已經非常全面且寫得很好。我不會在這裡重複這些問題,因為加上它們會使他們的回答過長。)/profile

以上是用均值堆棧的用戶身份驗證的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板