角色保护有时允许进入localhost上的安全组件


老夫的少女心吖
2025-03-11 06:19:13 (28天前)


我的RoleGuard看起来像这样:

从“@ angular / router”导入{CanLoad,Route};
从“../_services”导入{AuthenticationService};
从@ @ angular / core导入{Injectable};

@Injectable({…

2 条回复
  1. 0# 石茶 | 2019-08-31 10-32



    问题可能在于

    CanLoad



    CanLoad

    盖尔德保护了一个

    module

    加载但一次

    module

    然后加载

    CanLoad

    后卫什么也没做。



    例如,假设用户登录了应用程序并导航到某个模块。之后他点击退出。现在,如果用户想要,他将能够导航到相同的模块,因为它已经加载。



    因此,如果您想保护您的应用程序,最好使用

    CanActivate







    CanActivate

    进入你的RoleGaurd





    1. import { CanLoad, CanActivate, Route,Router,
      ActivatedRouteSnapshot, RouterStateSnapshot } from @angular/router’;
      import { AuthenticationService } from “../_services”;
      import { Injectable } from @angular/core”;

    2. @Injectable({ providedIn: root })
      export class RoleGuard implements CanLoad, CanActivate {

    3. constructor(private authService: AuthenticationService,private router: Router) { }
    4. canLoad(route: Route) {
    5.     let authorities = route.data.roles;
    6.     if (this.authService.hasAnyRole(authorities)) {
    7.         return true;
    8.     }
    9.     return false;
    10. }
    11. canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
      let authorities = route.data.roles;
      if (this.authService.hasAnyRole(authorities)) {
      return true;
      }
      return false;
      }

    12. }

    13. </code>

登录 后才能参与评论