我在生产环境中无法使我的应用程序工作。
我的应用程序使用@auth0/angular-jwt中的angular-jwt
如果用户拥有有效的令牌,他将被转发到MainScreenComponent,否则他将被重定向到登录页面。
import { AuthGuard } from './services/Authentication/auth-guard.service';
import { AuthGuardLogin } from './services/Authentication/auth-guard-login.service';
import { AuthService } from './services/Authentication/auth.service';
import { BrowserModule , HAMMER_GESTURE_CONFIG} from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { LoginComponent } from './components/login/login.component';
import { NotFoundComponent } from './components/not-found/not-found.component';
import { NoAccessComponent } from './components/no-access/no-access.component';
import { AppComponent } from './app.component';
import { environment } from '../environments/environment';
import { ServiceWorkerModule } from '@angular/service-worker';
import { MainScreenComponent } from './components/main-screen/main-screen.component';
import { JwtModule } from '@auth0/angular-jwt';
export function tokenGetter() {
return localStorage.getItem('token');
}
@NgModule({
declarations: [
AppComponent,
LoginComponent,
NotFoundComponent,
NoAccessComponent,
MainScreenComponent
],
imports: [
BrowserModule,
BrowserAnimationsModule,
HttpClientModule,
FormsModule,
ReactiveFormsModule,
ServiceWorkerModule.register('ngsw-worker.js', { enabled: environment.production }),
RouterModule.forRoot([
{ path: '', component: MainScreenComponent , canActivate: [AuthGuard]},
{ path: 'login', component: LoginComponent, canActivate: [AuthGuardLogin] },
{ path: '**', component: NotFoundComponent }
]),
JwtModule.forRoot({
config: {
tokenGetter: tokenGetter,
whitelistedDomains: environment.whiteListDomains,
}
})
],
providers: [
AuthService,
AuthGuard,
AuthGuardLogin,
],
bootstrap: [AppComponent]
})
export class AppModule { }
如果我使用ng serve,一切都像预期的那样工作,但在ng build --prod之后,如果我的初始URL是https://my-site:4300/login,我会得到404错误。在这种情况下,RouterModule设置将被忽略。
如果我输入https://my-site:4300,我将被重定向到https://my-site:4300/login,并显示登录页面。
有任何想法吗?
========================
更新于2018年10月31日:
这是AuthGuardLogin:
import { AuthService } from './auth.service';
import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';
@Injectable({
providedIn: 'root'
})
export class AuthGuardLogin implements CanActivate {
constructor( private router: Router, private authService: AuthService) { }
canActivate() {
if (this.authService.isLoggedIn() === false) {
return true; }
this.router.navigate(['/']);
return false;
}
}
现在我所做的是将网站托管在Kestrel服务器中。现在它运行得非常完美。为什么?我不知道 :-)!
顺便说一下,Kestrel服务器托管在Windows服务中。因此,我可以将其与我们的软件包一起部署,并通过常规安装进行安装。
最好的问候和非常感谢!!!