Angular 2 + Heroku,总是重定向到https://而不是使用http:// [英] Angular 2+Heroku , always redirect to https:// instead of using http://
本文介绍了Angular 2 + Heroku,总是重定向到https://而不是使用http://的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个在Heroku中托管的Angular 2的应用程序,我想将所有http请求重定向到https,那么正确的方法是什么?谢谢!
I have an application of Angular 2 hosted in Heroku, I would like to redirect all http requests to https, what would be the correct way to do it? Thank you!
推荐答案
如果您希望将任何URL重定向到其https版本,请将此类作为服务实现。该类将检查开发者模式,以便仅在应用程序部署在产品中时才会发生重定向。
If you wish to redirect any URL to its https equivalent, implement this class as a service. The class checks for developer mode so that redirect will only happen when the app is deployed in 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;
}
}
这个警卫必须适用于每一个路径。例如:
This guard must be applied to every path. For example:
{path: 'mypath', component: MyPathComponent, canActivate: [IsSecureGuard]}
这篇关于Angular 2 + Heroku,总是重定向到https://而不是使用http://的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文