#30 Spring Security | Custom Login

Telusko
23 Jul 202415:11

Summary

TLDRThis video script delves into customizing a default login form in a Spring Web application. It covers the basics of Spring Web, the role of filters in security, and the concept of sessions. The tutorial guides through changing default usernames and passwords, understanding how the server handles security, and exploring session IDs. It also demonstrates logging in through Postman, showcasing the practical application of Spring Security filters and session management.

Takeaways

  • 🔒 The video discusses changing the default username and password in a Spring application for enhanced security.
  • 🛡️ It explains how Spring Security handles the security aspect through a series of filters that form a filter chain.
  • 🕵️‍♂️ The script clarifies that the front controller, also known as the dispatcher servlet, plays a crucial role in directing requests to the appropriate controllers.
  • 📜 The video mentions that by default, Spring Security provides a login form and handles authentication, but custom filters can be added for further customization.
  • 🔄 It demonstrates how session management works in Spring, ensuring that once a user is logged in, they can access the application without re-authenticating for each request.
  • 🔎 The script shows how to view and verify the session ID through developer tools in a web browser, which is crucial for understanding session persistence.
  • 🛑 The video covers the process of logging in through Postman, a REST client, by sending a GET request with the necessary authorization headers.
  • 🔄 It explains that upon successful login, a new session ID is generated, which is different for each login instance.
  • 📝 The script guides on how to change the default username and password by setting `spring.security.user.name` and `spring.security.user.password` in the application properties.
  • 🔄 It shows that after changing the username and password, the application still maintains session continuity, allowing for multiple accesses without re-login.
  • 🔒 The video concludes by emphasizing the importance of understanding how filters work in Spring Security for anyone looking to customize authentication mechanisms.

Q & A

  • What is the default username and password for the login form as mentioned in the script?

    -The default username is 'user', and the password is displayed in the console.

  • Why does the script mention changing the default username and password?

    -The speaker wants to have their own username and password for security reasons instead of using the default ones provided by the system.

  • Who is responsible for handling security in the context of the script?

    -Spring Security is responsible for handling security, which includes adding its own filters to the filter chain.

  • What is the role of the 'dispatcher servlet' in the context of the script?

    -The dispatcher servlet acts as a front controller that routes incoming requests to the appropriate controller after passing through the filter chain.

  • How does the session ID work in the application described in the script?

    -The session ID is part of a cookie that is created when a user logs in and remains the same across different requests and URLs until the user logs out or the cookies are deleted.

  • Can the session ID be viewed by the user?

    -Yes, the session ID can be viewed by the user through the developer tools in their browser, specifically in the cookies section of the network tab.

  • What is the purpose of the filter chain in the context of the script?

    -The filter chain processes requests in a sequential manner, with each filter having the opportunity to modify the request or response, authenticate the user, or perform other security checks.

  • How can a user log in to the application using Postman?

    -A user can log in using Postman by sending a GET request with the necessary authorization headers containing the username and password using the Basic Auth method.

  • What happens when a user logs in with the correct credentials through Postman?

    -Upon logging in with the correct credentials, the user receives a 200 status code, and a new session ID is generated for the authenticated session.

  • How can the speaker customize the username and password for the login form?

    -The speaker can customize the username and password by specifying 'spring.security.user.name' and 'spring.security.user.password' in the application properties file.

  • What is the significance of filters in Spring Security?

    -Filters in Spring Security are crucial for security as they form a chain that checks for authentication, authorization, and other security measures before a request reaches the controller.

Outlines

00:00

🔒 Understanding Spring Security and Custom Login

The script discusses the process of customizing the default login form in a Spring application. It begins by explaining the default username and password setup and the desire to change these to a custom username and password. The video aims to clarify who handles security, the role of the login page, and the concept of sessions, which allow users to access the application without re-authenticating for each request. It also touches on the possibility of using tools like Postman to perform login operations outside of a traditional form. The explanation includes a basic overview of how Spring controllers work, the role of servlets, and the function of the front controller or dispatcher servlet in the request process. The importance of filters in the Spring Security framework is highlighted, showing how they form a chain that processes each request to enforce security measures.

05:00

🛠️ Exploring Filters and Session Management in Spring Security

This paragraph delves deeper into the filter chain mechanism of Spring Security, explaining how requests are processed through a series of filters that can alter or inspect the request and response. The script mentions specific filters like the UsernamePasswordAuthenticationFilter and how they contribute to the login process. It also demonstrates how to check the session ID through the browser's developer tools and how to print the session ID within the application using the HttpServletRequest object. The paragraph emphasizes the persistence of the session across multiple requests and the automatic logout that occurs when cookies are deleted.

10:02

🔄 Customizing Username and Password in Spring Security Configuration

The script outlines how to change the default username and password used by Spring Security. It details the steps to set custom credentials through application properties and demonstrates testing the changes by attempting to log in with both the default and custom credentials. The video also discusses the implications of using multiple users with different credentials, which will be covered in future videos. The paragraph concludes with a successful login using the new credentials and the observation that the session remains active across multiple requests, indicating the session ID's role in maintaining user state.

15:03

📡 Logging In Through Postman and Future Exploration of Request Types

The final paragraph of the script discusses using Postman to send a GET request to the application's homepage, resulting in an unauthorized error due to the lack of authentication credentials. It demonstrates how to use the authorization tab in Postman to send the correct username and password to gain access. The video shows a successful login via Postman, resulting in a new session ID. The script ends with a teaser for future videos that will explore whether different types of requests, such as POST requests, can also be used for authentication.

Mindmap

Keywords

💡Spring Web

Spring Web refers to a part of the Spring Framework that provides a comprehensive way to build web applications. It is integral to the video's theme as it discusses how to customize and interact with web applications using Spring Web. For instance, the script mentions 'Spring web GS into two parts one is a servlet way which we are doing now and then there's also reactive way we are not focusing on reactive here, spring reactive we are only focusing on, the spring web now'.

💡Controller

In the context of the video, a controller in Spring Web is a class that handles HTTP requests and returns a response. It is a fundamental concept as the script explains how requests are processed through controllers like 'home controller' or 'check balance controller'. The video's narrative revolves around customizing these controllers to handle security and sessions.

💡Servlet Container

A Servlet Container is a server-side component that can run servlets. In the video, it is mentioned that 'Tomcat because of that servlets so all these things all, your controller gets converted into servlets'. It is key to understanding how Spring Web applications are deployed and served.

💡Dispatcher Servlet

The Dispatcher Servlet, also known as the front controller in the video, is responsible for dispatching requests to the appropriate handler (controller). It is a central concept as the script describes how 'every request from the client when is going goes to the controller which you created it goes to, the disp salet', highlighting its role in the request handling process.

💡Filter Chain

A Filter Chain in the video refers to a sequence of filters that process a request before it reaches the controller. The script explains that 'request goes from the client to, the filter first this is your filter, chain and then from here it goes to the, front controller'. Filters are crucial for implementing security features like authentication.

💡Spring Security

Spring Security is a powerful and highly customizable authentication and access-control framework. The video discusses adding Spring Security to a web application, which introduces its own filters into the filter chain, as mentioned in 'when you add Spring Security we are, calling those things those things are, responsible'.

💡Session

A session in the context of the video is a way to maintain state information between requests. The script delves into how 'session so when you log in I can access, the same page multiple times', indicating the importance of sessions in user authentication and maintaining logged-in state.

💡Session ID

The Session ID is a unique identifier assigned to a user's session. The video script mentions checking the session ID 'if I want to see the, session ID can I can I do that so I want, to check that as well', which is essential for understanding how sessions are tracked and maintained across requests.

💡Username and Password

The script discusses customizing the default username and password for a login form 'I want to change one thing and then during that, process we'll understand different, concepts in between uh the thing which I, want to change is the username and, password'. This is central to the video's focus on security and user authentication.

💡Postman

Postman is a REST client used for testing APIs. The video script includes a demonstration of using Postman to authenticate with a web application 'what if I want to do that from a postman can I do, that so Postman basically any rest, client uh can I do that'. It illustrates an alternative method to test authentication mechanisms.

Highlights

Introduction to changing the default username and password in a Spring application for enhanced security.

Understanding the role of controllers and the login process in Spring Web applications.

Exploring the concept of session management and its importance in maintaining user authentication state.

Demonstration of how to view and verify session IDs in a web application.

Explanation of the filter chain mechanism in Spring Security and its role in processing HTTP requests.

Discussion on customizing Spring Security filters for specific application needs.

Illustration of how to bypass the default login form using Postman or other REST clients.

Technical walkthrough of the servlet container's role in running Spring controllers.

Clarification on the front controller's function in the Spring MVC architecture.

Insight into the automatic generation of login forms by Spring Security filters.

Guide on modifying the application properties to set custom username and password for authentication.

Verification of the new username and password through the application's login process.

Explanation of how session IDs are generated and managed across different user sessions.

Practical example of printing session IDs within the application's response for debugging purposes.

Demonstration of logging into a Spring application using Postman with basic authentication.

Conclusion summarizing the key points covered in the video on Spring Security and session management.

Transcripts

play00:00

so now we got a default form using which

play00:02

you can log in right but then I want to

play00:05

change one thing and then during that

play00:07

process we'll understand different

play00:08

concepts in between uh the thing which I

play00:10

want to change is the username and

play00:12

password because by default you are

play00:14

getting a username as user and the

play00:16

password you are getting in the console

play00:19

I don't want it I want to have my own

play00:21

password so that's one thing but then

play00:24

there are certain things which you have

play00:25

to understand in between first of all

play00:27

who is handling the security part here

play00:29

how exactly uh when we are defining the

play00:32

controllers someone else I mean that

play00:35

someone else is your page the login page

play00:37

is coming in between how that is

play00:39

possible next I want to talk about the

play00:42

session so when you log in I can access

play00:44

the same page multiple times I I mean

play00:47

not just same page in the application if

play00:49

you have multiple controllers in this

play00:51

case we only have one but let's say if

play00:53

you have multiple controllers do we have

play00:55

to log in for each request uh not

play00:57

exactly because we have something called

play00:59

session behind the scene how that s is

play01:01

getting created and if I want to see the

play01:03

session ID can I can I do that so I want

play01:05

to check that as well and then what if I

play01:07

don't want to use a login form what if I

play01:09

want to do that from a postman can I do

play01:11

that so Postman basically any rest

play01:13

client uh can I do that so let's try

play01:15

that everything in this video so first

play01:17

thing how that login form is coming

play01:20

there when I'm requesting for the

play01:21

homepage see to understand this let's go

play01:23

back to the basics of spring web see

play01:26

when you create controller so let's say

play01:27

we have this box here this is your

play01:29

server and the most important thing here

play01:31

is the controllers right so these are

play01:33

the controllers which you're calling now

play01:35

let's say this is your uh home

play01:37

controller this is your add controller

play01:40

or this is your check balance controller

play01:42

so let's say if you have a bank account

play01:43

or you want to check your balance so we

play01:44

got multiple controllers here right and

play01:46

a client will send the request right so

play01:50

that's how the flow goes right so client

play01:52

send request response goes from the

play01:53

server to the client right everything is

play01:55

good of course the object here is the

play01:57

HTTP request object this is http

play01:59

response object uh which we get from the

play02:02

server and this is your container now if

play02:05

you talk about this controller here

play02:07

behind the scene these things are

play02:08

running on a surate container see as I

play02:11

mentioned before spring web GS into two

play02:14

parts one is a surate way which we are

play02:17

doing now and then there's also reactive

play02:19

way we are not focusing on reactive here

play02:21

spring reactive we are only focusing on

play02:22

the spring web now in this every

play02:25

controller gets converted into selet

play02:27

behind the scene so basically you are

play02:29

are able to run this on Tomcat because

play02:32

of that svets so all these things all

play02:35

your controller gets converted into

play02:37

svets okay so this is running on the

play02:39

seret container now this is your Tomcat

play02:42

which is a serlet container right but

play02:44

then before the request goes to the

play02:48

controller we got something here which

play02:50

is called your front controller so this

play02:52

is your front controller also called a

play02:55

dispatcher svet so every request from

play02:59

the client when is going goes to the

play03:00

controler which you created it goes to

play03:02

the disp salet but before it goes from

play03:05

the disp salet there are more things

play03:07

there by default we don't invoke them or

play03:10

even if they are there they're just

play03:11

passing it but we can customize it so

play03:14

when you add Spring Security we are

play03:17

calling those things those things are

play03:18

responsible but what are those things so

play03:20

those things are your filters so there's

play03:23

a filter chain here so I don't have

play03:25

horizontal space I will do that in

play03:26

vertical so basically what you have is

play03:28

you have something called a filter chain

play03:30

here so request goes from the client to

play03:35

the filter first this is your filter

play03:37

chain and then from here it goes to the

play03:39

front controller and then from front

play03:41

controller it interacts with different

play03:43

uh different controllers here now what

play03:46

is this filter chain in the filter chain

play03:48

you will be having multiple filters this

play03:50

is filter one let's say F F1 this is

play03:52

Filter 2 this is filter three and I'm

play03:55

not saying that you'll be having all

play03:56

this filter by default there might be

play03:59

few filters there might be more filters

play04:01

it depends upon how you configure your

play04:02

application by default there are certain

play04:04

filters but then when you talk about

play04:05

Spring Security it adds its own filter

play04:08

here okay so what it does is when the

play04:11

request goes from the client to the

play04:13

server the the Tomcat it looks for the

play04:15

filter first do we have any filters now

play04:18

Spring Security says yes there are

play04:19

filters multiple filters not just one

play04:21

let's check what are those filters are

play04:23

in the earlier version we used to see

play04:25

those filters here for some reason just

play04:27

not coming in the console uh not sure

play04:30

why so what I will do is I will ask my

play04:32

co-pilot to give me these security

play04:47

filters okay so you can see it is giving

play04:49

you a list of filters 11 filters but I

play04:51

think there are more filters which is

play04:53

not showing uh so if you scroll down or

play04:56

if you scroll up basically here uh we

play04:58

got security context assistance filter

play05:00

we got logout filter we got username

play05:02

password authentication filter now this

play05:04

is what was working when we got the

play05:06

login form so even if you're ACC

play05:08

accessing for the home controller it

play05:09

says hold on uh you are not logged in so

play05:12

let me take care of it so this filter

play05:14

comes on picture then we got login

play05:16

default page generator filter page

play05:17

authentication filter request cashier

play05:19

aware filter there are lot of filter

play05:21

here as you can see uh but I think there

play05:24

are more which is not showing so there

play05:25

are a lot of filters of course you don't

play05:27

have to remember all because Spring

play05:29

Security take care of it but when you

play05:31

want to customize it yes you can

play05:32

customize those filters then you need to

play05:34

know those filters and this filter so F1

play05:36

F2 which I'm showing here these are

play05:38

those filters uh by default that it it

play05:42

applies some filters to you and that's

play05:44

why it is giving you a login form now

play05:47

behind the scene how this Filter Works

play05:49

is it works in a chain format so when a

play05:51

request goes to the server it says okay

play05:53

let's execute F1 F1 can decide I I mean

play05:57

F1 I can actually change data as well

play05:59

let's say if you want to add two numbers

play06:01

uh 2 + 5 it goes to the filter it checks

play06:04

I mean using filter you can check uh are

play06:06

those two numbers actually integers or

play06:09

are there two numbers bigger than five

play06:11

so whatever filter whatever condition

play06:12

you want to add basically you can do

play06:14

that in the filter you can change the

play06:15

request you can change the response as

play06:17

well because response goes in the same

play06:19

format so if the request goes like this

play06:23

the response goes like this right so it

play06:25

goes to the filter so you can change the

play06:27

request you can change the response and

play06:29

whatever you can whatever you want to do

play06:31

uh but here we are not changing data we

play06:32

just checking if the user logged in or

play06:34

not so one of the filter here acts like

play06:36

a login filter it says Hey the user is

play06:38

not authenticated let's send the login

play06:40

form okay but let's say if the user is

play06:43

logged in already and by sending the

play06:45

session ID they can basically check if

play06:47

the user logged in yes don't ask for the

play06:49

login page let's send the request so

play06:52

that's how this Filter Works and they it

play06:54

uses something called chain as I

play06:55

mentioned so this filter will send

play06:57

request to F2 F2 will send it to F3

play06:59

three so there's something called Next

play07:01

filter or do filter chain so it goes for

play07:03

the next filter I hope now things are

play07:05

making sense how exactly uh when you

play07:07

call a controller the security part is

play07:09

getting activated is because of these

play07:11

filters we have talked about a lot of

play07:12

things now let's go for the second point

play07:14

which is the session ID so when you say

play07:17

this session is getting generated

play07:18

because if I relaunch this and of course

play07:20

it will give you a new password okay

play07:22

this is a new password I will just copy

play07:24

this because I want to re log in and

play07:26

just refresh this just wanted to make

play07:28

sure I'm not Lo logged in and now I'll

play07:31

be saying user and this is a password

play07:34

sign in now I'm logged in right and it

play07:36

doesn't matter how many time I refresh I

play07:37

can still see the same page it's not

play07:39

like it is giving me the login page but

play07:41

after log out it will give the login

play07:43

page what if you are changing your

play07:45

browser so when you change your browser

play07:47

you got a new instance right a new

play07:49

particular application even that will

play07:51

ask you for the login uh just to show

play07:53

you the proof I'm opening my Chrome

play07:55

Local Host 880 it is sending a request

play07:57

for the homepage now the inspect element

play07:59

of chrome is better than Safari I've

play08:01

never tried on Safari let's try on

play08:02

Chrome so I will do the same thing again

play08:05

same password enter I'm signed in how do

play08:07

I check this session ID you can check it

play08:10

from here right so you can just go back

play08:12

here and say inspect more tools and

play08:17

developer tools okay so here uh if I

play08:21

refresh once again let's go back to the

play08:23

con Network Tab and here if you can see

play08:25

we got continue so basically that's a a

play08:28

query parameter they're sending but

play08:29

required this is request for the

play08:30

homepage okay this is request for the

play08:32

homepage I will click here and if you

play08:35

see there are certain things here one of

play08:37

the thing is the session ID if I click

play08:39

on this uh you can see s session ID so

play08:42

this is a part of a cookie and this is

play08:46

your session ID so that number the alpha

play08:50

numeric number which you can see here

play08:52

it's it's actually heac code uh that's

play08:54

your session ID and every time you log

play08:56

in it will change let me show you so I

play08:58

just refresh this and now send the

play09:00

request for the log out yes I'm sure and

play09:06

if I go to log out new session ID or is

play09:09

it the same thing even I forgot what was

play09:11

session ID before doesn't matter let's

play09:14

create a new user and I mean new login

play09:17

sign in and request for the Local Host

play09:20

because you can see we don't have

play09:21

question mark continue there so it says

play09:23

Local Host and we got a new session ID

play09:27

there if you can see the number has

play09:28

changed if you remember the old number

play09:30

but what if I want to print this here in

play09:33

the response just to see if the session

play09:35

is changing or not you can do that from

play09:37

your code so just go back here now if I

play09:39

want to print this session ID what I can

play09:41

do is I can just go back here and get

play09:44

the hold on the

play09:46

HTTP serlet request so as I mentioned

play09:50

before behind the scene everything is

play09:51

serlet right even the controllers are

play09:53

serate so it will have two objects the

play09:55

request object response object they're

play09:57

called HTTP server request object and

play09:59

HTTP seret response object I just want a

play10:02

request now don't want to play with the

play10:03

response one so this is the HTTP subet

play10:07

request object which I got hold on now

play10:09

this request object has multiple methods

play10:12

and just wanted to confirm so this HTTP

play10:15

seret request should be a part of jakara

play10:17

ser. HTP package okay with this object I

play10:20

can simply say request. get

play10:24

session dot get ID so this is this will

play10:28

basically return the ID let's relaunch

play10:30

the application because we have changed

play10:32

the code and we got a new password so

play10:34

let's copy this as well go back to your

play10:36

browser I will stick to whichever Safari

play10:39

is there in fact let's hit back to

play10:41

Chrome itself okay so first of all we'll

play10:43

do the I mean it will log out by default

play10:45

because we have restarted the

play10:47

application no no

play10:50

no session is still there okay so now

play10:54

let me just log in once again

play10:56

and sign in so we are logged in and you

play10:59

can see we are printing the session ID

play11:02

as well so if I go to Local Host you can

play11:04

see this is the same value which you can

play11:06

see there right I hope you can see this

play11:08

font size but yeah this is this is the

play11:10

same thing and every time you refresh

play11:13

you will get the same session ID and not

play11:14

just for this particular URL doesn't

play11:16

matter which URL you go to you will get

play11:18

the same session ID but yes if you

play11:20

delete your cookies this will be gone so

play11:23

you will be logged out automatically

play11:24

it's as simple as that so that's the

play11:26

session ID which we were trying to print

play11:27

and of course we can have multiple

play11:29

controllers you do that with let's say I

play11:32

want to print the about content I want

play11:33

to add two numbers whatever you want to

play11:35

do just check if you're getting the same

play11:37

session ID okay what next the next thing

play11:40

I want to do is uh I want to change the

play11:43

username password I'm not happy with the

play11:45

password which is is generating here how

play11:48

do I change it see one of the filter

play11:50

which is the username authentication

play11:51

filter if you remember one of the filter

play11:53

we have here which is this uh this

play11:57

checks if you have your username

play11:59

password mentioned in the property files

play12:01

if not it will simply create its own

play12:04

password what we can do is we can add

play12:07

the username password so for doing that

play12:09

you can say spring. security. user.name

play12:13

and you can mention the name here so I'm

play12:15

going for name naen and spring.

play12:19

security. user. password and I'm going

play12:22

to set this as teliscope so the username

play12:24

is naen the password is Tesco and this

play12:27

is a property okay I know in the

play12:28

community version it will not highlight

play12:30

much if you're using ultimate version

play12:32

this looks good but yeah let's use

play12:34

community so now with this let's restart

play12:36

the application and go back to the

play12:37

browser so first let's hit the log out

play12:41

and now we are logged out so let's try

play12:43

with the user and in fact what about the

play12:46

password is it is it generating the

play12:47

password if you scroll nowhere it is

play12:50

generating a password because it knows

play12:51

now that you have your own password so

play12:54

let's try with this password first which

play12:56

is teliscope and I sign in no bad

play13:00

currenti Sals so now I will try with

play13:02

naen and Tesco sign in we are in okay it

play13:07

says save the password no because I'm

play13:09

going to change it okay so now if you

play13:11

refresh you're still logged in and you

play13:13

can access it multiple times is that

play13:16

good so now you have your own username

play13:18

password I know I know what you're

play13:20

thinking uh what about different users

play13:22

different username passwords we'll do

play13:24

that in the upcoming videos but yeah at

play13:26

least we can change the username

play13:27

password I want to do one final thing

play13:30

which is uh logging in through the

play13:32

postman or maybe any UI tool or any rest

play13:36

line tool so I do have Postman in this

play13:37

machine so I will just uh fire it so

play13:40

that's Postman used it for some other

play13:42

URL this time I want to hit Local Host

play13:46

colon 80 this is the

play13:49

homepage

play13:51

and send okay so you can see we got an

play13:54

status code which is uh 4 41

play13:57

unauthorized that means you are not

play14:00

allowed here you know why you're not

play14:01

allowed because you're not sending the

play14:03

username password how do we send that so

play14:05

if you can see we have a tab here which

play14:07

is authorization and by default there is

play14:10

no Au we have to say hey I have a

play14:13

username password and to do that you

play14:15

will click on basic o there are multiple

play14:16

options here we got JWT barrier token

play14:19

multiple options I will stick to basic o

play14:21

now and let's explore others later basic

play14:23

o so let's enter the username which is

play14:25

naven and the password is teliscope in

play14:29

fact let's give some wrong password TCO

play14:31

one send still unauthorized th Isco and

play14:36

we got the response it says 200 we are

play14:38

happy and it generates a new session ID

play14:40

because a new login so you can see this

play14:41

session ID is not matching with this so

play14:44

different users different S ID okay uh

play14:47

looks good so that's how basically you

play14:49

can change the username password you can

play14:52

access it through the postman now we

play14:54

understood also how Filter Works we have

play14:56

seen that in the diagram here yeah

play14:58

that's what I talk about in this

play15:00

particular video and we'll talk about

play15:02

some certain more things but if you want

play15:04

to not just sending a get request post

play15:07

request will it work let's try that in

play15:09

the upcoming videos bye-bye

Rate This

5.0 / 5 (0 votes)

Étiquettes Connexes
Spring SecurityFilter ChainSession IDUsernamePasswordAuthenticationPostmanREST ClientWeb ApplicationSecurity ConceptsLogin Process
Besoin d'un résumé en anglais ?