我已经看到,可以通过添加此内容来更改Jhipster登录页面,
registerAuthenticationSuccess() {
this.eventManager.subscribe('authenticationSuccess', (message) => {
this.principal.identity().then((account) => {
if (account.authorities.indexOf('ROLE_CONTRACTOR') >=0)
{
this.router.navigate(['/property']);
}
else
{
this.account = account;
}
});
});
}
在home.component.ts中,并在onInit方法中将其调用为
this.principal.identity().then((account) => {
this.account = account;
});
this.registerAuthenticationSuccess();
但是,就我而言,这不起作用。在我的主页中,我仅定义了管理员权限,就可以访问如下所示的route.ts文件中的主页,
export const HOME_ROUTE: Route = { path: '', component: HomeComponent, data: { authorities: ['ROLE_ADMIN'], pageTitle: 'home.title' }, canActivate: [UserRouteAccessService]};
但是,当我以承包商身份登录时,我被重定向到登录页面,说我没有访问主页的权限。它不会重定向到属性页。
答案 0 :(得分:1)
我想,在Jhipster中,一旦我们登录,默认情况下它将进入HomeComponent。在您的情况下,它失败了,因为只有ROLE_ADMIN可以访问HomeComponent。因此,显然,它将不允许具有ROLE_CONTRACTOR的用户登录,它将在调用您编写用于路由器导航的代码的方法之前重定向到未经授权的页面。
因此,请尝试从路由路径中删除权限:['ROLE_ADMIN'],这样它将允许所有用户访问HomeComponent,并且如果用户使用ROLE_CONTRACTOR,它将重定向到属性页。