2017-03-01 8 views
0

の代わりに常にhttps://にリダイレクトします。私は角膜2のアプリケーションをHerokuでホストしています。すべてのHTTP要求をhttpsにリダイレクトしたいと思います。それをするために?ありがとうございました!角2 + Heroku、http://

答えて

3

任意の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; 
    } 

} 

このガードは、すべてのパスに適用する必要があります。たとえば、

{path: 'mypath', component: MyPathComponent, canActivate: [IsSecureGuard]} 
+0

解決策は、ありがとうございます! –