【问题标题】:Angular 6: How to protect route for account activation linkAngular 6:如何保护帐户激活链接的路由
【发布时间】:2019-03-06 04:19:20
【问题描述】:

我有一个从后端生成到电子邮件的链接:

例如:localhost/register/profile?id=39&token=IGCHSXtSzlHzGFsZPt

当用户点击链接时,用户将重定向到个人资料页面进行编辑。在重定向到页面之前,我需要验证令牌是否存在于数据库中。

我有 api 来验证令牌:

例如: localhost/registration/verifyConfirmationToken?id=39&token=IGCHSXtSzlHzGFsZPt

verifyToken(id:string, token: string) {
    const params = new HttpParams().set('a', id).set('b', token);

    this.base_url = environment.MYAPI_REST_API_URL+"/registration/application/verifyConfirmationToken?a="+id+"&b="+token;
            return this.http.get(this.base_url)
            .pipe(map(user => {
                //return null if token not exist
                return user;
            }));
    }

token 存在时的响应(如果 token 不存在则返回 null):

{
"statusId": 4,
"createdBy": 0,
"creationTime": "2018-09-27 22:08:32",
"updatedBy": 0,
"updateTime": "2018-09-28 00:46:30",
"id": 39,
"email": "test@gmail.com",
"address": "noods",
"city": "bahyd",
"postcode": "70324324",
"active": true
}

问题是如果我将链接放入随机令牌,用户仍然可以查看个人资料页面。我怎样才能防止这种情况发生?

【问题讨论】:

    标签: angular angular5 angular6


    【解决方案1】:

    你可以看看 CanActivate 守卫:https://angular.io/api/router/CanActivate

    【讨论】:

      猜你喜欢
      • 2019-12-15
      • 2020-03-27
      • 1970-01-01
      • 2013-01-15
      • 2017-10-30
      • 2014-09-13
      • 2018-02-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多