Angular 7 + Spring Boot Basic Auth Using HTTPInterceptor Example
We had seen we had to duplicate the code for adding Basic Auth Headers to the HTTPRequest before making HTTP calls. In this tutorial we will be implement a HTTPInterceptor which will intercept all outgoing HTTP requests.

Angular 7+ Spring Boot - Table of Contents
Angular 7 + Spring Boot Application Hello World Example Angular 7 + Spring Boot Application CRUD Example Angular 7 + Spring Boot Application Login Example Angular 7 + Spring Boot Application Basic Authentication Example Angular 7 + Spring Boot Basic Auth Using HTTPInterceptor Example Angular 7 + Spring Boot JWT Authentication Hello World Example
Video
This tutorial is explained in the below Youtube Video.Implement changes for Basic Authentication on the Angular side
We will be modifying the code we developed in the previous tutorial The angular project we will be developing is as follows-
import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders } from '@angular/common/http'; 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:8080/employees/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') } }