Angular 11 SpringBoot Jwt Authentication example – Spring Security + MySQL
Tags: angular, jwt, springboot, authentication
Published: 2020-12-06 11:08:56

Tutorial: Angular 11 SpringBoot Jwt Authentication example – Angular 11 + Spring Security + MySQL Full Stack – Part 1: Overview and Architecture.

The post is Part 1 of the series: Angular Spring Boot JWT Authentication example | Angular 11 + Spring Security + MySQL Full Stack. In this part, we show you Overview and Architecture of the System (from Angular frontend to SpringBoot backend). You will see the combination of big components and what you need to do for the security part (authentication & authorization) of full-stack web development.

Part 1: [Overview and Architecture](https://ozenero.com/angular-11-springboot-jwt-authentication-example)

Part 2: [Build Spring Boot Backend](https://ozenero.com/spring-framework/spring-boot/angular-spring-boot-jwt-authentication-example-angular-6-spring-security-mysql-full-stack-part-2-build-backend)

Part 3: [Build Angular Frontend](https://ozenero.com/java-integration/angular-spring-boot-jwt-authentication-example-angular-6-spring-security-mysql-full-stack-part-3-build-frontend)

## Angular 11 SpringBoot Jwt Authentication example

We will build an application, from frontend (Angular) to backend (Spring Boot), which allows users to register, login account. This application is secured with JWT (JSON Web Token) authentication and Spring Security. Then, depending on the role of current User (user, pm or admin), this system accepts what he can access:

![Angular-Login-Form](https://ozenero.com/wp-content/uploads/2020/12/Angular-Login-Form.png)

![Register-Form](https://ozenero.com/wp-content/uploads/2020/12/Register-Form.png)

![Home-Page-of-Jack-User](https://ozenero.com/wp-content/uploads/2020/12/Home-Page-of-Jack-User.png)

![Content-of-Jack-User](https://ozenero.com/wp-content/uploads/2020/12/Content-of-Jack-User.png)

The diagram below show how our system handles User Registration and User Login processes:

![angular-spring-security-jwt-authentication-work-process-diagram](https://ozenero.com/wp-content/uploads/2020/12/angular-spring-security-jwt-authentication-work-process-diagram.png)

## Demo

https://youtu.be/7ZfInOvFsz0

## Spring Boot back-end with Spring Security

This is diagram for Spring Security/JWT classes that are separated into 3 layers:
– HTTP
– Spring Security
– REST API

![spring-boot-angular-spring-security-jwt-authentication-architecture-diagram-back-end-server](https://ozenero.com/wp-content/uploads/2020/12/spring-boot-angular-spring-security-jwt-authentication-architecture-diagram-back-end-server.png)

– SecurityContextHolder provides access to the SecurityContext.
– SecurityContext holds the Authentication and possibly request-specific security information.
– Authentication represents the principal which includes GrantedAuthority that reflects the application-wide permissions granted to a principal.
– UserDetails contains necessary information to build an Authentication object from DAOs or other source of security data.
– UserDetailsService helps to create a UserDetails from a String-based username and is usually used by AuthenticationProvider.
– JwtAuthTokenFilter (extends OncePerRequestFilter) pre-processes HTTP request, from Token, create Authentication and populate it to SecurityContext.
– JwtProvider validates, parses token String or generates token String from UserDetails.
– UsernamePasswordAuthenticationToken gets username/password from login Request and combines into an instance of Authentication interface.
– AuthenticationManager uses DaoAuthenticationProvider (with help of UserDetailsService & PasswordEncoder) to validate instance of UsernamePasswordAuthenticationToken, then returns a fully populated Authentication instance on successful authentication.
– SecurityContext is established by calling SecurityContextHolder.getContext().setAuthentication(…​) with returned authentication object above.
– AuthenticationEntryPoint handles AuthenticationException.
– Access to Restful API is protected by HTTPSecurity and authorized with Method Security Expressions.

## Angular front-end with Interceptor

![Angular front-end with Interceptor](https://ozenero.com/wp-content/uploads/2020/12/spring-boot-angular-spring-security-jwt-authentication-architecture-diagram-front-end-client.png)

– app.component is the parent component that contains routerLink and router-outlet for routing. It also has an authority variable as the condition for displaying items on navigation bar.
– user.component, pm.component, admin.component correspond to Angular Components for User Board, PM Board, Admin Board. Each Board uses user.service to access authority data.
– register.component contains User Registration form, submission of the form will call auth.service.
– login.component contains User Login form, submission of the form will call auth.service and token-storage.service.

– user.service gets access to authority data from Server using Angular HttpClient ($http service).
– auth.service handles authentication and signup actions with Server using Angular HttpClient ($http service).
– every HTTP request by $http service will be inspected and transformed before being sent to the Server by auth-interceptor (implements HttpInterceptor).
– auth-interceptor check and get Token from token-storage.service to add the Token to Authorization Header of the HTTP Requests.

– token-storage.service manages Token inside Browser’s sessionStorage.