首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用HTTPInterceptor的角7+ Spring不调用API

使用HTTPInterceptor的角7+ Spring不调用API
EN

Stack Overflow用户
提问于 2020-04-27 21:17:54
回答 1查看 268关注 0票数 0

不知道我做错了什么。需要帮助..。如果需要的话,我也可以提供其他代码。

学习在角8+ Spring中实现基本的HTTP登录。我编写了下面的代码,能够看到console.log,但它从不调用API。这意味着如果我在chrome浏览器中看到“网络”选项卡,它就永远不会出现。还在调试模式下启动了Spring,它从未停止过。所以我猜我做错了什么。我在控制台中没有看到任何错误。

注意:我的Spring运行在5000端口

authentication.service.ts

代码语言:javascript
复制
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { BehaviorSubject, Observable } from 'rxjs';
import { map } from 'rxjs/operators';

  export class User {
    constructor(
      public status: string,
    ) { }
  }

@Injectable({
  providedIn: 'root'
})
export class AuthenticationService {
  constructor(
    private httpClient: HttpClient
  ) {
  }

  authenticate(username, password) {
    console.log(username);
    console.log(password);
    const headers = new HttpHeaders({ Authorization: 'Basic ' + btoa(username + ':' + password) });
    return this.httpClient.get<User>('http://localhost:5000/api/validateLogin', { headers }).pipe(
      map(
        userData => {
          sessionStorage.setItem('username', username);
          let authString = 'Basic ' + btoa(username + ':' + password);
          sessionStorage.setItem('basicauth', authString);
          return userData;
        }
      )
    );
  }

  isUserLoggedIn() {
    let user = sessionStorage.getItem('username')
    console.log(!(user === null))
    return !(user === null)
  }

  logOut() {
    sessionStorage.removeItem('username')
  }
}

basicauthhttpinterceptor.service.ts

代码语言:javascript
复制
import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler } from '@angular/common/http';
import { AuthenticationService } from './authentication.service';

@Injectable({
  providedIn: 'root'
})
export class BasicAuthHtppInterceptorService implements HttpInterceptor {

  constructor() { }

  intercept(req: HttpRequest<any>, next: HttpHandler) {
    if (sessionStorage.getItem('username') && sessionStorage.getItem('basicauth')) {
      req = req.clone({
        setHeaders: {
          Authorization: sessionStorage.getItem('basicauth')
        }
      })
    }
    return next.handle(req);
  }
}

app.module.ts

代码语言:javascript
复制
providers: [
  {  
    provide:HTTP_INTERCEPTORS, useClass:BasicAuthHtppInterceptorService, multi:true 
  }
  ],

解决方案:

我没有更改login.component.ts中的代码

新法典:

代码语言:javascript
复制
checkLogin() {
    (this.loginservice.authenticate(this.username, this.password).subscribe(
      data => {
        this.router.navigate([''])
        this.invalidLogin = false
      },
      error => {
        this.invalidLogin = true

      }
    )
    );
  }

旧法典:

代码语言:javascript
复制
  checkLogin() {
    if (this.loginservice.authenticate(this.username, this.password)
    ) {
      this.router.navigate([''])
      this.invalidLogin = false
    } else
      this.invalidLogin = true
  }
EN

回答 1

Stack Overflow用户

发布于 2020-04-27 21:50:35

解决办法:

我没有更改login.component.ts中的代码

新法典:

代码语言:javascript
复制
checkLogin() {
    (this.loginservice.authenticate(this.username, this.password).subscribe(
      data => {
        this.router.navigate([''])
        this.invalidLogin = false
      },
      error => {
        this.invalidLogin = true

      }
    )
    );
  }

调用身份验证方法的旧代码方式:

代码语言:javascript
复制
checkLogin() {
    if (this.loginservice.authenticate(this.username, this.password)
    ) {
      this.router.navigate([''])
      this.invalidLogin = false
    } else
      this.invalidLogin = true
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61468611

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档