From b13c73ff241d35ac2a4f834652e2f16c1bdab7e9 Mon Sep 17 00:00:00 2001 From: Art Date: Mon, 13 Sep 2021 20:35:37 +0300 Subject: [PATCH] 120. Creating Interceptor (#14 Section 16: Interceptor) --- .../src/app/interceptor/auth.interceptor.ts | 49 +++++++++++++++++++ .../src/app/service/authentication.service.ts | 18 +++---- .../src/environments/environment.ts | 3 +- 3 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 support-portal-frontend/src/app/interceptor/auth.interceptor.ts diff --git a/support-portal-frontend/src/app/interceptor/auth.interceptor.ts b/support-portal-frontend/src/app/interceptor/auth.interceptor.ts new file mode 100644 index 0000000..fe77a44 --- /dev/null +++ b/support-portal-frontend/src/app/interceptor/auth.interceptor.ts @@ -0,0 +1,49 @@ +import {Injectable} from '@angular/core'; +import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http'; +import {Observable} from 'rxjs'; +import {environment} from "../../environments/environment"; +import {AuthenticationService} from "../service/authentication.service"; + +@Injectable() +export class AuthInterceptor implements HttpInterceptor { + + private publicUrlPatterns: string[] = []; + private host: string = environment.apiUrl; + + constructor(private authenticationService: AuthenticationService) { + let publicUrlsSpringPatterns: string[] = environment.publicUrls; + let hostPattern = this.host.replace("/", "\/"); + for (const urlPattern of publicUrlsSpringPatterns) { + + let newUrlPattern = urlPattern + .replace('/**', "\/.*") + .replace('/*', '\/[A-Za-z0-9\-\.]+') + +"$"; + this.publicUrlPatterns.push(hostPattern + newUrlPattern); + } + } + + intercept(request: HttpRequest, next: HttpHandler): Observable> { + + if (this.isRequestUrlMatches(request.url)) { + request = this.injectAuthToken(request); + } + return next.handle(request); + } + + private injectAuthToken(request: HttpRequest) { + this.authenticationService.loadToken(); + const token = this.authenticationService.getToken(); + request = request.clone({ + setHeaders: { + Authorization: `Bearer ${token}` + } + }); + return request; + } + + public isRequestUrlMatches(requestUrl: string): boolean { + return requestUrl.includes(this.host) && + !this.publicUrlPatterns.some(urlPattern => requestUrl.match(urlPattern)); + } +} diff --git a/support-portal-frontend/src/app/service/authentication.service.ts b/support-portal-frontend/src/app/service/authentication.service.ts index 75e2e8a..8e7cd44 100644 --- a/support-portal-frontend/src/app/service/authentication.service.ts +++ b/support-portal-frontend/src/app/service/authentication.service.ts @@ -6,14 +6,14 @@ import {Observable} from "rxjs"; import {User} from "../model/user"; import {JwtHelperService} from "@auth0/angular-jwt"; -const USER_STORAGE_KEY = "user"; -const JWT_TOKEN_STORAGE_KEY = "jwt-token"; - @Injectable({ providedIn: 'root' }) export class AuthenticationService { + public USER_STORAGE_KEY = "user"; + public JWT_TOKEN_STORAGE_KEY = "jwt-token"; + private host: string = environment.apiUrl; private token: string | null; private loggedInUser: string | null; @@ -38,26 +38,26 @@ export class AuthenticationService { public logout(): void { this.token = null; this.loggedInUser = null; - this.storage.removeItem(JWT_TOKEN_STORAGE_KEY); - this.storage.removeItem(USER_STORAGE_KEY); + this.storage.removeItem(this.JWT_TOKEN_STORAGE_KEY); + this.storage.removeItem(this.USER_STORAGE_KEY); this.storage.removeItem("users"); } public saveToken(token: string): void { this.token = token; - this.storage.setItem(JWT_TOKEN_STORAGE_KEY, token); + this.storage.setItem(this.JWT_TOKEN_STORAGE_KEY, token); } public loadToken(): void { - this.token = this.storage.getItem(JWT_TOKEN_STORAGE_KEY); + this.token = this.storage.getItem(this.JWT_TOKEN_STORAGE_KEY); } public addUserToLocalStorage(user: User) { - this.storage.setItem(USER_STORAGE_KEY, JSON.stringify(user)); + this.storage.setItem(this.USER_STORAGE_KEY, JSON.stringify(user)); } public getUserFromLocalStorage(): User { - let userJson = this.storage.getItem(USER_STORAGE_KEY); + let userJson = this.storage.getItem(this.USER_STORAGE_KEY); return JSON.parse(userJson!); } diff --git a/support-portal-frontend/src/environments/environment.ts b/support-portal-frontend/src/environments/environment.ts index b7d6f94..5c95f57 100644 --- a/support-portal-frontend/src/environments/environment.ts +++ b/support-portal-frontend/src/environments/environment.ts @@ -4,7 +4,8 @@ export const environment = { production: false, - apiUrl: 'http://localhost:8080' + apiUrl: 'http://localhost:8080', + publicUrls: ['/user/login', '/user/register', '/user/*/image/**', '/user/image/**'] }; /*