使用Angular LoginService重定向到错误的URL

问题描述 投票:0回答:1

我有一个简单的登录服务,该服务应将用户请求重定向到身份验证服务。想法是,如果将应用程序部署在我们的生产环境中,则将用户定向到一个URL,如果将其部署在测试环境中,则将它们重定向到另一个URL。但是实际上,该服务会将所有请求重定向到第二个“平台开发”环境。我在下面添加了一些代码,将不胜感激。

import { Injectable } from '@angular/core';
import { Location } from '@angular/common';

import { Principal } from '../auth/principal.service';
import { AuthServerProvider } from '../auth/account.service';

@Injectable()
export class LoginService {

    constructor(
        private principal: Principal,
        private authServerProvider: AuthServerProvider,
        private location: Location
    ) {}

    login():any {
        // from jhi 4.0
        let port = (location.port ? ':' + location.port : '');
        if (port === ':9000' || port === ':9001') {
            port = ':8080';
        }

        // if on platform1
        if( location.hostname !== "localhost" && location.hostname.indexOf("development") == -1){
            location.href = '//' + location.hostname + port + '/portal/oauth2/authorization/oidc';
        // if on platform-development
        }else{
            location.href = '//' + location.hostname + port + '/oauth2/authorization/oidc';
        }
    }
}

在生产中运行时,该请求将始终重定向到/oauth2/authorization/oidc URL,而不是正确的/portal/oauth2/authorization/oidc URL,这将导致错误404。

javascript angular redirect jhipster openid-connect
1个回答
0
投票

我建议您使用环境文件。看看这个角度教程https://angular.io/guide/build

使用此方法,可以将url变量部署在不同的环境中时进行更改。

© www.soinside.com 2019 - 2024. All rights reserved.