Spring Security explained with no code

Bouali Ali
23 Nov 202316:50

Summary

TLDRIn this educational video, the host demystifies Spring Security by drawing a relatable analogy to organizing an exclusive party. The backend or REST API is likened to the party space, with endpoints as doors and entry points managed by controllers. Spring Boot Starter Security Dependency acts as the security company, while security filters are the bouncers at each door. The video explains the authentication process using JWT tokens and the role of the security configuration class. It guides viewers through the intricacies of Spring Security without coding, making it accessible to all.

Takeaways

  • πŸŽ‰ The video aims to demystify Spring Security without coding, using a party analogy to explain the concept.
  • 🏠 The 'party space' in the analogy represents the backend or REST API, which is the core area of the application.
  • πŸšͺ 'Doors and entry points' in the party are likened to endpoints in a Spring application, controlled by party controllers.
  • πŸ›‚ The 'security team' or 'bouncers' are equivalent to the security filters in Spring Security, ensuring only authorized access.
  • 🎫 The 'party ticket' is analogous to a JWT token in Spring Security, used to authenticate and authorize users.
  • πŸ” The 'security company' hired for the party represents the Spring Boot Starter Security dependency, providing a base for security setup.
  • πŸ› οΈ Custom security configurations are made by assigning 'security guys' to each door, similar to configuring security filters for each endpoint.
  • πŸ”— The 'authentication controller' is where users get their 'digital tickets', paralleling the process of obtaining JWT tokens.
  • πŸ”‘ The 'user details service' and 'password encoder' are crucial for verifying user credentials and are part of the Dow authentication provider.
  • πŸ”„ The 'security context holder' updates with each request, maintaining stateless session management as per the security contract.

Q & A

  • What is the main focus of the video?

    -The main focus of the video is to explain the architecture of Spring Security without writing any code, using a party analogy to make it more understandable.

  • What is the role of the 'party space' in the Spring Security analogy?

    -In the analogy, the 'party space' represents the backend or REST API, which is the core area where all the application's functionalities are centralized.

  • What does the term 'end points' refer to in the context of the video?

    -In the video, 'end points' are likened to the doors and entry points of a party space, which in the context of a Spring application, are the API endpoints controlled by the party controllers.

  • What is the function of the 'security team' in the video's analogy?

    -The 'security team' in the analogy represents the bouncers at a party, which in Spring Security terms, are the security filters that check if guests (users) are authorized to enter the system.

  • What is the digital equivalent of a party ticket in the Spring Security context?

    -In the Spring Security context, the digital equivalent of a party ticket is a JWT (JSON Web Token), which users present as proof of authentication.

  • What does the 'spring boot starter security' dependency represent in the analogy?

    -The 'spring boot starter security' dependency is compared to hiring a security company for the party, which comes with its own standard procedures but can be customized to fit specific security needs.

  • How does the video explain the role of the 'security filters' in Spring Security?

    -The video explains that security filters are like security guards at each door of a party, responsible for checking if the JWT token is valid, correctly formatted, and issued by the system.

  • What is the purpose of the 'authentication controller' in the Spring Security setup?

    -The 'authentication controller' is where users receive their digital tickets (JWT tokens), serving as the registration method for guests to gain access to the party (Spring application).

  • What is the significance of the 'security configuration class' in the video?

    -The 'security configuration class' is where the Spring application's security requirements are defined, similar to a meeting with a security company to outline how the party should be secured.

  • How does the video describe the process of authenticating a user in Spring Security?

    -The video describes the authentication process as a series of checks starting from the user's request, through the filter chain, to the authentication manager, and finally updating the security context holder if the authentication is successful.

  • What are the three exceptions that the video mentions during the authentication process?

    -The three exceptions mentioned during the authentication process are DisabledException, LockedException, and BadCredentialsException, which correspond to different authentication errors.

Outlines

00:00

πŸŽ‰ Introduction to Spring Security

The speaker welcomes viewers to the channel and introduces the topic of the video, which is an exploration of Spring Security without any coding. They mention a new Spring Boot course and invite viewers to join their community on social media. The analogy of hosting an exclusive party is used to explain securing a Spring application, where the backend or REST API is likened to the party space, and entry points are the doors. The need for security measures, or 'bouncers', is emphasized to ensure only authorized guests enter, using JWT tokens as digital tickets.

05:01

πŸ” Deep Dive into Spring Security Configuration

The speaker elaborates on the party analogy, explaining the role of the security company, which corresponds to the Spring Boot Starter Security dependency. They discuss the importance of configuring security filters for each entry point and the process of validating JWT tokens. The video then transitions into a detailed explanation of the security configuration class in Spring Security, which is akin to a meeting with the security company to set up specific security measures. The concept of a security filter chain is introduced as a contract between the party organizers and the security company, outlining the rules for access control.

10:04

πŸ› οΈ Understanding Authentication in Spring Security

This section delves into the internal workings of Spring Security's authentication process. The speaker uses a diagram to illustrate how an authentication request is handled, starting from the user's credentials to the backend, through the filter chain, and finally to the authentication controller. The role of the authentication manager and the provider manager is explained, along with the authentication provider's responsibility for verifying user details. The process of handling exceptions such as disabled, locked, or bad credentials is also discussed.

15:04

πŸ”„ JWT Token Validation and Security Context Update

The final paragraph focuses on the process of JWT token validation within the Spring Security framework. The speaker describes how a request with a JWT token is processed, including the validation of the token and the subsequent user lookup using the user details service. The importance of updating the security context holder after successful authentication is highlighted. The speaker concludes by summarizing the key components of securing web applications and REST APIs with Spring Security, encouraging viewers to engage with them on social media and support the channel.

Mindmap

Keywords

πŸ’‘Spring Security

Spring Security is a powerful and highly customizable authentication and access-control framework for Java applications. It is designed to protect web applications from unauthorized access and other security threats. In the video, Spring Security is the central theme, with the speaker aiming to demystify its architecture without writing any code. The script uses an analogy of a party to explain how Spring Security works, emphasizing its role in controlling access to the 'party space,' which represents the backend or REST API of an application.

πŸ’‘Authentication

Authentication is the process of verifying the identity of a user or device. It is a critical aspect of security, ensuring that only authorized individuals can access a system. In the context of the video, authentication is likened to the process of checking party guests' tickets (JWT tokens) at the door, ensuring that they are who they claim to be. The video explains that Spring Security uses an 'authentication controller' to issue digital tickets and that the 'security filters' check these tickets upon entry.

πŸ’‘JWT Token

A JSON Web Token (JWT) is a compact, URL-safe means of representing claims to be transferred between two parties. The token is digitally signed and can be verified and trusted because it is difficult to alter without breaking the signature. In the video, JWT tokens are compared to party tickets, serving as a digital proof of identity and authorization. The script mentions that presenting a JWT token is akin to showing a ticket at the entrance of a party.

πŸ’‘Security Filters

Security filters are components in the Spring Security framework that intercept and process incoming HTTP requests to enforce security constraints. They act as gatekeepers, checking for the presence and validity of security credentials. In the video script, security filters are compared to security personnel at a party's entrance points, ensuring that each guest has a valid ticket and is authorized to enter.

πŸ’‘Spring Boot Starter Security

Spring Boot Starter Security is a dependency that provides a comprehensive security configuration for Spring Boot applications. It simplifies the setup of security mechanisms by providing default configurations and allowing for easy customization. The video script uses the analogy of hiring a security company for a party, where the 'security company' represents the Spring Boot Starter Security dependency that comes with its own standard procedures, but can be tailored to specific needs.

πŸ’‘Security Configuration Class

In Spring Security, the security configuration class is where developers define the security rules and components used by the application. It is a central place to configure how security is enforced, including which users have access to which resources. In the video, the security configuration class is likened to a meeting with a security company to plan how to secure the party, detailing how to validate and process security credentials.

πŸ’‘Authentication Manager

The Authentication Manager in Spring Security is responsible for the actual authentication process. It takes an authentication request, validates the credentials, and returns an Authentication object if successful. In the video, the Authentication Manager is depicted as the core component that checks the validity of the 'tickets' (JWT tokens) and ensures that only legitimate guests gain access to the party.

πŸ’‘UserDetailsService

UserDetailsService is an interface in Spring Security that allows for the retrieval of user-specific data, typically from a database or other persistent storage. It is used to load user-specific information, such as roles and authorities, which are then used in the authentication process. In the script, UserDetailsService is compared to a service that checks the guest list at a party to confirm if the bearer of a ticket is indeed on the list and has the right to enter.

πŸ’‘SecurityContextHolder

SecurityContextHolder is a central class in Spring Security that holds the SecurityContext, which contains the Authentication object representing the currently authenticated user. It is used to access security-related information during the processing of a request. In the video, SecurityContextHolder is likened to a system that keeps track of who is inside the party, ensuring that only authenticated and authorized individuals are recognized as legitimate guests.

πŸ’‘Stateless Session Management

Stateless session management refers to a session management approach where the server does not store any session data on the server-side. Instead, all necessary information is encoded into a token that is sent to the client. In the context of the video, stateless session management is compared to a party scenario where each guest must present a ticket every time they wish to enter, regardless of whether they have already been inside.

Highlights

Introduction to a video tutorial on Spring Security without coding

Announcement of a new Spring Boot course

Invitation to join the community on social media

Analogizing a Spring application to an exclusive party

Explanation of backend or REST API as the party space

Endpoints in a Spring application compared to party entry points

Party controllers managing specific aspects of the event

Need for a security system to check guest authorization

Authentication controller as the digital ticket registration method

JWT token as a digital party ticket

Spring Boot Starter Security as the security company

Security filters as security personnel at each door

Process of checking the JWT token for entry

Security configuration class as the meeting with the security company

Security filter chain as the contract between organizers and security

Stateless session management in Spring Security

Authentication provider and its role in the security setup

JWT authentication filter's role in the filter chain

Authentication process in Spring Security with user credentials

Exceptions handling in authentication process

Provider manager and its use of authentication provider

UserDetailsService for fetching user information

SecurityContextHolder for holding security context

JWT token validation process by the JWT authentication filter

Dispatcher servlet's role in handling requests

Summary of securing web applications with Spring Security

Call to action for subscribing, liking, and sharing the video

Transcripts

play00:00

hey what's up tiies welcome back to my

play00:02

channel I'm thrilled to have you here

play00:05

for today's video where we were going to

play00:07

unravel the mysteries of Spring Security

play00:10

the best part of it we're doing it

play00:12

without writing a single line of code I

play00:15

know security can sometimes feel like a

play00:18

complex puzzle but fear not we're

play00:21

breaking down the Spring Security

play00:23

architecture step by step using a simple

play00:26

analogy by the end of this video you'll

play00:28

have a crystal clear understanding of

play00:31

how security really works but before we

play00:34

dive into the details I've got something

play00:36

exciting to share check out my brand new

play00:38

spring boot course it's your ticket to

play00:41

mastering the framework deep intricacies

play00:44

the link is in the description below of

play00:46

this video also don't miss out on

play00:48

joining our growing community on social

play00:50

media all the links you need are right

play00:53

down there so let's connect now if

play00:56

you're new to my channel hit that

play00:58

subscribe button give this video a

play01:01

thumbs up and share it let's spread the

play01:03

knowledge together and help this channel

play01:06

grow imagine you're throwing the

play01:08

ultimate party but you want to make sure

play01:11

it's exclusive just like organizing a

play01:14

party involves meticulous planning

play01:16

securing a spring application involves

play01:19

setting up the perfect entrance let's

play01:22

break down the party analogy into the

play01:24

word of Spring Security without diving

play01:27

into the code first things first think

play01:30

of your party space as your backend or

play01:33

rest API it's the heart of the event

play01:36

where everything comes together now this

play01:39

fantastic party space has doors and

play01:42

entry points in the take word we call

play01:45

them end points these are controlled by

play01:48

the party controllers each managing

play01:51

specific aspects of the event to avoid

play01:55

any Uninvited project egg scenarios we

play01:58

need a system to check check if guests

play02:00

are authorized enter the security team

play02:04

the bouncers of our digital party just

play02:07

like real world party needs tickets our

play02:09

spring application has a registration

play02:12

method in the authentication controller

play02:15

this is where guests get their digital

play02:18

tickets but having a ticket isn't enough

play02:21

you need to show it at the door and

play02:25

think of this as presenting a JWT token

play02:28

which is a digital version of your party

play02:31

ticket now to make sure only cool crowd

play02:35

gets in we hire a security company this

play02:38

is the spring boot starter security

play02:41

dependency they come with their own way

play02:44

of doing things but we want a more

play02:46

advanced setup so imagine assigning a

play02:50

security guy to each door or each entry

play02:53

point of our party space these are like

play02:56

the security filters in our Spring

play02:58

Security setup so their job is to check

play03:01

everything first does the person have a

play03:04

ticket which is our JWT token is the

play03:07

ticket correctly formatted for today's

play03:09

party then check the username and code

play03:12

on the ticket and finally ensure the

play03:15

ticket is legit and issued by us with

play03:19

the user registered in our internal

play03:22

system so if everything checks out we

play03:25

update our system let the person in and

play03:28

even give a route Guide to the party

play03:31

otherwise they just get rejection

play03:34

treatment so now let's break down the

play03:36

security diagram and see how it all

play03:39

works then I'll guide you step by step

play03:42

through each step and share the

play03:45

corresponding code links after deciding

play03:47

to organize the party this is the

play03:49

equivalent of having our backend or IPI

play03:53

ready and then calling a security

play03:56

company is the equivalent of adding the

play03:59

spring boot starter security dependency

play04:01

then we need to have a meeting together

play04:04

with the security company to explain the

play04:06

way we want to secure our party and how

play04:09

to get different information and

play04:12

validate them so this is the equivalent

play04:14

of the security configuration class

play04:17

where we need to tell spring what are

play04:20

the different components to use in order

play04:23

to secure the API now let me explain to

play04:27

you the configuration class so as you

play04:29

can see here this is the spring or the

play04:31

security configuration class from the

play04:34

previous tutorial so I will leave you

play04:36

also the link in the description of this

play04:38

video so here let's go directly to the

play04:40

security filter chain which is let's

play04:43

imagine it as the contract between the

play04:45

party organizers and security company so

play04:48

here we tell them that we want to

play04:51

disable the csrf and then we want to

play04:55

authorize some people so here we said

play04:58

that we have list of people that we want

play05:01

to authorize by default no matter what

play05:04

they don't require anything so this is

play05:06

how we have here permit all and then as

play05:09

you can see here for example in order to

play05:12

access this space or this VIP space the

play05:15

guy or like the the person presented in

play05:18

front of you needs to have for example

play05:21

let's say here or let's try to replace

play05:23

this role with a special ticket all

play05:26

right same here and then finally we say

play05:29

that that any request needs to be

play05:32

authenticated so this is the most

play05:34

important part after that as part of our

play05:37

contract with a with security company we

play05:39

want to tell them that the session

play05:42

management we want it to be stateless

play05:44

this means that each time even if a

play05:47

person was authenticated before and

play05:50

leaves the party parameters and wants to

play05:52

get back again so we need to recheck

play05:56

everything from scratch all right so

play05:58

each time we have have a person in and

play06:01

then we forget about him so we don't

play06:02

save any state so let saying okay I

play06:05

remember this guy he was checked in

play06:06

before no it's not the case every time

play06:09

someone wants to get in even if he was

play06:13

inside before we need to double check

play06:15

again then we need to determine what is

play06:18

the authentication provider so how to do

play06:21

that how to check the system how to

play06:23

check if the user has the correct ticket

play06:27

if the information of this user are the

play06:29

same as in the ticket or not and then

play06:32

here adding a filter before means that

play06:35

this is where we want to place our

play06:37

security guys so for example saying I

play06:40

want to have one or two people in the in

play06:42

the front or in the main entrance and so

play06:45

and so forth and the rest right here is

play06:47

just for the lout so now this is how

play06:50

this is how we can determine the

play06:52

contract between us or between the party

play06:55

organizers and the security company so

play06:58

it's just it's it's all about telling

play07:00

them how to do things because they H

play07:03

they have their own way of doing things

play07:06

but we want something specific for us

play07:08

all right now you get the analogy but we

play07:12

need to understand how things internally

play07:15

work so let me dive you now through a

play07:18

diagram that explains how the

play07:20

authentication really works and how to

play07:23

authenticate a user in Spring secret so

play07:26

as you can see here we have this diagram

play07:29

and and here we see that first of all we

play07:31

start with an authentication request

play07:33

where the user will send his credentials

play07:36

the credentials are username and

play07:38

password the request will reach our

play07:41

backend and the first thing that will

play07:43

get executed is our filter chain so we

play07:47

have many filters in our spring

play07:49

application even without declaring or

play07:51

creating a specific filters spring has

play07:54

its own filters and among these filters

play07:58

we see here here that we have our once

play08:01

per request filter which is the JWT

play08:04

authentication filter that we created in

play08:07

our tutorial and then the request will

play08:10

be forwarded to authent to the

play08:12

authentication controller since for the

play08:16

authentication if you remember we have a

play08:18

check to check if this request is coming

play08:21

for the authentication and if the answer

play08:24

is yes we don't execute the filter so we

play08:27

just pass to the next filter chain and

play08:29

execute the rest so here we come

play08:32

directly to the authentication

play08:33

controller which has a dependency which

play08:36

is the authentication manager so we

play08:39

inject our authentication manager inside

play08:42

our authentication controller and then

play08:44

it's directly in the service but I just

play08:47

didn't want to make this diagram too

play08:49

long so from the authentication

play08:51

controller we are calling the

play08:52

authentication Service and then we have

play08:55

the authentication manager as a

play08:57

dependency in the auth authentication

play08:59

Service so the authentication manager it

play09:03

attempts to authenticate the past

play09:05

authentication object returning a fully

play09:08

populated authentication object

play09:10

including granted authorities if

play09:13

successful and in case something is not

play09:16

working or something wrong with

play09:17

authentication we will get one of these

play09:20

three exceptions first whether a

play09:23

disabled exception a locked exception or

play09:25

a bad credentials exceptions so in case

play09:29

want to handle exceptions for the

play09:31

authentications these are the three

play09:33

exceptions that you need to take care of

play09:36

so then this authentication manager is

play09:39

an interface and spring has a default

play09:42

implementation which is a class called

play09:44

provider manager and this is the

play09:46

commonly used in the spring applications

play09:49

and then these provider managers needs

play09:52

or uses an authentication provider and

play09:55

the authentication provider is the

play09:58

object of the Bean that we configured in

play10:00

our Bean configuration or application

play10:03

configuration class so I'm talking about

play10:06

this one so if you go to the application

play10:08

config here you have an implementation

play10:11

or a bean of type authentication

play10:13

provider and here we are providing an

play10:16

implementation of this authentication

play10:19

provider which is already an interface

play10:22

all right and among the implementation

play10:25

of this interface we have the Dow

play10:28

authentic ation provider so now if we go

play10:31

back here we have or we are providing

play10:33

our Dow authentication provider and this

play10:36

Dow authentication provider needs two

play10:39

dependencies or two services at least

play10:42

first we need the password encoder this

play10:44

means how we encoded the password while

play10:48

persisting it to our persistence system

play10:51

so our persistence system it might be

play10:54

different things and also we need an

play10:57

object of type user detail service and

play11:00

this user details service will try to

play11:03

fetch the user and here we need to to

play11:06

have our own implementation of the user

play11:09

detailed service interface and its

play11:11

implementation provided by us it will

play11:14

try to connect to a storage system it

play11:16

might be like a post degree SQL like the

play11:18

case we had in our tutorial or or a

play11:21

mongodb or any type of other storage and

play11:24

this user detailed service will return

play11:27

an object of type user details and this

play11:30

user details will provide spring with

play11:33

the following information username

play11:35

password authorities is enabled is

play11:38

account non-expired is account nonlocked

play11:41

and is credentials non-expired and from

play11:44

is account nonlocked and is enabled

play11:47

properties you can see here that this is

play11:49

why and how we are returning the

play11:51

disabled exception and the locked

play11:53

exception and then of course our

play11:56

provider manager will pass an an object

play11:59

of you of type username password

play12:02

authentication and then it will be

play12:04

passed to the Dow authentication in

play12:06

order to perform the authentication

play12:08

process once everything is okay and once

play12:11

everything is fine we update the

play12:14

security context holder so the security

play12:17

context holder is a spring object that

play12:20

holds some information it has a security

play12:23

context and inside this security context

play12:26

we have an object of type Authentication

play12:29

which is the same object that will be

play12:31

returned by our authentication manager

play12:34

and this authentication object has the

play12:36

principle credentials and authorities

play12:39

which are returned from these user

play12:42

details right here all right so this is

play12:44

the process of authentication in Spring

play12:48

Security Now if we send a request with a

play12:51

JWT in the header with a token in the

play12:54

header and we want to perform and see

play12:57

how this token will will get verified

play13:00

and checked by our filters so now let's

play13:03

jump into it now I will show you a

play13:05

different diagram showing how this

play13:08

process or this flow works all right so

play13:11

now in the next diagram we will see how

play13:15

the filter really works so now we have a

play13:18

user that will send a request to our

play13:21

backend so it might be a get post past

play13:24

uh patch delete and so and so forth any

play13:27

type of http request or HTTP actions so

play13:31

we said that the first thing that will

play13:34

be hit in the application is the filter

play13:37

or the filters that we have in our

play13:39

application and among these filters we

play13:42

have an object of type once per request

play13:45

filter which is the JWT authentication

play13:47

filter we spoke about it in just few

play13:50

seconds ago and here we have something

play13:53

so if the token exists then what we need

play13:57

to do we need to go to the JWT service

play14:01

and we need to validate the token here

play14:03

we have two scenarios the first one if

play14:06

the token is not valid so then an

play14:10

exception of type token and valid

play14:12

exception will be thrown and this will

play14:14

be the response to our final user so as

play14:17

you can see here it's already in Red so

play14:20

this is an exception then if the token

play14:23

is valid we will go or we will call the

play14:25

user details service in order to find

play14:28

the user from the database after

play14:31

extracting the user email or the subject

play14:34

from our token using this JWT service

play14:38

then once the user details is called or

play14:42

the user details service is called we

play14:44

have two scenarios so let's start with

play14:47

the exceptional one so if the user is

play14:49

not found then we will throw a user not

play14:52

found exception and again we will return

play14:55

back an exception or an error to our

play14:58

customer if not if the user is already

play15:01

found what we need to do next is

play15:04

updating the security context holder

play15:07

once again so the filter as we showed in

play15:10

the previous diagram that's why here

play15:13

it's also linked to the on per request

play15:15

filter or the JWT authentication filter

play15:18

because any of them whether the provider

play15:21

manager or the our authentication

play15:23

manager object can update the security

play15:25

context holder or as well as like this

play15:28

the same thing for the JWT

play15:30

authentication filter so then what we do

play15:34

we update our security context holder

play15:37

and we update the authentication and

play15:39

then we pass the request to the

play15:41

dispatcher serlet and then the

play15:43

dispatcher serlet will take will take

play15:46

care of dispatching the request to the

play15:49

correct controller of our API and then

play15:53

of course it will be treated it will go

play15:55

to the service database and so on so

play15:57

forth and then we will return back a

play16:00

response to the final user otherwise you

play16:03

see here that in which cases we have or

play16:06

we might have a 403 all right now I

play16:10

think springing security and securing

play16:13

web applications and rest apis is much

play16:16

much much easier with this breakdown of

play16:20

the different components of security I

play16:22

hope you like this video and don't

play16:25

forget to join me and connect with me on

play16:27

social media and of course don't forget

play16:30

to like share and subscribe to my

play16:32

YouTube channel in order to help me grow

play16:35

this Channel and spread the knowledge to

play16:37

more and more people thank you so much

play16:40

and see you next

play16:49

time

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Spring SecurityWeb SecurityAPI ProtectionAuthenticationAuthorizationSecurity FiltersJWT TokensSecurity AnalogyTech TutorialDeveloper Guide