Angular中无效的url应该在重定向之前检查某些情况

编程入门 行业动态 更新时间:2024-10-24 11:13:01
本文介绍了Angular中无效的url应该在重定向之前检查某些情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的初始域是localhost:4200,它根据路由将我重定向到某个页面,但是我想输入一个无效域,例如localhost:4200/Whasup或localhost:4200/Whasdown,其中Whasup和whasdown页面不存在.在重定向到默认的Angular 404页面之前,我需要检查一些条件.如果我的条件不满足,那么我想将其重定向到404页面,否则我想处理它.

My initial domain is localhost:4200 and which in redirects me to some page, based on my routing, but I want to input an invalid domain for example localhost:4200/Whasup or localhost:4200/Whasdown, where Whasup and whasdown page doesn't exist. Before i get redirected to default Angular 404 page, I need to check some conditions. And if my condition doesn't satisfy then i want to redirect it to 404 page else i want to handle it.

我的app-routing.module.ts(Auth Guard检查用户是否已登录)

my app-routing.module.ts (Auth Guard Checks if the user is logged in or Not)

const routes: Routes = [ // Fallback when no prior route is matched // Shell.childRoutes([]), // { path:'home', component: HomeComponent}, // { path: '**', redirectTo: '', pathMatch: 'full' } Shell.childRoutes([ { path: '', component: RedirectToComponent, canActivate: [AuthGuard] }, { path: 'dashboard', loadChildren: 'src/app/dashboard/dashboard.module#DashboardModule', canActivate: [AuthGuard] }, { path: 'exec-dashboard', loadChildren: 'src/app/exec-dashboard/exec-dashboard.module#ExecDashboardModule', canActivate: [AuthGuard] }, { path: '**', redirectTo: '', canActivate: [AuthGuard] } ]), { path: 'auth', loadChildren: 'src/app/auth/auth.module#AuthModule', canActivate: [AuthGuard] } ]; @NgModule({ imports: [ RouterModule.forRoot(routes, { enableTracing: false, // Enable for debug purpose. useHash: true, preloadingStrategy: PreloadAllModules }) ], exports: [RouterModule], providers: [AuthGuard] })

我还实现了KeyCloak Angular拦截器,如果用户未登录,它可以帮助我进入keycloak登录页面.我在app.module.ts中定义了提供程序.

I have also implemented KeyCloak Angular interceptor which helps me get to the keycloak login page, if user is not logged. I have providers defined in app.module.ts.

providers: [ { provide: APP_INITIALIZER, useFactory: initializer, multi: true, deps: [KeycloakService] } ],

因此,每当我访问初始域(localhost:4200)时,都会调用初始化器函数,然后重定向到任何页面,无效子域url(localhost:4200/Whasup)也会发生同样的情况.我可以任意实现上述功能,也可以按角度实现子域逻辑,例如tenant1.localhost:4200或tenant2.localhost:4200(部署时为tenant1.mycompany).有人可以建议我一种以角度实现子域路由的方法.非常感谢.

So whenever I hit my initial domain(localhost:4200), intializer function gets called before i get redirected to any page, same should happen with invalid sub-domain url(localhost:4200/Whasup). Can i implement above mentioned by any chance, or can i implement sub domain logic in angular, for example, tenant1.localhost:4200 or tenant2.localhost:4200 , (when deployed it would be tenant1.mycompany). Can someone suggest me a way to implement sub domain routing in angular. Thanks Much.

推荐答案

我将在此处发布,因为我的解释不适合评论.我不确定这是否一定会回答您的问题,因为有很多事情需要考虑.所以就这样...

I will post here as my explanation will not fit in a comment. I'm not sure if this will necessarily answer your question as there are a number of things to consider. So here it goes...

注意

如果您希望应用程序与子域一起使用,则不能将 localhost 与子域(例如 tenantName.localhost:4200 )一起用于本地测试,则必须使用hosts文件设置自定义域.您可以在此处看到一个示例.我个人更喜欢使用/tenantname 而不是子域,但这是个人喜好,我想取决于您的业务需求.

If you want your application to work with subdomains you cannot use localhost together with subdomains (like tenantName.localhost:4200) to test locally, you'll have to setup custom domains using the hosts file. You can see an example of this here. Personally I prefer having a /tenantname rather than a subdomain, but that's personal preference and based on your business requirements I guess.

处理重定向

无论使用子域还是将客户端名称作为URL的一部分,您都应该能够在用户登录后 处理到租户URL"的自动重定向".假设成功登录后作为响应的一部分,您将拥有租户名称.有了租户名称后,您可以轻松地将用户重定向到正确的URL.

Whether working with subdomains or the client name as part of the URL, you should be able to handle the "automatic redirection" to the "tenant URL" after the user is logged in. I am assuming that after a successful login as part of the response you will have the tenant name. Once you have the tenant name you can easily redirect the user to the correct URL.

我之所以提及这一点,是因为在您的最后一条评论中,您指出在应用程序启动时您要重定向到特定于租户"的URL.重要:如果没有登录用户,您将无法知道重定向到哪个URL.

I am mentioning this because in your last comment you stated that on application start you want to redirect to a "tenant specific" URL. Important: Without the user being logged in you cannot know which URL to redirect to.

现在,如果用户已登录并且可以访问该网站?在 AuthGuard 中,您可以有一个条件来检查用户是否已登录,如果是,则将用户重定向到正确的租户URL.同样,这是我假设作为登录响应的一部分,您具有一些信息,使您可以了解用户所属的租户.

Now if the user is logged in and they access the website? Well in the AuthGuard you can have a condition that checks if the user is logged in, and if yes redirect the user to the correct tenant URL. Again this is me assuming that as part of the login response you have some information that allows you to know the tenant that the user belongs to.

简而言之,我建议:

  • 确保成功进行身份验证的响应返回有关租户的一些信息.
  • 使用身份验证响应中的租户信息,您可以处理将用户从Angular应用重定向到正确的租户URL.
  • 还请记住,当用户尝试访问他们无权访问的租户的URL时,您需要处理一些情况.

更多推荐

Angular中无效的url应该在重定向之前检查某些情况

本文发布于:2023-06-09 04:12:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/594990.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   情况   Angular   url

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!