最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Keycloak check-sso create - Stack Overflow

programmeradmin1浏览0评论

I'm using Angular 8.0.3, keycloak 8.0.0 and the keycloak-service 7.0.1

I'm trying to make some public pages, so I'm using 'check-sso'. If I have understood correctly, if the user is not logged-in, the browser will be redirected back to the application and remain unauthenticated.

I have this routing for my app :

const routes: Routes = [
  {
    path: '',
    loadChildren: () => import('src/app/pages/pages.module').then(m => m.PagesModule),
    canLoad: [IsAuthenticateGuard]
  },
  {
    path: 'login',
    ponent: LoginComponent,
    canLoad: [IsNotAuthenticateGuard]
  },
  {
    path: '**',
    redirectTo: ''
  }
]

guards :

export class IsAuthenticateGuard implements CanLoad {

  constructor(private keycloakService: KeycloakService, private router: Router) {}

  canLoad(
    route: Route,
    segments: UrlSegment[]
  ): Observable<boolean>|Promise<boolean>|boolean {
    return this.keycloakService.isLoggedIn().then(isLogged => {
      if (!isLogged) {
        this.router.navigateByUrl('/login');
      }
      return isLogged;
    });
  }
}
export class IsNotAuthenticateGuard implements CanActivate {

  constructor(private keycloakService: KeycloakService,
              private router: Router) {}

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
    Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
    return this.keycloakService.isLoggedIn().then(isLogged => {
      if (isLogged) {
        this.router.navigateByUrl('/home');
      }
      return !isLogged;
    });
  }

}

And I have this init config for my keycloak :

keycloakService.init({
        config: keycloakConfig,
        initOptions: {
          onLoad: 'check-sso',
          checkLoginIframe: false
        },
        bearerExcludedUrls: ['/login'] // (I tried also '.*')
      })

but I'm redirected to :
http://localhost:4200/#error=login_required&state=0b36094c-92ac-4703-a800-77f27e4d206d
and nothing append...

I read this, but not find the solution :

.html#_javascript_adapter

I'm using Angular 8.0.3, keycloak 8.0.0 and the keycloak-service 7.0.1

I'm trying to make some public pages, so I'm using 'check-sso'. If I have understood correctly, if the user is not logged-in, the browser will be redirected back to the application and remain unauthenticated.

I have this routing for my app :

const routes: Routes = [
  {
    path: '',
    loadChildren: () => import('src/app/pages/pages.module').then(m => m.PagesModule),
    canLoad: [IsAuthenticateGuard]
  },
  {
    path: 'login',
    ponent: LoginComponent,
    canLoad: [IsNotAuthenticateGuard]
  },
  {
    path: '**',
    redirectTo: ''
  }
]

guards :

export class IsAuthenticateGuard implements CanLoad {

  constructor(private keycloakService: KeycloakService, private router: Router) {}

  canLoad(
    route: Route,
    segments: UrlSegment[]
  ): Observable<boolean>|Promise<boolean>|boolean {
    return this.keycloakService.isLoggedIn().then(isLogged => {
      if (!isLogged) {
        this.router.navigateByUrl('/login');
      }
      return isLogged;
    });
  }
}
export class IsNotAuthenticateGuard implements CanActivate {

  constructor(private keycloakService: KeycloakService,
              private router: Router) {}

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
    Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
    return this.keycloakService.isLoggedIn().then(isLogged => {
      if (isLogged) {
        this.router.navigateByUrl('/home');
      }
      return !isLogged;
    });
  }

}

And I have this init config for my keycloak :

keycloakService.init({
        config: keycloakConfig,
        initOptions: {
          onLoad: 'check-sso',
          checkLoginIframe: false
        },
        bearerExcludedUrls: ['/login'] // (I tried also '.*')
      })

but I'm redirected to :
http://localhost:4200/#error=login_required&state=0b36094c-92ac-4703-a800-77f27e4d206d
and nothing append...

I read this, but not find the solution :

https://github./mauriciovigolo/keycloak-angular https://www.keycloak/docs/latest/securing_apps/index.html#_javascript_adapter

Share Improve this question asked Dec 11, 2019 at 15:41 Ugo EvolaUgo Evola 6475 gold badges10 silver badges26 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

You are using onLoad: 'check-sso' which only authenticates the client if the user if logged in.

You should use onLoad: 'login-required' instead.

发布评论

评论列表(0)

  1. 暂无评论