0

我正在尝试使用 java 8、spring boot、Spring MVC 和带有 angular cli 的前端来修复 Web 应用程序中的错误。当用户登录应用程序并创建一个考虑用户配置文件权限的菜单时,使用 java,但应用程序使用具有静态路径的角度路由器,因此如果用户重写 URL,即使没有权限,他也可以访问任何内容。

const routes: Routes = [
  {
      path: '',
      component: WebservicesComponent,
      children: [
        { path: 'perfis', loadChildren: './wsperfis/wsperfis.module#WsperfisModule', },
        { path: 'acessos', loadChildren: './wsacessos/wsacessos.module#WsacessosModule', },
        { path: 'novoAcesso', loadChildren: './novo-acesso/novo-acesso.module#NovoAcessoModule', },
        { path: 'servicos', loadChildren: './wsservicos/wsservicos.module#WsservicosModule' },
        { path: 'novoperfil', loadChildren: './wsnovoperfil/wsnovoperfil.module#WsnovoperfilModule' }
      ]
  }
];


@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class WebservicesRoutingModule {
}

@CrossOrigin
    @RequestMapping("/menu")
    public List<Object> menu(@RequestParam(value = "idPerfil") int idPerfil) {

        List<Menu> menus = menuService.getMenus(idPerfil);

        List<Object> menu = new ArrayList<Object>();

        Map<String, Object> mapMenu = new HashMap<String, Object>();
        Map<String, String> mapSubMenu = new HashMap<String, String>();
        List<Object> listMapSubMenu = new ArrayList<Object>();

        for (Menu menuItem : menus) {

            if (!mapMenu.containsValue(menuItem.getPaiPrompt())) {

                mapMenu = new HashMap<String, Object>();
                listMapSubMenu = new ArrayList<Object>();

                mapMenu.put(LABEL, menuItem.getPaiPrompt());
                mapMenu.put(URL, menuItem.getPaiUrl());
                mapMenu.put(ICON, menuItem.getPaiIcon());

                for (Menu submenu : menus) {

                    if (menuItem.getPaiPrompt().equals(submenu.getPaiPrompt())) {
                        mapSubMenu = new HashMap<String, String>();
                        mapSubMenu.put(LABEL, submenu.getFilhoPrompt());
                        mapSubMenu.put(URL, submenu.getFilhoUrl());
                        mapSubMenu.put(ICON, submenu.getFilhoIcon());
                        listMapSubMenu.add(mapSubMenu);
                    }

                }
                mapMenu.put(ITEMS, listMapSubMenu);
                menu.add(mapMenu);
            }

        }

        return menu;
    }
4

2 回答 2

0

您应该在前端和后端添加验证,例如,当前端和组件中的路径更改时,它会检查会话是否发送其路径 ID,后端将其与指定的菜单进行比较,所有这些在进行任何其他 api 调用之前。

另一个更复杂(和安全)的解决方案是通过检查菜单或用户配置文件在 api 本身上添加验证,这样即使用户访问他不应该访问的页面(它在 js 中映射),他也不会访问未经授权的 api。

于 2019-10-29T17:37:13.917 回答
0

我可以使用 Guard 文件中的 canActiveChild 验证来做到这一点,但现在我第一次调用它时遇到了问题。那里的呼叫在我第一次调用它时保持待处理状态,但在下一次调用中它工作正常。放松代码:

constructor(private router: Router, private _cookieService: CookieService, private comumService: ComumService) {}

canActivate() {
    if (this._cookieService.get('AuthorizationToken')) {
        return true;
    }
    this.router.navigate(['login']);
    return false;
}

canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
    console.log('state.url: ' + state.url);
    // tslint:disable-next-line:triple-equals
    if (state.url == '/dashboard' || this.validaAcesso(state.url)) {
        return true;
    } else {
        console.log('Entrou aqui!!!');
        window.alert('You don\'t have permission to view this page');
        this.router.navigate(['dashboard']);
        return false;
    }
}

validaAcesso(url: string) {
    this._cookieService.getAll();
    this.comumService.validaAcesso(url).subscribe((data: Boolean) => {
        console.log(data.valueOf());
        if (data.valueOf()) {
            console.log('validaAcesso return true');
            this.result = true;
        } else {
            console.log('validaAcesso return false');
            this.result = false;
        }
    });
    return this.result;
}

}

于 2019-11-04T21:29:08.927 回答