Securing Swagger API Documentation with an API Key (JWT) | FREE COURSE

Get Arrays
22 Feb 202141:34

Summary

TLDREste curso se enfoca en la creación y seguridad de la documentación de APIs utilizando Swagger. Aprenderás qué es la documentación de una API, por qué es importante y cómo Swagger puede ayudar en este proceso. Seguidamente, se aborda la implementación y configuración de Swagger para requerir una clave API, crucial para la seguridad de tu aplicación. El instructor te guía a través de la construcción de un angular application que permitirá a los usuarios obtener una clave API. Al finalizar, tendrás un sólido entendimiento de la documentación de APIs y cómo crearla y protegerla adecuadamente. Además, se discuten los prerrequisitos, incluyendo el conocimiento de Java, Spring Framework, Spring Security, JSON Web Token, Angular, HTML, CSS y TypeScript. El curso ofrece una visión general de la aplicación base, que es una aplicación de gestión de facturas, y cómo se integra con Swagger para proporcionar una experiencia de usuario mejorada y facilidad en el mantenimiento y actualización de la API.

Takeaways

  • 📚 **Creación de documentación de API**: Se discute cómo utilizar Swagger para crear documentación de API que sea útil para los usuarios y cómo integrarla con una aplicación existente.
  • 🛡️ **Seguridad con API Key**: Se aborda la configuración de Swagger para requerir un API key, explicando cómo proteger la documentación de la API y los endpoints.
  • 🔗 **Swagger como herramienta**: Se menciona que Swagger es una herramienta para crear documentación de API y se da una visión general de sus ventajas.
  • 🚀 **Angular para aplicaciones front-end**: Se indica que se construirá una aplicación Angular para que los usuarios puedan obtener un API key y usarlo para acceder a la API.
  • 💻 **Pre-requisitos técnicos**: Se resalta la importancia de entender tecnologías como Java, Spring Framework, Spring Security, API, JSON Web Token, Angular, HTML, CSS, JavaScript y TypeScript antes de tomar el curso.
  • 🏗️ **Sobre la configuración de Swagger**: Se detalla el proceso para agregar la configuración de Swagger a una aplicación existente en lugar de construir una aplicación desde cero.
  • 🔑 **JWT como API Key**: Se explica que un JSON Web Token (JWT) se usará como el API key y se destaca la necesidad de comprender su estructura y uso.
  • 📝 **Importancia de la documentación de API**: Se discuten las razones por las cuales crear documentación de API es beneficioso, incluyendo mejora de la experiencia del usuario, aumento de la conciencia, ahorro de tiempo y dinero, y facilidad de mantenimiento.
  • 🏛️ **Aplicación de ejemplo**: Se presenta una visión general de la aplicación de gestión de facturas que se utilizará a lo largo del curso para示范strar cómo se integra Swagger.
  • 🛠️ **Configuración de seguridad existente**: Se asume que la configuración de seguridad para la API ya está en su lugar y se indica que el curso en seguridad es un requisito previo para comprender estos aspectos.
  • ⚙️ **Adición de dependencias de Swagger**: Se proporciona una guía para agregar la dependencia de Swagger en el archivo pom.xml para poder utilizar las funcionalidades de Swagger en la aplicación.

Q & A

  • ¿Qué es la documentación de una API y por qué es importante crearla?

    -La documentación de una API es un conjunto de instrucciones que permite a los usuarios entender cómo usar o integrarse con la API. Es importante porque mejora la adaptación del usuario, aumenta el conocimiento sobre la API, ahorra tiempo y dinero al evitar la necesidad de soporte directo, y facilita el mantenimiento de la API al proporcionar una especificación clara de cómo funciona.

  • ¿Por qué se utiliza Swagger para la documentación de la API?

    -Swagger es una herramienta que permite crear y visualizar la documentación de una API de manera interactiva. Se utiliza porque proporciona una forma fácil de generar y mantener la documentación, y permite a los desarrolladores probar y ejecutar las operaciones de la API directamente desde la interfaz de usuario de Swagger.

  • ¿Qué es un JSON Web Token (JWT) y cómo se relaciona con la seguridad de una API?

    -Un JSON Web Token (JWT) es un estándar abierto (RFC 7519) que define una estructura de codificación compacta que representa una cadena de notificaciones que pueden ser decodificada y verificadas en un servidor. Se relaciona con la seguridad de una API porque actúa como un API key, proporcionando una forma de autenticación segura para acceder a los recursos de la API.

  • ¿Qué tecnologías son necesarias para comprender y aprovechar este curso sobre documentación de API con Swagger?

    -Se necesita entender Java, el framework Spring y Spring Security, así como conceptos de API y JSON Web Token. Del lado del cliente, se espera que el estudiante tenga conocimientos en Angular, HTML, CSS, JavaScript y TypeScript.

  • ¿Cómo se puede obtener el mejor rendimiento del curso y qué recursos externos son útiles?

    -Para obtener el mejor rendimiento del curso, se recomienda tomar el curso de seguridad previo, entender JSON Web Token, ver todos los videos, utilizar recursos en línea para investigar dudas, codificar junto con el curso si es posible y, si hay preguntas, comunicarse con el instructor.

  • ¿Qué es una 'Docket' en el contexto de configuración de Swagger?

    -Una 'Docket' en Swagger es un objeto que se utiliza para configurar y personalizar la documentación de la API. Se define en una clase de configuración y se utiliza para链定 ('chain') múliples configuraciones juntas, como la información de la API, el esquema de seguridad y las anotaciones de Swagger.

  • ¿Cómo se define la información de contacto en la documentación de Swagger?

    -La información de contacto se define mediante un método que devuelve un objeto 'Contact'. Este objeto se construye con detalles como el nombre de contacto, la URL de contacto y el correo electrónico de contacto, que generalmente son proporcionados por el desarrollador de la API.

  • ¿Cómo se indica en Swagger que se requiere un API key para acceder a la documentación de la API?

    -Se indica que se requiere un API key definiendo un objeto 'ApiKey' en la configuración de Swagger. Se especifica el nombre del encabezado (como 'Authorization') y se asocia con un 'Security Reference' que describe el tipo de autorización requerida.

  • ¿Qué es una 'Security Context' y cómo se define en Swagger?

    -Una 'Security Context' en Swagger define el alcance y las reglas de seguridad que se aplicarán a una o varias rutas de la API. Se define utilizando el método 'securityContexts' y se asocia con un 'Security Reference' que se puede aplicar a las rutas de la API para requerir autenticación.

  • ¿Cómo se asegura que los usuarios obtengan un API key para usar la documentación de la API?

    -Los usuarios obtienen un API key a través de una aplicación frontal, como se describe en la documentación de Swagger. Generalmente, implica que el usuario se registre o inicie sesión en la aplicación, solicita un token y luego lo utiliza para autorizar su acceso a la documentación de la API.

  • ¿Por qué se recomienda tomar el curso de seguridad antes de este curso sobre documentación de API con Swagger?

    -Se recomienda tomar el curso de seguridad primero porque este curso asume que el estudiante ya tiene un conocimiento sólido de Spring Security y JSON Web Token, que son fundamentales para entender cómo proteger una API y requerir un API key. El curso de seguridad proporciona las bases necesarias para comprender la configuración de seguridad en el curso de Swagger.

Outlines

00:00

😀 Introducción al curso de Swagger API Documentation

El primer párrafo presenta el curso centrado en la creación de documentación de API con Swagger, utilizando una clave API para la seguridad. Se menciona que el curso incluirá la definición de la documentación de API, la implementación y configuración de Swagger para requerir una clave API, y la construcción de una aplicación Angular para la obtención de claves de API. Además, se ofrece una visión general de la documentación de API, su importancia y cómo el usuario puede interactuar con ella, incluyendo el proceso de autenticación para obtener una clave API.

05:01

📚 Requisitos previos y tecnologías clave

Este párrafo aborda los conocimientos previos necesarios para aprovechar el curso, incluyendo Java, Spring Framework, Spring Security, API, JSON Web Token, Angular, HTML, CSS, JavaScript y TypeScript. Se destaca la importancia de comprender estos conceptos para no encontrarse estancado durante el curso y se sugiere tomar un curso de seguridad previo que cubrirá Spring Security y JSON Web Token.

10:02

📋 Contenidos y objetivos del curso

Se describe el plan de estudios del curso, que incluye introducciones, explicación de los conceptos básicos de la documentación de API, detalles sobre Swagger, una revisión general de la API y su seguridad, configuración de Swagger para requerir una clave API, uso de anotaciones de Swagger y construcción de una aplicación Angular. Se enfatiza la importancia de entender la documentación de API y se ofrece orientación para obtener el máximo beneficio del curso.

15:04

🛠️ Ventajas de crear documentación de API

En este párrafo se discuten las razones por las cuales crear documentación de API es beneficioso, como mejorar la adaptación del usuario, aumentar la conciencia, ahorrar tiempo y dinero, y facilitar el mantenimiento. Se proporciona una breve explicación de cómo la documentación de API puede ser una herramienta valiosa para los desarrolladores y la empresa.

20:05

🏢 Aplicación base y su configuración de seguridad

Se presenta una visión general de la aplicación base que se utilizará en el curso, la cual es una aplicación de gestión de facturas. Se describen las operaciones CRUD típicas y cómo se realiza la inyección de dependencias. Además, se menciona la configuración de seguridad, la cual es un requisito previo para el curso, y se sugiere tomar un curso específico de seguridad para comprender plenamente la configuración.

25:10

📄 Adición de dependencia y configuración inicial de Swagger

Se detalla el proceso para comenzar con Swagger en la aplicación, incluyendo la adición de la dependencia de Swagger en el archivo pom.xml y la creación de una clase de configuración de Swagger. Se enfatiza la importancia de la dependencia para que Swagger funcione correctamente en la aplicación.

30:10

📝 Configuración avanzada de Swagger con constantes y beans

Este párrafo se enfoca en la creación de una clase de constantes para almacenar información que se utilizará en la configuración de Swagger, como la información de contacto, el título y la descripción de la página, entre otros. Se describe cómo se estructurará la configuración de Swagger utilizando beans y se menciona la importancia de la separación de los métodos para una mejor legibilidad y facilidad de mantenimiento.

35:11

🔐 Definición de seguridad y contexto para Swagger

Se explica cómo definir la seguridad para Swagger, incluyendo la creación de un esquema de seguridad que utiliza una clave API en el encabezado de las solicitudes. Se detalla la definición de un contexto de seguridad y cómo se especifica el alcance y la descripción de la autorización. Este párrafo culmina con la expectativa de crear el bean de Swagger en la siguiente lección.

Mindmap

Keywords

💡Swagger

Swagger es una herramienta de código abierto para la creación de una especificación y documentación de una API. En el video, se utiliza Swagger para generar la documentación de la API que permite a los usuarios entender cómo interactuar con ella. Se menciona que Swagger se configurará para requerir una clave API, lo que demuestra su importancia en la fase de seguridad de la API.

💡API Key

Una API Key es un mecanismo de seguridad que autoriza el acceso a una API. En el contexto del video, se implementa un esquema de seguridad que requiere un API Key para acceder a la documentación y a los recursos de la API, lo que proporciona una capa adicional de protección contra el acceso no autorizado.

💡Spring Framework

El Spring Framework es un marco de trabajo de código abierto para la creación de aplicaciones empresariales. En el video, se indica que el entendimiento del Spring Framework es un requisito previo para el curso, ya que la aplicación que se está documentando utiliza Spring para su desarrollo y se espera que los estudiantes comprendan cómo funciona.

💡Spring Security

Spring Security es un módulo del Spring Framework que proporciona autenticación y autorización. En el video, se resalta la importancia de comprender Spring Security, ya que se utiliza para proteger la API y se espera que los estudiantes de este curso ya tengan conocimientos en este tema.

💡JSON Web Token (JWT)

JSON Web Token es un estándar abierto (RFC 7519) que define una estructura de tokens que se pueden usar para la autenticación entre el cliente y el servidor. En el video, JWT se menciona como el tipo de clave API que se utilizará, y es fundamental que los estudiantes entiendan cómo funcionan los JWT para trabajar con la API segura.

💡Angular

Angular es un framework de aplicaciones web de un solo page (SPA) desarrollado por Google. En el video, se menciona que se construirá una aplicación Angular para que los usuarios puedan obtener una clave API, lo que indica que Angular se utilizará para crear la interfaz de usuario que interactúa con la API.

💡API Documentation

La documentación de una API es un conjunto de recursos que describen cómo utilizar una API. En el video, se destaca la importancia de la documentación para permitir a los desarrolladores y usuarios entender y utilizar la API de manera efectiva, y se menciona que Swagger facilitará esta documentación.

💡Security Configuration

La configuración de seguridad es un aspecto crítico en el desarrollo de APIs que garantiza que solo los usuarios autorizados puedan acceder a los recursos. En el video, se aborda cómo se implementa la configuración de seguridad en la API utilizando Spring Security y JWT, lo que es esencial para proteger los datos y las transacciones.

💡RESTful API

Una API RESTful es un tipo de API que sigue los principios del arquitectura de transferencia de estado representacional (REST). En el video, se habla de un ejemplo de una aplicación de gestión de facturas que utiliza una API RESTful para permitir a los clientes ver y gestionar sus facturas.

💡CRUD Operations

CRUD es una acrónimo que representa Create, Read, Update y Delete (Crear, Leer, Actualizar y Eliminar), operaciones básicas que se realizan en una base de datos. En el video, se mencionan las operaciones CRUD como parte de las funcionalidades que se documentarán y protegerán mediante Swagger y la clave API.

💡Docket Bean

Un Docket Bean es una configuración en Spring que se utiliza para definir laSwagger. En el video, se utiliza un Docket Bean para configurar Swagger y especificar detalles como la versión de Swagger, la información de la API y cómo se debe proteger el acceso a la API.

Highlights

The course aims to teach how to create and secure API documentation using Swagger with an API key.

Swagger is used to define what an API is, its capabilities, and how it can be utilized.

The course covers the importance of API documentation for developers and end-users.

Swagger is not only a documentation tool but also helps in implementing and configuring API security.

The course will guide through building an Angular application for users to obtain an API key.

Prerequisites include understanding Java, Spring Framework, Spring Security, and JSON Web Tokens.

The course is a continuation of a security course focusing on securing APIs with Spring Security and JSON Web Tokens.

The instructor emphasizes the importance of understanding the basics of Angular, HTML, CSS, JavaScript, and TypeScript.

The course outline is provided to give learners a clear expectation of what will be covered.

The course is designed to be concise, covering high-level concepts and practical implementation.

The instructor recommends taking a security course before this one for a better understanding of Spring Security.

API documentation serves as a technical content deliverable that instructs users on how to use an API effectively.

Benefits of API documentation include improved user adoption, increased awareness, time and cost savings, and easier maintenance.

The base application used in the course is an invoice management system with typical CRUD operations.

The application uses Spring Boot and has security configurations that protect endpoints requiring authentication.

Swagger configuration involves adding a dependency and creating a Swagger Config class.

The course will detail how to configure Swagger annotations to customize the UI page.

The final project involves building an Angular application from scratch to work in conjunction with the secured API.

Transcripts

play00:05

hello and welcome to this course

play00:06

securing swagger api documentation with

play00:09

an api key

play00:10

in this course i'm going to show you how

play00:12

you can use swagger to create an api

play00:14

documentation

play00:15

photo api we'll start by defining what

play00:18

an api documentation is

play00:20

why you would want to create one why you

play00:22

might want to use swagger

play00:23

for your api documentation then we'll

play00:26

move on to the ide

play00:27

and i'll show you how to implement and

play00:29

configure swagger to require an api key

play00:31

for your application

play00:33

and finally i'll show you how to build

play00:35

an angular application

play00:36

where you can send your users to get an

play00:38

api key so that they can use it for your

play00:40

api

play00:41

by the end of this course you will have

play00:42

a solid understanding of what an api

play00:45

documentation is

play00:46

and how to properly create and secure

play00:48

api documentation

play00:50

using swagger we have a lot to cover in

play00:52

this course

play00:53

i hope you're excited and i will see you

play00:55

in the course

play01:06

so this is your api documentation where

play01:08

your user would come in to look at your

play01:10

api documentation

play01:12

and possibly try it out so for instance

play01:14

if i go to the

play01:16

get request here and i can read about

play01:18

this

play01:19

and then i can also try it out so i'll

play01:21

click on try it out and then

play01:22

execute this as you can see here i got

play01:25

this four or three forbidden

play01:26

so the idea here would be your user

play01:29

would go

play01:29

to say hey i need to get an api key so

play01:32

they'll just go ahead and click here

play01:33

and then here i'm gonna go ahead and log

play01:35

in with jade and

play01:37

i put in my information click on login

play01:39

and you can see now i'm logged into the

play01:41

frontend application

play01:43

and then i would navigate to my security

play01:45

tab and then here show token

play01:46

well you have to hide the token because

play01:48

it's sensitive information and then to

play01:50

show the token you would click here and

play01:52

then

play01:52

click on it and then it says token

play01:54

copied and then the user would go back

play01:56

here and authorize so they would click

play01:58

here

play01:58

and then pass in the token and then

play02:00

click on authorize

play02:02

and then close this and then now if they

play02:04

go ahead and say execute again

play02:06

you can see now they get an actual

play02:07

response so this was the whole

play02:09

idea of what i wanted to do with the

play02:11

front end and with this api

play02:13

documentation application and if you

play02:14

guys have any questions uh go ahead and

play02:16

reach out to me

play02:17

thank you for taking the course i hope

play02:19

you find it valuable and i will see you

play02:21

guys in the next one

play02:27

i wanted to go over the pre-requisite

play02:29

for this course

play02:30

and this is really something that you

play02:32

really have to understand as far as

play02:34

understanding how to

play02:34

take advantage of this course so let's

play02:36

go ahead and get started so the first

play02:37

technology

play02:38

you need to really understand is java

play02:41

because everything is java base for the

play02:43

back end

play02:43

so you need to understand java the

play02:45

spring framework and also spring

play02:47

security

play02:48

because we're going to be working with

play02:49

spring security as well now the reason

play02:51

i want to make sure that you understand

play02:53

this is because

play02:54

we will not be building the application

play02:57

that we're going to be working on in

play02:58

this course

play02:59

we will only be adding the swagger

play03:00

configuration on top of this application

play03:03

and this application is already using

play03:05

java spring framework and spring

play03:06

security

play03:07

so when i give you the overview of that

play03:09

application i'm expecting you to

play03:11

understand

play03:12

what the application is doing because

play03:14

we're not going to go into the basics

play03:16

of those technologies so you have to

play03:18

understand you know java spring

play03:19

framework and spring security

play03:21

to some degree and then you need to

play03:23

understand application programming

play03:25

interface

play03:25

because this is a concept that we will

play03:27

also be working on since this whole

play03:29

application

play03:30

at least the back end is an application

play03:32

programming interface

play03:33

so you need to make sure you understand

play03:34

that as well and then a json web token

play03:37

so this one web token is going to be

play03:39

our api key and you also need to

play03:41

understand what that means right so you

play03:43

need to understand

play03:44

what kind of information a json web

play03:46

token is carrying and why it's used and

play03:48

things like that so

play03:49

make sure you understand on json web

play03:51

token and then from the front

play03:52

application we'll be building on angular

play03:54

applications so i'm expecting you to

play03:56

understand the basics of angular and for

play03:58

that of course you have to understand

play04:00

html css and javascript so

play04:02

make sure you get those covered and then

play04:04

since angular uses typescript

play04:06

so you need to understand typescript as

play04:08

well now i'm not

play04:10

expecting anyone to be an expert in any

play04:12

of those technologies but at least

play04:14

you have to be comfortable uh with them

play04:16

uh so that you don't get stuck

play04:17

during the course so make sure you

play04:19

understand those technologies

play04:21

well or make sure you're comfortable

play04:23

with them uh or even if you get stuck

play04:25

then you know how to you know research

play04:26

it and fix whatever

play04:28

problem that you have but you have to be

play04:30

comfortable in those technology

play04:36

let's go over the course outline so that

play04:38

you guys understand what to expect from

play04:40

this course so the first thing i want to

play04:42

say

play04:43

is this course is not a very long course

play04:45

so we're not going to have like a dozen

play04:48

sections or anything like that so what

play04:50

you're looking at right now is just a

play04:52

high level of exactly

play04:53

what we're going to be doing in this

play04:55

course so the first part is going to be

play04:56

for the introductions uh this is the

play04:58

part that we

play04:59

we're in now and this is just going to

play05:01

be me introducing the course to you

play05:03

uh telling you how to get the best out

play05:04

of this course and etc and the next part

play05:06

is going to be me going over

play05:08

the concepts with you so i'm going to be

play05:10

talking about api documentation

play05:12

what is api why do we need api

play05:15

documentations what are the advantages

play05:17

of having an api documentation

play05:19

and why you want to do that and then

play05:21

after that we're going to cover swagger

play05:23

so

play05:23

swagger is like an implementation of an

play05:25

api documentation

play05:27

or just an api documentation tool just a

play05:29

tool that you can use to

play05:31

create a documentation for api so we're

play05:32

going to go over swagger

play05:34

and then i'm going to give you some

play05:35

resources as well regarding swagger

play05:38

and then we're going to look at an

play05:39

overview of the api that we have so

play05:42

we're going to start off with some base

play05:43

code and i'm going to walk you through

play05:45

this code and that's going to be

play05:46

our api overview and then i'm also going

play05:48

to go over the security aspect of that

play05:50

application as well

play05:52

so you're gonna understand you know what

play05:54

kind of security that the application

play05:56

has in place

play05:56

and what it's expecting from a user

play05:58

trying to access the application

play06:00

in order for that user to successfully

play06:02

access it and then we're gonna

play06:04

put in the configuration for swagger so

play06:05

we're going to put this wire

play06:06

configuration

play06:07

which is going to require us to put in

play06:09

an api key in order to be able to access

play06:12

the api documentation

play06:13

so after that we're going to move over

play06:15

to some other swagger annotations and

play06:18

those annotations we're mostly going to

play06:19

be using

play06:20

them in the controller so i'm going to

play06:22

show you um how you can customize the

play06:24

ui page for swagger and most of that

play06:27

those annotations i'm going to be

play06:29

putting them in the controller and then

play06:30

lastly we're going to build the angular

play06:32

app so we're going to start from scratch

play06:34

with that application and then build it

play06:36

from there now

play06:37

as you can see this is a very condensed

play06:39

high level

play06:40

overview of the course but there's a lot

play06:43

more in this course so

play06:44

there's no way i'm going to be able to

play06:46

you know list every single technology

play06:48

that is being used on here

play06:49

but at least i hope you guys understand

play06:52

what exactly

play06:53

we're going to be doing in this course

play06:59

i want to talk a little bit about how to

play07:01

get the most out of this course and as

play07:03

the creator of the course

play07:05

i always think that it's a good idea for

play07:07

me to actually tell you how to get the

play07:09

best out of this course so

play07:11

this course is a continuation of a

play07:13

security course that i built and

play07:15

one of the first thing that i would

play07:16

highly recommend every student who want

play07:18

to take this course to do

play07:20

is to take the security course the

play07:21

security course is really a continuation

play07:24

of this course now the reason i didn't

play07:26

mention this and that we work with it is

play07:28

because

play07:28

you know there might be some students

play07:30

that are already very

play07:32

savvy with spanx security so there's no

play07:34

point for them to take this spring

play07:36

security course that i built

play07:38

and i'm going to show that course in a

play07:39

second but i would highly encourage

play07:41

everyone

play07:42

if you're not really good with spanx

play07:44

security then you have to take the

play07:46

security course first before you can

play07:47

take this course

play07:48

because this will cover pretty much

play07:50

everything you need to know about spanx

play07:51

security and json web token

play07:53

and how to secure an api using spam

play07:55

security and json web token

play07:57

and what you're looking at right now is

play07:59

the actual course and it's called json

play08:01

web token with spring security and

play08:02

angular and in that course i

play08:04

show how to secure an angular

play08:05

application using json web token

play08:08

amongst other things so that's the first

play08:10

thing i would highly encourage

play08:12

everyone to take unless you're really

play08:14

good with spring security

play08:15

um then you have to take this course

play08:17

before you actually take this

play08:19

swagger course and this will really help

play08:22

you a lot and even the base application

play08:23

is the same the base application that

play08:25

i'm using

play08:26

in this swagger course that application

play08:28

is coming from the security course so

play08:30

i would highly encourage everyone to

play08:32

take the security course

play08:33

and then come back to this course to

play08:35

understand how we can

play08:37

add swagger on top of the security of an

play08:40

api

play08:41

using a json web token or an api key the

play08:43

next thing to get the most out of this

play08:45

course is going to be to

play08:46

understand json web token because we're

play08:49

going to use json web token as our api

play08:51

key now

play08:52

an api key doesn't necessarily have to

play08:54

be a json web token

play08:55

it can be any string character that you

play08:57

use um

play08:58

preferably something that's not really

play09:00

easy to read but it can be any string of

play09:02

characters so make sure you understand

play09:04

json web token and what it means and

play09:06

what it why it's used to

play09:08

a secure api or why you can use it as an

play09:11

api key

play09:12

if you want to access the secure api

play09:14

application and the next you have to

play09:16

watch the lectures remember this is a

play09:18

lecture slash

play09:19

video based course so you have to watch

play09:22

all the lectures

play09:23

because this is where the content of the

play09:25

course uh actually is

play09:26

and this is where you will see what the

play09:28

instructor is doing

play09:30

uh and the explanation of everything

play09:31

that the instructor is doing as well and

play09:33

then next you have to use your online

play09:35

resources now

play09:37

i wouldn't say don't ask a question if

play09:38

you have a question but

play09:40

if something happens or you're stuck or

play09:41

you have a quick question

play09:43

i usually just google it because i take

play09:45

courses myself so

play09:46

if something happened to my application

play09:49

or i have an issue or there's something

play09:51

that i don't know

play09:52

or just something just popped in my head

play09:53

while i'm watching the course i would

play09:55

just google it

play09:56

and if i can't find the solution then i

play09:58

would contact the professor

play10:00

and or the instructor and see if they

play10:02

can answer the question but first i

play10:03

would

play10:04

definitely use my online resources to

play10:06

see if i can find a solution in the next

play10:09

i would say if you can you can try to

play10:11

code along

play10:12

now this is not really required mostly

play10:14

if you're already

play10:16

experienced so you don't necessarily

play10:18

have to code you can just watch what i'm

play10:19

doing but

play10:20

if you are a newbie then i would

play10:21

definitely say if you can code along

play10:24

then

play10:24

you know try to do that because this is

play10:26

going to build uh muscle memory

play10:28

along with actual memory of what you're

play10:30

learning and this is all going to stick

play10:32

together because you know you're going

play10:33

to be coding

play10:34

writing code and thinking as you do that

play10:36

as well so if you code along

play10:38

you'll develop those meso memories that

play10:40

you need and lastly you can go ahead and

play10:41

ask me a question

play10:43

so if you're stuck or your application

play10:44

is not running or you have just some

play10:46

questions or it can actually be any

play10:48

question then you can

play10:50

reach out in the q a or just send me a

play10:51

message um

play10:53

and then i will try to answer your

play10:55

questions so another thing is i

play10:57

answer every question in less than 24

play10:59

hours

play11:00

so you're not going to be waiting for

play11:01

days and me not getting back to you i

play11:03

try to answer all questions

play11:05

within 24 hours so those are the main

play11:07

points that i would say

play11:08

um you need to have covered if you want

play11:10

to take advantage of this course and get

play11:13

the best of this course like i said

play11:14

uh the security course really i would

play11:17

recommend it because

play11:18

the base application we're going to be

play11:20

using it's coming straight from this

play11:21

course

play11:22

and if you don't really know what spanx

play11:23

security is or you're not really good

play11:25

with spring security you don't really

play11:26

understand

play11:27

everything about it then i would

play11:29

definitely say take the security course

play11:30

first

play11:31

and then come and take this course

play11:38

one of the most important questions that

play11:40

we have to answer

play11:41

in this course is what exactly is an api

play11:44

documentation

play11:45

so an api documentation is really just

play11:48

something you can deliver to people

play11:50

and they can use that to understand

play11:52

exactly how to

play11:53

effectively use or integrate with your

play11:55

api so it's just a way that you're gonna

play11:58

be able to tell them

play11:58

say hey if you want to use my api here

play12:01

is the link or here's the url

play12:03

here are the different endpoints and

play12:05

this is how you call those endpoints and

play12:07

this is these are the parameters that

play12:08

you need to pass

play12:09

etc so it's really just something you

play12:11

can deliver which is why it says the

play12:13

technical content deliverable meaning

play12:15

you can deliver it

play12:16

and it has instructions on how to

play12:18

effectively use and integrate with

play12:19

your api and i have another more defined

play12:22

definition below and i'm not going to

play12:24

go ahead and read it to you but it's

play12:26

really just saying the same thing hey

play12:27

you want to be able to be as

play12:29

thorough as possible because there might

play12:31

be people that are not developers or

play12:33

they're not very tech savvy and they

play12:36

might still be able to

play12:37

you know go to your api and try to

play12:39

understand what it's doing and they

play12:40

should be able to understand exactly

play12:42

um how to use it and what it's doing i

play12:44

mean of course they're not gonna be

play12:45

able to understand everything uh like a

play12:47

software engineer or

play12:49

a developer but they should be able to

play12:50

understand exactly

play12:52

what the api is and what it's doing and

play12:54

how to actually use it to to some degree

play12:56

so it's really just a way for you to

play12:58

tell people or tell the world

play13:00

say hey this is my api and this is how

play13:03

you're supposed to use that obviously

play13:05

this is a very

play13:06

high level summary but you have to be as

play13:09

thorough as possible you have to define

play13:11

what kind of information you're going to

play13:12

give them back how they need to call

play13:14

your api

play13:15

if there is any authentication that they

play13:17

need to do and things like that so it's

play13:19

really just

play13:20

explaining to people how to use your api

play13:28

now the next question that we need to

play13:30

answer is

play13:31

why would you want to create an api

play13:34

documentation so why would you want to

play13:35

do that

play13:36

well there are many advantages to

play13:38

creating an api documentation

play13:39

for your api and i've read many articles

play13:42

about this there are

play13:43

dozens of reasons why you would want to

play13:45

do that but i'm just going to go over

play13:47

a few of them so the first one we're

play13:48

going to look at is going to be

play13:50

improved user adaption so if your api

play13:53

documentation

play13:54

is very nice and detailed and people

play13:56

come in and they can use it and it's

play13:58

very easy for them to use

play14:00

and what this is going to do is just

play14:01

gonna make it easier for them to

play14:03

use your api because it's very easy to

play14:06

understand

play14:07

and they love the experience so what's

play14:08

gonna happen is they're gonna tell

play14:10

other people about that they're gonna

play14:12

say hey you should try to

play14:13

use this api because they have a very

play14:15

nice api documentation

play14:17

and everything is going to be easy for

play14:18

you and this overall

play14:20

improved experience for your developers

play14:22

is going to go a very long way because

play14:24

you know they will just adapt or use

play14:26

your api

play14:27

more and more and more because the

play14:28

documentation is just so great it makes

play14:30

everything so easy to use it the next

play14:32

reason you might want to do that

play14:33

is to increase awareness so that ties

play14:36

into the first point

play14:37

so if one person uses your api and they

play14:41

like the experience

play14:42

and they're just gonna go ahead and tell

play14:44

other people and then

play14:45

more people are gonna come in and use

play14:47

our api which is what you want in the

play14:49

long run so those two points they're

play14:51

very close to each other

play14:52

the first one is just improve the

play14:54

experience and as a result

play14:56

of that more people are going to use it

play14:58

you know people are going to tell

play14:59

other people and then more people are

play15:01

just going to use your api which is what

play15:03

you want in the end

play15:04

the next reason you might want to do

play15:06

that is because it's going to save you a

play15:07

lot of time and

play15:08

a lot of money so let's imagine you have

play15:11

like some api

play15:12

that people can use but you don't have

play15:14

an api documentation so

play15:16

every time someone needs to use your api

play15:18

they're going to have to

play15:19

reach out to you or reach out to someone

play15:22

in the company or

play15:23

one of the owners of the api or someone

play15:25

who can answer the question so

play15:27

you know you might just be sending

play15:28

emails out and you would have to do that

play15:31

every time someone needs to use your api

play15:33

and that's just gonna cost you time

play15:35

and money so that's not the best way to

play15:37

do things when you can just

play15:38

say hey just go to this website or if

play15:41

they google the the google

play15:43

api or they you know hey there's this

play15:46

company or

play15:47

this service have a free api or even if

play15:49

it's not free

play15:50

or some api that they can use and then

play15:52

your api documentation page will come up

play15:55

so that will just save you

play15:57

uh not having to deal with people coming

play15:59

in and asking you directly or sending

play16:01

you email

play16:02

or whatever the case might be so it's

play16:03

going to save you a lot of time

play16:05

and with that a lot of money and the

play16:07

last point i want to touch on

play16:08

is maintenance so if everyone knows

play16:11

how the api works and what kind of

play16:13

methods and functions

play16:15

and domain that is supposed to return

play16:17

that's just going to make for

play16:18

a better production application because

play16:21

in the api documentation you have to be

play16:23

very very clear as to what your api is

play16:26

doing what's

play16:27

returning uh what are the domains like i

play16:30

said and what are some of the methods

play16:32

etc so since this is already clear it's

play16:35

gonna help

play16:36

developers internal developers making

play16:38

your api

play16:39

even better because everyone understands

play16:42

how the api works so that's gonna make

play16:45

it easier for maintenance whenever you

play16:47

have to make changes or to

play16:48

improve parts of the api and things like

play16:50

that and those are just four reasons

play16:52

there's

play16:53

way more reasons that i haven't even

play16:55

read or i don't even know about

play16:57

but it's definitely very important for

play16:58

you to have api documentation for

play17:01

api because that's just how you're going

play17:03

to tell people

play17:04

how to use it and integrate with it

play17:11

i want to give you guys an overview of

play17:13

the base application that we're going to

play17:15

start with

play17:16

and unlike all my other courses for this

play17:18

course we're not going to start from

play17:19

scratch because the focus is to create

play17:21

the square configuration so i'm going to

play17:22

give you a quick walkthrough of the

play17:24

application that we're going to start

play17:26

with so i'm just going to open my id

play17:28

here and let's take a look at the main

play17:30

application

play17:31

and by the way this application is more

play17:33

like an invoice management application

play17:36

and the idea is you have some sort of a

play17:38

store online or something where like you

play17:40

have transactions going on

play17:42

and then you have this api where you

play17:44

know your clients can come and

play17:46

look at invoices and things like that so

play17:48

this is the main application class you

play17:50

know nothing

play17:51

here has been changed it's still the

play17:52

same and let's go ahead and take a look

play17:54

at the resource so we only have the

play17:56

invoice resource here

play17:57

just gonna collapse this for now and you

play17:59

can see we have the typical risk

play18:01

controller because this is a rest avi

play18:03

and then we have this injection here

play18:05

where we do the dependency injection for

play18:07

the invoice service and in here

play18:09

we have all of the typical crud

play18:11

operations so add update

play18:13

get a list of everything get invoices

play18:16

for a specific customer

play18:17

we can also get an invoice if we know

play18:19

the invoice number

play18:20

and we can delete an invoice so that's

play18:22

just typical

play18:23

credit operation nothing too complicated

play18:26

here and

play18:26

this controller or this rest resource is

play18:29

calling

play18:30

the service here so let's go ahead and

play18:32

take a look at the service

play18:34

just gonna click here so the service is

play18:36

pretty simple we're just using the gpa

play18:38

repository for everything

play18:40

as you can see here it's imported here

play18:42

and we inject it in this class here

play18:44

and for saving an invoice we call the

play18:45

save to get all the invoices we call the

play18:48

find all

play18:49

um we can also find an invoice by the

play18:51

invoice number if we can find it we'll

play18:53

through this custom exception that i

play18:54

created and i'm going to take a look at

play18:56

this in a second

play18:57

and then we can you know update an

play18:59

invoice which is the same as the save

play19:00

the only difference is we have to pass

play19:02

in the

play19:02

primary key for that invoice and we can

play19:05

also delete an invoice

play19:06

or find invoice by specific customer

play19:09

this application

play19:10

is really very simple i didn't want to

play19:11

add any you know crazy logic

play19:13

and here just keeping it simple since

play19:15

the point is to just show you how to

play19:17

actually create the configuration for

play19:19

the actual documentation for this api

play19:22

so let's go ahead and take a look at the

play19:24

repository here

play19:26

since we're injecting it here and we're

play19:27

calling it here in this class so the

play19:29

repository is very typical

play19:30

we just extend the jpa repository pass

play19:33

the actual

play19:34

model and the primary key for it and

play19:36

then we're just using the jpa query

play19:38

language and our method name so that we

play19:40

can get the information that we need

play19:41

from the backend

play19:42

and all of this code is gonna be in the

play19:44

video resource so you can just go ahead

play19:46

and download it

play19:47

so that we can get started and then we

play19:49

also have the configuration for security

play19:51

here

play19:52

so i'm just gonna collapse this for now

play19:54

and scroll up

play19:55

so this is the typical security

play19:57

configuration that you would see for

play19:59

like a restful api

play20:01

build with spring boot so we're just

play20:03

allowing here

play20:04

all of the resources that swagger needs

play20:06

so that we don't black those

play20:08

and then everything else you need to be

play20:10

authenticated and this is just the

play20:11

configuration here

play20:13

with the http security and like i

play20:15

mentioned before

play20:16

the security course is a requirement for

play20:18

this course so if you want to understand

play20:20

exactly what's going on here in terms of

play20:22

the security configurations

play20:23

um then you just have to take that

play20:25

course and you know pretty much

play20:26

everything is going to be almost the

play20:27

same

play20:28

um i mean the course is going in depth

play20:31

into a lot of detail in terms of

play20:32

security

play20:33

but it's a requirement for this course

play20:35

so we're not going to be going over

play20:36

everything in details here but please

play20:38

what's going on here we have this filter

play20:41

that we injecting in this class and we

play20:43

pass it in here as a filter

play20:45

before every request and then we're just

play20:46

saying hey for our statement policy

play20:48

we're going to use stateless because

play20:50

we're not going to be using session or

play20:51

anything that we're going to track

play20:53

this is going to be you know every

play20:55

request comes in we check

play20:56

for an api key so we're not tracking any

play20:58

values or any any logged in users or

play21:00

anything like that and then down below

play21:02

on line 35 we say hey for every url that

play21:05

contains these

play21:06

paths just allow everyone to access them

play21:09

and then for any other request we need

play21:11

to authenticate so

play21:12

that way we can block every other access

play21:14

to the application

play21:16

only the swagger resources will pass our

play21:19

security filter

play21:20

and we can also take a look at this

play21:22

filter real quick

play21:23

and this filter is pretty much the same

play21:26

filter that was built in the security

play21:27

course because this course is required

play21:29

and all we're doing here is to check to

play21:31

make sure that every request coming

play21:33

in has on you know a valid api key

play21:36

before we allow it to go through the

play21:38

application so

play21:39

again if you want to understand this you

play21:41

have to take the security course because

play21:42

this can get a little bit complicated

play21:44

and the point of this course is to go

play21:46

over the configuration for swagger

play21:49

and other things that we have here is

play21:50

this utility class that we're using

play21:53

and it's using the same library that is

play21:54

used in the security course for

play21:57

uh you know decoding creating token and

play21:59

validating token and things like that

play22:01

and we have all of these methods in

play22:03

there like i mentioned

play22:04

you have to take the security course so

play22:05

that you can understand this so even

play22:07

though you will have this

play22:08

code and your as part of the course you

play22:10

need to understand

play22:12

everything that's going on here and to

play22:13

do that you have to take the security

play22:15

course

play22:15

since this course is like a continuation

play22:18

of the security course

play22:19

and then we already looked at the

play22:21

service we can take a look at that

play22:23

exception

play22:24

that i mentioned earlier so this

play22:26

exception is just extending the runtime

play22:27

exception

play22:28

and it's calling the super constructor

play22:31

so the constructor in this class

play22:33

passing in the message that we're

play22:34

passing in here and then we have the

play22:36

invoice entity so we can take a look at

play22:38

this real quick

play22:39

it's annotated with at entity because

play22:41

it's going to be saved in a database or

play22:42

mapped to a database

play22:44

and then we have the you know the

play22:45

typical id for the primary key

play22:48

the invoice number a list of product on

play22:50

a real products

play22:51

uh customer name and a total so very

play22:53

typical simple poju

play22:55

just trying to keep it simple and lastly

play22:57

we're going to take a look at our

play22:58

configuration so in our

play23:00

properties file uh you know i have the

play23:02

typical database

play23:04

this is digital secret obviously this is

play23:06

a joke

play23:07

our secret has to be fair secure and has

play23:10

to be very complicated string not just

play23:12

this word secret

play23:13

and then i have the credential for the

play23:15

database and some other

play23:16

configuration for jpa so

play23:20

that's pretty much everything for this

play23:21

application again if you don't know how

play23:23

to create an api you can take my other

play23:25

courses on api

play23:27

and if you want to understand the

play23:28

security aspect of this how we create

play23:30

the security configuration

play23:32

and this uh generality filter and this

play23:36

dudability utility class that we're

play23:37

using to validate the token

play23:39

then you have to take the security

play23:40

course which is a requirement for taking

play23:42

this course because in this course

play23:44

we're going to be focusing on creating

play23:46

the configuration for swag just make

play23:48

sure you remember that because we will

play23:49

not

play23:50

be building you know all of these in

play23:52

this course

play23:53

this is something that we already worked

play23:54

on in the previous course which is a

play23:56

requirement for this course so

play23:58

i hope you guys understand this and if

play24:00

you have any questions you can just

play24:01

reach out to me

play24:02

if you have any questions if you want

play24:04

coupons or anything like that i can give

play24:06

you like

play24:07

a discount on the security course but

play24:09

it's required for this course so

play24:10

any questions just reach out and i'll

play24:12

see you guys in the next one

play24:18

the first thing we need to do in our app

play24:20

to get started with swagger is to add

play24:22

the dependency for swagger so let's go

play24:24

into the

play24:25

pom file so i'm gonna click here and go

play24:27

all the way down to the pom file

play24:28

gonna collapse this for now and this is

play24:31

the dependency that we're gonna need for

play24:33

swagger so

play24:34

i'm gonna uncomment it so we're gonna be

play24:38

using the latest version which is

play24:39

version three so you have to make sure

play24:41

that you have this dependency here

play24:43

well you're gonna have the code so that

play24:44

events is probably going to be there

play24:46

already

play24:47

but this is the dependency that's going

play24:48

to bring all of these swagger jars that

play24:50

we need

play24:51

so that we can create our configuration

play24:53

so make sure you have this dependency

play24:56

in your application and then in the next

play24:58

lecture we're gonna actually get started

play25:00

with the actual configuration so i'll

play25:02

see you guys in the next one

play25:10

let's go ahead and add our swagger

play25:11

configuration class

play25:13

so i'm going to go ahead and close all

play25:14

of these tabs that i'm not using

play25:17

and you can create a new package for

play25:18

this like a swagger package

play25:21

but i'm just going to go ahead and put

play25:22

it in this configuration here so i'm

play25:24

going to put it in class

play25:26

and i'm just going to name it swagger

play25:28

config

play25:30

you can name it whatever you want just

play25:32

make sure it's something that's

play25:33

meaningful

play25:35

so first thing we want to do here since

play25:38

this is the configuration class we want

play25:39

to put the add configuration annotation

play25:41

on it

play25:42

so i'm going to do add and then

play25:44

configuration

play25:47

and this is supposed to come from spring

play25:48

and after that we have a lot of constant

play25:52

that we need

play25:53

so that we can create the content for

play25:54

the page itself so this is like

play25:56

something that's going to be up to you

play25:58

whatever you want to show when someone

play26:00

comes to your api

play26:01

what you want them to read about the api

play26:03

and things like that so i'm going to go

play26:04

ahead and copy and paste those

play26:06

constant because they're just constant

play26:08

like there's no logic or anything like

play26:09

that

play26:10

and then i'm gonna walk you through each

play26:11

one of them so i'm gonna go ahead and

play26:13

paste them i've already copied them

play26:15

and put them there as you can see

play26:16

they're all just constant so

play26:18

the first one is the security reference

play26:20

the security reference means

play26:22

you know what type of access it's going

play26:24

to be and that's a token

play26:25

access or api key access in the

play26:28

authorization description so

play26:30

we're going to tell the user what kind

play26:32

of authorization that they're getting

play26:34

and this is like full api permission

play26:36

because once you get a token then you

play26:37

can access all of the endpoints now

play26:40

if that specific token um was not going

play26:42

to access

play26:43

specific endpoints um but only like

play26:45

certain endpoints

play26:47

then you probably would have to change

play26:48

this to be hey this can access

play26:50

everything under

play26:51

slash whatever or you know or only

play26:54

this specific api and then the scope is

play26:57

unlimited

play26:57

you know the scope of the token is

play26:59

unlimited once you have a token then

play27:01

you can access everything and this is

play27:03

for the contact information so i put

play27:05

some dummy email here

play27:07

some dummy url here for our organization

play27:10

invoice.com

play27:11

uh and then to contact me it's gonna be

play27:13

like invoice api support you can click

play27:16

on that

play27:16

and it's gonna take you to this email or

play27:18

whatever so that's just going to be the

play27:20

text of it i believe

play27:21

and then the title of the page so that's

play27:23

invoice management api

play27:25

and this is the big title that you see

play27:27

like in bold

play27:28

once you first access the swagger ui

play27:30

page and we're going to see that in a

play27:32

second

play27:32

and this below is the description and

play27:34

inside the description

play27:36

you can actually put html elements as

play27:38

you can see i'm doing here i'm putting

play27:40

this note

play27:41

in bold so you just put a text in here

play27:44

you can see that all this plus to

play27:46

concat the text because it's really long

play27:48

and inside that string i also

play27:50

embed the actual link that i want as you

play27:53

can see here

play27:54

inside the whole string i put this

play27:56

anchor tag here

play27:57

and then put the actual url that i want

play28:00

whenever they click to get the

play28:01

actual api token which is going to be

play28:03

the front-end application

play28:05

then this is going to take them to this

play28:06

and you know i put the target to blank

play28:08

so that you can open in a new page and

play28:10

if i'm using this backlash is to escape

play28:13

this other uh double quote here because

play28:16

we're already inside of double quote we

play28:17

have the

play28:18

terms of service here so you know terms

play28:20

of service will go here

play28:21

whatever you want them to know the api

play28:23

version i just put 1.1

play28:25

but that would be the actual api version

play28:27

because you might have different version

play28:29

the license whatever the license is i'm

play28:31

using apache here because i don't have

play28:32

an actual license

play28:33

and that's the page for that so if they

play28:35

want to use more about the licensing

play28:37

and the path that we want to secure is

play28:40

going to be like every path in the

play28:41

application

play28:42

except the one that we configure in the

play28:44

security configuration

play28:46

if you guys remember so if we go to the

play28:47

secure configuration except those paths

play28:49

right here

play28:50

because um those are needed whenever we

play28:53

try to access the

play28:54

swagger ui because then he needs to go

play28:57

ahead and fetch some

play28:58

jars and things like that so we have to

play28:59

allow all of those

play29:01

endpoints by default for swagger to work

play29:03

otherwise you won't be able to fetch

play29:05

those

play29:05

pages and jars and then we have the tag

play29:09

and i'm going to show you what the tag

play29:11

is the tag is gonna be like

play29:13

the text that you see on top of the api

play29:16

before you

play29:16

start seeing the end points so we can

play29:18

have like an invoice tab or user tag or

play29:21

things like that

play29:22

but since we're only having invoice and

play29:24

um that's the only

play29:26

attack that we're going to have and i'm

play29:27

going to show you how this is going to

play29:29

come into play

play29:29

everything here is going to come into

play29:31

play once we have the ui and i can

play29:33

access the ui and we can see all of

play29:35

those in action

play29:36

then i'll go and say hey this text here

play29:38

represents this and this is that

play29:40

so you will understand um why we need

play29:42

all of these things and again you didn't

play29:44

have to create constant for

play29:45

all of these i just like to use constant

play29:48

because it's a little bit cleaner

play29:49

and you can actually put all of this in

play29:51

a separate class if you want like a

play29:52

swagger

play29:53

constant class or something like that

play29:55

because you know this is just constant

play29:57

just you know it's text nothing's going

play29:59

to change on this

play30:00

uh or every day or there's no logic in

play30:02

it or anything like that

play30:04

but i just put them in here and then i

play30:05

can just reach out and then

play30:07

pass them in my configuration so we just

play30:10

added the configuration class we haven't

play30:12

actually added the

play30:13

configuration for swagger yet and we

play30:15

just put in some constant are we gonna

play30:17

need

play30:17

to create this configuration so i'll see

play30:20

you guys in the next one

play30:26

i know i said that we could actually

play30:28

keep those constant in this class

play30:30

but i have a better idea so i decided to

play30:32

just put them in a constant class

play30:34

because

play30:34

they're taking a big chunk of this class

play30:36

as you can see here because it's too

play30:37

many of them so

play30:39

i'm gonna just cut everything here

play30:42

and then i'm gonna create a new package

play30:44

so in the main package

play30:46

i'm gonna add a new package and i'm just

play30:48

going to call it constant

play30:52

in that constant package i'm going to

play30:54

create a new class

play30:56

and i'm just going to call it swagger

play30:57

constant

play31:00

and i'm just going to add them here so

play31:02

i'm gonna do ctrl v

play31:04

and paste them here now i can use them

play31:06

by just calling this class and then call

play31:08

the actual

play31:09

constant that i need but i have to make

play31:11

those um public

play31:13

because right now they're all private so

play31:14

i'm just gonna copy this public here

play31:16

and then change those so i just changed

play31:18

all of them to public so that i can use

play31:20

them

play31:20

outside of this class at this point i

play31:23

can just close the class

play31:24

and then use everything from this class

play31:26

so let's just

play31:27

collapse this most of the swagger

play31:29

configuration

play31:30

is depending on one bin and this bin is

play31:34

called a docket bin and that's the bin

play31:36

that we have to define

play31:37

and then we can chain all of the rest of

play31:39

the configuration

play31:41

all of the api information and api keys

play31:44

information

play31:45

etc so let's go ahead and define these

play31:47

speeds i'm going to go down here and

play31:48

then i'm just going to do bin

play31:51

and then underneath here i'm going to

play31:52

create a public method

play31:54

i'm using the public and remember this

play31:56

is the docket so i'm going to do docket

play32:00

and i'm just going to call it api docket

play32:04

and it doesn't take any parameters and

play32:06

i'm just going to open and close for the

play32:08

body of the method

play32:09

and what i want to do here is just

play32:11

return a new docket so i'm going to

play32:13

return new dacket and then in here i

play32:16

have to pass in the

play32:18

swagger version so i'm going to do

play32:19

swagger version 2

play32:22

and we can click on this to take a look

play32:23

at it so if i hold ctrl on my

play32:26

keyboard and i just click that you can

play32:29

see here it's going to take us to the

play32:30

documentation for this

play32:32

so here you can see we're using swagger2

play32:34

not swagger 1.2

play32:36

and this one down below is for open api

play32:38

so we're not using that one but we're

play32:40

using

play32:40

swagger2 here so let's go ahead and

play32:42

close that and go back to the

play32:44

configuration

play32:45

so now you can actually chain all of the

play32:48

information in this configuration

play32:50

but i don't think that's the best way to

play32:51

do it and i'm going to show you what

play32:52

that looks like in a minute

play32:54

so you can actually chain everything you

play32:55

can do api info and then you pass in

play32:57

your api information

play32:59

so i'm not gonna just chain everything

play33:00

on that one configuration because it

play33:02

just makes it harder to read and

play33:03

understand

play33:04

so i'm gonna separate this and put as

play33:06

much as i can and separate methods

play33:09

and then we can just call those methods

play33:10

inside of here so i'm going to go down

play33:12

here and then i'm going to create a

play33:13

private method

play33:14

oops and this method is going to

play33:17

return the api info so i'm going to do

play33:20

api info as you can see here

play33:22

i'm going to give it the same name and

play33:23

it's not going to accept any parameters

play33:25

open and close

play33:26

the braces and what i have to return

play33:28

here is just a new api info so i'm going

play33:30

to do return

play33:31

a new api info and in that constructor

play33:35

i can actually pass in the information

play33:37

that i need to construct this

play33:38

constructor and the first one is going

play33:40

to be the api title

play33:43

and of course we need to import that so

play33:46

let's go here

play33:47

my action and import static another

play33:50

thing i want to do is to just go ahead

play33:51

and import everything from the

play33:54

let's see close this so we have the

play33:56

constant here

play33:57

so our constant so we know that we're

play33:59

going to use all this constant in this

play34:01

configuration class so we can just

play34:02

import everything

play34:03

so what i'm going to do here i'm going

play34:04

to go back here and in this list of

play34:06

import

play34:07

as you can see the api title is being

play34:09

imported here so i'm just going to put

play34:10

an asterisk

play34:11

so that i can import everything and then

play34:13

collapse this that way i don't have to

play34:14

do an import for

play34:16

every time i want to access something

play34:17

here it will be imported so the next

play34:20

thing i want to do for the api info so

play34:22

this is where you're passing the api

play34:23

information

play34:24

i have to pass in the api description

play34:27

and then

play34:28

pass in the api version so i'm going to

play34:30

do api

play34:31

version and i'm going gonna also pass in

play34:34

the terms of use so terms of service

play34:36

and then next i have to pass in a

play34:38

contact so i'm gonna do

play34:40

contact and this is gonna be a method

play34:42

that i'm gonna create in a second

play34:44

and then we can pass in the license and

play34:46

i'm just gonna put this on a new line

play34:49

and after the license we have to pass in

play34:52

um

play34:52

well let me just take a look at the

play34:54

constructor so you guys can understand

play34:55

what's going on here so you can see here

play34:57

we have to pass in in this constructor

play35:00

not this one though we're not using the

play35:02

deprecated one so in this one we have to

play35:04

pass in the title description version

play35:06

terms of service contact license

play35:08

license url and then we have to pass in

play35:10

an array of

play35:11

our list of version vendor extensions so

play35:15

we don't have any vendors extensions and

play35:17

i'm not using that now so we're just

play35:18

going to pass in

play35:19

an empty list for this but i just wanted

play35:21

to show you

play35:22

uh exactly what how do i know what i

play35:25

have to pass in is to just read the

play35:26

documentation

play35:28

or i can just use the quick help from

play35:30

the from the id so i can do control

play35:32

space

play35:33

and then it will show me uh what i have

play35:34

to pass in here so after the license

play35:36

we have to pass in the license url and

play35:39

then after that we can pass in the empty

play35:41

list and this empty list is going to be

play35:42

for the

play35:44

vendors extension or vendor extension so

play35:46

we can close this for now

play35:47

close this constant class and we can

play35:50

pass in the

play35:52

collection that empty list okay so

play35:55

because we don't have any windows

play35:56

extension

play35:57

so this is going to be the api info and

play35:59

we can use this api info

play36:00

to chain it in this bin right here okay

play36:03

so instead of doing all of this in that

play36:05

one

play36:06

bin or in this one method we're just

play36:08

going to separate them

play36:10

in in different methods and we can call

play36:12

those methods

play36:13

up here so next we have to define the

play36:15

contacts i'm gonna just copy this name

play36:18

i'm gonna scroll up here go down and

play36:21

then here i'm just gonna create another

play36:22

private and this is gonna be the contact

play36:24

so i'm going to do contact

play36:26

and i'm going to call it contact and of

play36:28

course it's not going to take me

play36:29

parameters

play36:30

and i need to return a new contact so

play36:32

i'm going to return new and for the

play36:33

contact we just have to pass in

play36:36

the contact name contact url and the

play36:38

contact email so here i'm just gonna do

play36:40

contact name

play36:41

pass in the contact url and then pass in

play36:45

the contact email

play36:46

and then i can put a semicolon here and

play36:49

then that's it you can see that you can

play36:51

actually chain

play36:52

all of these into this bin right here or

play36:54

in this method

play36:55

but i'm just decoupling everything so

play36:58

that in case you have

play36:59

to make changes then it becomes a lot

play37:01

easier for you as you can see here and

play37:03

like i said before

play37:04

most of the swagger documentation is

play37:06

going to be defined

play37:08

in this bin right here and you're going

play37:09

to see how this is going to look as we

play37:12

put all of this together so i'll see you

play37:14

guys in the next one

play37:20

so let's go ahead and continue with the

play37:21

configuration so what i have to define

play37:23

next

play37:24

is to tell swagger that i'm going to be

play37:26

using an api key

play37:28

and this api key is going to be in the

play37:30

header of every request and that header

play37:32

is going to be

play37:34

the authorization header so i have to go

play37:36

down here and then i'm going to do

play37:37

oops i'm going to do private

play37:41

and this is called api key so api key

play37:46

and i'm just going to call the api key

play37:48

and it doesn't take any parameters open

play37:50

and close curly braces for the body of

play37:51

the method and what i want to do here

play37:53

is to return a new api key so i'm going

play37:56

to do return

play37:57

new api key and then inside here i have

play38:00

to pass in the security reference so i'm

play38:03

gonna do security

play38:04

reference and this is the token access

play38:06

as you can see here

play38:07

and then i have to pass in the header

play38:09

name so i'm gonna do

play38:11

authorization okay as you can see here

play38:14

the string authorization which is gonna

play38:16

be the header

play38:18

and then i have to say the security

play38:22

scheme is going to be and the header so

play38:25

i have to do security scheme

play38:27

and and then i say header and then

play38:30

get the name from the header so this is

play38:32

how to find that you're going to be

play38:34

using an api key

play38:35

and that api key is going to be in the

play38:37

header which is going to be

play38:38

in the header of every request that gets

play38:41

sent to the actual

play38:42

backend or with the request and then

play38:44

next we have to define our security

play38:46

context so i'm going to go down here and

play38:47

then define another private method

play38:49

and this time i'm going to call it

play38:50

security context

play38:52

and i just name it security context and

play38:55

it's not going to accept any parameters

play38:57

and then here i have to return a

play38:59

security context so i'm going to do

play39:00

security context

play39:02

and then call the builder on that and

play39:03

then here i have to pass in

play39:05

a list of security references so i'm

play39:07

going to do

play39:08

security references and then here i'm

play39:10

going to pass in a security reference so

play39:13

security

play39:14

references or reference and i'm going to

play39:17

define this method in a second and then

play39:18

i can call build on that as well so that

play39:20

we can build

play39:21

our security context so in the security

play39:23

reference here which is going to be

play39:26

a list we're gonna define just the scope

play39:28

of that security

play39:30

uh or this key that we're gonna give to

play39:32

users so that they can access the api so

play39:34

this is gonna define the authorization

play39:36

scope

play39:36

uh the description of that as well so

play39:39

let's go ahead down here

play39:41

and let's define that method so i'm

play39:42

going to do private and i'm just going

play39:44

to copy this name

play39:46

and here i'm going to say remember this

play39:49

is a list so i'm going to do list

play39:51

and it's going to be a list of security

play39:54

reference

play39:56

and i'm going to call it security

play39:57

reference and then what i have to do

play39:59

here

play40:01

is to define the authorization scope so

play40:04

i'm going to do authorization scope

play40:05

and that's an array and i'm just going

play40:07

to give it a name here

play40:09

and then i have to set this equal to

play40:12

an array and then inside that array i'm

play40:14

going to pass in a new authorization

play40:16

scope

play40:17

and inside that constructor i can pass

play40:19

in the authorization scope that i

play40:20

defined which is unlimited

play40:22

and then here i can also pass the

play40:24

description so i'm going to do

play40:25

authorization

play40:26

description so once i have that in place

play40:28

i can just return it as a singleton list

play40:31

so i can go down here and then i can do

play40:32

return

play40:34

singleton list and then inside here i

play40:37

can just pass in a new security

play40:39

reference

play40:41

and then in this constructor of the

play40:42

security reference i can pass in the

play40:44

security reference

play40:46

and the scope that we just defined here

play40:48

which is the authorization scope

play40:50

so i'm gonna rename this

play40:53

uh because it's plural so we only have

play40:56

one

play40:56

i'm gonna rename this and then just pass

play40:59

it here

play41:00

okay so now we're done so at this point

play41:03

we have everything that we need to

play41:05

actually create

play41:06

this bin for the dacket so in the next

play41:09

lecture

play41:09

we're going to go ahead and use all of

play41:11

this and you're going to see how this is

play41:12

all going to come together

play41:14

and then we're going to create this bean

play41:16

and then after that we'll be able to see

play41:17

something

play41:18

when we try to access the url for daca

play41:21

so

play41:21

i'll see you guys in the next lecture

Rate This

5.0 / 5 (0 votes)

相关标签
Documentación APISwaggerAPI KeySeguridad APIAngularJWTSpring FrameworkSpring SecurityJavaRESTful API
您是否需要英文摘要?