简体   繁体   English

基于角度角色保护的客户端问题保护

[英]Angular Role Based Protection from Client Side Question

I am new to angular and have been googling about this but haven't really found anything helpful (probably my google terms I'm using). 我刚接触angular,并一直在谷歌上搜索,但是并没有真正找到任何帮助(可能是我使用的Google术语)。

In a traditional server side application, you can generate a page which has different controls/functions/layout/etc based on the authenticated user. 在传统的服务器端应用程序中,您可以基于身份验证的用户生成一个页面,该页面具有不同的控件/功能/布局/等。 Because this is all done on the server side, two different users (who see two different things) have no idea what the other user might see. 因为这都是在服务器端完成的,所以两个不同的用户(他们看到两个不同的东西)不知道另一个用户可能会看到什么。 Since it is all server side, no way to run fiddler or some other tool to extract all the typescript/javascript/html/etc. 由于都是服务器端,因此无法运行提琴手或其他工具来提取所有typescript / javascript / html / etc。

In angular, lets say you have routes for roles ClientRole => /pages/1, /pages/2 AdminRole => /pages/1, /pages/2, /pages/admin/1, /pages/admin/2 用角表示,假设您具有角色的路由ClientRole => / pages / 1,/ pages / 2 AdminRole => / pages / 1,/ pages / 2,/ pages / admin / 1,/ pages / admin / 2

For the routes, I am guessing you would just send a new route list through an observable/ngrx store anytime the user auth changed to update the list of routes (thus preventing someone from going through the "routes file" to see there is a "/page/admin" route defined if they aren't in a role that has that route)? 对于路由,我猜想您会在用户身份更改时随时通过observable / ngrx存储发送新的路由列表,以更新路由列表(从而防止某人通过“路由文件”查看是否存在“ / page / admin”路由是否已定义(如果他们没有该路由的角色)?

How could this work with components? 如何使用组件? So that if someone were to try to reverse engineer the client side components, they wouldn't even have the "admin components" on the client side unless they were logged in and in the admin role? 这样,如果有人要对客户端组件进行逆向工程,那么除非他们以管理员身份登录,否则他们甚至在客户端都没有“管理组件”?

Let your jwt token contain something like level that can be either "user" or "admin". 让您的jwt令牌包含可以作为“用户”或“管理员”之类的level Once they're logged in, you can call isLoggedIn() in your guard service. 他们登录后,可以在guard服务中调用isLoggedIn()

So in your auth.service.ts : 所以在您的auth.service.ts

private isLoggedInAs = new BehaviorSubject<any>('');
isLoggedIn = this.isLoggedInAs.asObservable();

// check if user is logged in, and if so with what level
public isLoggedIn() {
    // insert relevant filters that will return false
    const jwtpayload = jwt.decode(this.getToken());
    this.updateLoggedIn(jwtpayload.level);
    return true;
}

updateLoggedIn(level: string) {
  this.isLoggedInSource.next(level);
}

And in your guard.service.ts : 在您的guard.service.ts

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    if (this.authService.isLoggedIn()) {
        this.router.navigate(['/pages']);
        return false;
    } else {
        return true;
    }
}

And wherever you need to know their level, you can do something like this: 在需要了解其级别的任何地方,都可以执行以下操作:

ngOnInit() {
    this.authService.isLoggedIn.subscribe(isLoggedIn => {
        if (isLoggedIn) {
            this.isLoggedIn = isLoggedIn;
        } else {
            this.isLoggedIn = 'anonymous';
        }
    });
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM