Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何使用角度6+;nodejs_Node.js_Angular_Single Sign On - Fatal编程技术网

Node.js 如何使用角度6+;nodejs

Node.js 如何使用角度6+;nodejs,node.js,angular,single-sign-on,Node.js,Angular,Single Sign On,我想实现单点登录,但我不太明白如何实现。这就是我目前所拥有的。事实上,我没有错。只是单点登录不起作用,在两个客户端中我都必须登录两次,尽管服务器是一个。我使用角度6节点(快速) token.interceptor.ts: import { Injectable } from "@angular/core"; import { AuthService } from "../services/auth.service"; import { HttpInterceptor, HttpRequest,

我想实现单点登录,但我不太明白如何实现。这就是我目前所拥有的。事实上,我没有错。只是单点登录不起作用,在两个客户端中我都必须登录两次,尽管服务器是一个。我使用角度6节点(快速)

token.interceptor.ts:

import { Injectable } from "@angular/core";
import { AuthService } from "../services/auth.service";
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent, HttpErrorResponse } from "@angular/common/http";
import { Observable, throwError } from "rxjs";
import { catchError } from "rxjs/operators";
import { Router } from "@angular/router";

@Injectable()
export class TokenInterceptor implements HttpInterceptor {
    constructor(private auth: AuthService, private router: Router){
    }

    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        if (this.auth.isAuthenticated()) {
            req = req.clone({
                setHeaders: {
                    Authorization: this.auth.getToken()
                }
            })
        }
        return next.handle(req).pipe(
            catchError(
                (error: HttpErrorResponse) => this.handleAuthError(error)
            )
        )
    }

    private handleAuthError(error: HttpErrorResponse): Observable<any> {
        if (error.status === 401) {
            this.router.navigate(['/login']), {
                queryParams: {
                    sessionFailed: true
                }
            }
        }

        return throwError(error)
    }

}
从“@angular/core”导入{Injectable};
从“./services/auth.service”导入{AuthService};
从“@angular/common/http”导入{HttpInterceptor,HttpRequest,HttpHandler,HttpEvent,HttpErrorResponse};
从“rxjs”中导入{可观察,投掷者};
从“rxjs/operators”导入{catchError};
从“@angular/Router”导入{Router}”;
@可注射()
导出类TokenInterceptor实现HttpInterceptor{
构造函数(私有身份验证:身份验证服务,私有路由器:路由器){
}
截取(req:HttpRequest,next:HttpHandler):可观察{
if(this.auth.isAuthenticated()){
req=req.clone({
集合标题:{
授权:this.auth.getToken()
}
})
}
返回next.handle(req.pipe)(
捕捉错误(
(错误:HttpErrorResponse)=>此.handleAuthError(错误)
)
)
}
私有handleAuthError(错误:HttpErrorResponse):可观察{
如果(error.status==401){
this.router.navigate(['/login']){
查询参数:{
sessionFailed:true
}
}
}
返回投掷器(错误)
}
}
app.component.ts:

import { Component, OnInit } from '@angular/core';
import { AuthService } from './shared/services/auth.service';

@Component({
  selector: 'app-root',
  template: '<router-outlet></router-outlet>'
})
export class AppComponent implements OnInit {
  constructor(private auth: AuthService) {}

  ngOnInit() {
    const potentialToken = localStorage.getItem('auth-token')
    if (potentialToken !== null) {
      this.auth.setToken(potentialToken)
    }
  }
}
从'@angular/core'导入{Component,OnInit};
从“./shared/services/auth.service”导入{AuthService};
@组成部分({
选择器:'应用程序根',
模板:“”
})
导出类AppComponent实现OnInit{
构造函数(私有身份验证:身份验证服务){}
恩戈尼尼特(){
const-potentialToken=localStorage.getItem('auth-token'))
if(potentialToken!==null){
this.auth.setToken(potentialToken)
}
}
}
授权服务:

import { Injectable } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { Observable } from "rxjs";
import { tap } from "rxjs/operators";
import { User } from "../interfaces";

@Injectable({
    providedIn: 'root'
})
export class AuthService {

    private token = null;

    constructor(private http: HttpClient) {
    }

    login(user: User): Observable<{token: string}> {  
        return this.http.post<{token: string}>('/api/auth/login', user)
            .pipe(
                tap(
                    ({token}) => {
                        localStorage.setItem('auth-token', token)   
                        this.setToken(token)                     
                    }
                )
            )
    }

    setToken(token: string) {
        this.token = token
    }

    getToken(): string {
        return this.token
    }

    isAuthenticated(): boolean {
        return !!this.token
    }

    logout() {
        this.setToken(null) 
        localStorage.clear()

    }
}
从“@angular/core”导入{Injectable};
从“@angular/common/http”导入{HttpClient};
从“rxjs”导入{observeable};
从“rxjs/operators”导入{tap};
从“./接口”导入{User};
@注射的({
providedIn:'根'
})
导出类身份验证服务{
私有令牌=null;
构造函数(专用http:HttpClient){
}
登录(用户:用户):可观察{
返回此.http.post('/api/auth/login',user)
.烟斗(
水龙头(
({token})=>{
localStorage.setItem('auth-token',token)
this.setToken(令牌)
}
)
)
}
setToken(标记:字符串){
this.token=令牌
}
getToken():字符串{
还这个。代币
}
isAuthenticated():布尔值{
返回!!这个令牌
}
注销(){
this.setToken(null)
localStorage.clear()
}
}

这些应用程序是否在同一域/端口上?@David都具有相同的本地主机域,但端口不同。在这种情况下,
localStorage
将无法工作。您可能需要使用cookies或其他机制