간단한 AuthGuard가 구성되어있어 응용 프로그램에서 "정상적으로"탐색 할 때 완벽하게 작동합니다 (아래 코드 참조).Angular2 : 브라우저 탐색 버튼을 사용할 때 AuthGuard가 작동하지 않습니다.
이제 다음과 같은 상상 : => 그는에 /authentication/login
때문에 checkLogin
의 리디렉션되고 인증을 요구하는
사용자가 /content/secured-content
로 이동 => 그는 성공적으로 인증하기 때문에 /content/secured-content
다시 => 자신이 클릭 리디렉션됩니다 " 로그 아웃 "버튼을 클릭하면 성공적으로 로그 아웃됩니다 (checkLogin
은 이제 false를 반환합니다).
은 이제 중요한 물건은 : 사용자가 현재 보안 콘텐츠 페이지 (브라우저의 "뒤로"버튼)로 다시 이동할 때, canActivate
도 canActivateChild
도 canLoad
가 호출되고 있지 둘과 라우터가 행복하게 보안 컨텐츠를 표시합니다. 보안 컨텐트 자체는 로그 아웃시 폐기 된 세션에 의존하므로 보안이 유지되지만 사용자가 다시 /authentication/login
페이지로 리디렉션되어 해당 동작이 정직해야합니다.
추론에서 오류가 발생한 위치와 내 문제에 대한 적절한 해결책이 있는지 알려주실 수 있습니까?
첨부
경로 구성 조각 :
{
path: 'secured-content',
component: SecureComponent,
canLoad: [ AuthGuard ]
}
인증-guard.service.ts :
import { Injectable } from '@angular/core'
import { CanActivate, CanActivateChild, CanLoad,
Router, ActivatedRouteSnapshot, RouterStateSnapshot, Route
} from '@angular/router'
import { AuthenticationService } from 'app/authentication/authentication.service'
@Injectable()
export class AuthGuard implements CanActivate, CanActivateChild, CanLoad {
constructor(
private authService: AuthenticationService,
private router: Router
) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
if (this.checkLogin(state.url)) {
return true
}
return false
}
canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
return this.canActivate(route, state)
}
canLoad(route: Route): boolean {
const url = `/${route.path}`
return this.checkLogin(url)
}
private checkLogin(url: string): boolean {
if (this.authService.isAuthenticated()) {
return true
}
this.authService.redirectUrl = url
this.router.navigate([ '/authentication/login' ])
return false
}
}
NG의 --version :
@angular/cli: 1.0.1
node: 6.10.3
os: win32 x64
@angular/common: 4.1.2
@angular/compiler: 4.1.2
@angular/core: 4.1.2
@angular/forms: 4.1.2
@angular/http: 4.1.2
@angular/platform-browser: 4.1.2
@angular/platform-browser-dynamic: 4.1.2
@angular/router: 4.1.2
@angular/cli: 1.0.1
@angular/compiler-cli: 4.1.2
왜 canLoad? canActivate해야할까요? –
고마워 ** ** ** 그게 해결책이야. 나는 그것을 보지 못했다 ... 대답으로 추가 할 수 있겠는가? (명백한) 해결책으로 표시 할 수 있을까? – Naduweisstschon