我有一个在Heroku中托管的Angular 2的应用程序,我想将所有http请求重定向到https,这是正确的方法吗?谢谢!
如果您希望将任何URL重定向到其https等效项,请将此类实现为服务.该类检查开发人员模式,以便只有在prod中部署应用程序时才会发生重定向.
import {Injectable, isDevMode} from '@angular/core';
import {CanActivate, ActivatedRouteSnapshot} from '@angular/router';
@Injectable()
export class IsSecureGuard implements CanActivate {
canActivate(route: ActivatedRouteSnapshot): boolean {
if (!(isDevMode()) && (location.protocol !== 'https:')) {
location.href = 'https:' + window.location.href.substring(window.location.protocol.length);
return false;
}
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
这种防护必须适用于每条路径.例如:
{path: 'mypath', component: MyPathComponent, canActivate: [IsSecureGuard]}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1471 次 |
| 最近记录: |