#32 Spring Security | Custom Configuration

Telusko
27 Jul 202417:29

Summary

TLDRThis video tutorial delves into customizing Spring Security configurations beyond the default settings. It guides viewers through creating a config class to define beans and override the default security filter chain. The presenter demonstrates disabling CSRF protection, enforcing authentication for all requests, and toggling between form login and HTTP basic authentication. The tutorial also touches on making the application stateless for session management, providing a foundation for more advanced security implementations like JWT in future videos.

Takeaways

  • 😀 The video discusses configuring a Spring Security application with custom settings beyond the default configurations.
  • 🔒 It explains how to disable the default Spring Security configuration and set up a custom security filter chain.
  • 🛠️ The presenter demonstrates creating a config class to customize Spring Security settings, emphasizing the use of the @Configuration annotation.
  • 📝 The video covers changing application properties, such as usernames and passwords, and the importance of modifying the security filter chain.
  • 🔄 The process of disabling CSRF (Cross-Site Request Forgery) protection in Spring Security is detailed, showing both lambda and imperative syntax.
  • 🚫 It illustrates how to enforce authentication for every HTTP request, ensuring no page can be accessed without proper credentials.
  • 🔑 The video guides through enabling form login with customizer and handling HTTP basic authentication for tools like Postman.
  • 🔄 The presenter discusses the implications of disabling CSRF and making the HTTP session stateless using session management configurations.
  • 📱 The script touches on the challenges of form login in a stateless session and how it requires credentials with every request.
  • 🛑 The video shows how to disable the default logout functionality when implementing custom security configurations.
  • 🔍 Finally, the script provides insights into the builder pattern for configuring HTTP security settings in a readable and sequential manner.

Q & A

  • What is the default configuration provided by Spring Security when it is first implemented?

    -When Spring Security is first implemented, it provides a default configuration that includes a basic security setup. This includes a default user with a preset username and password, and a simple login form for authentication.

  • How can one customize the security settings in a Spring Security application?

    -To customize the security settings in a Spring Security application, one needs to create a configuration class annotated with `@Configuration` and use the `@EnableWebSecurity` annotation to override the default security configuration. This allows the developer to define their own beans and security configurations.

  • What is the purpose of the `SecurityFilterChain` in Spring Security?

    -The `SecurityFilterChain` in Spring Security is responsible for defining the sequence of security filters that are applied to the application's HTTP requests. Customizing this filter chain allows developers to control the security mechanisms applied to different parts of the application.

  • How can one disable CSRF (Cross-Site Request Forgery) protection in Spring Security?

    -To disable CSRF protection in Spring Security, one can use the `HttpSecurity` object's `csrf()` method with a customizer that disables CSRF, either by using lambda syntax for a cleaner code or by using the imperative style with a custom `CsrfConfigurer`.

  • What does the `authorizeRequests()` method do in Spring Security configuration?

    -The `authorizeRequests()` method in Spring Security configuration is used to specify the authorization rules for HTTP requests. It allows developers to define which requests should be authenticated, which can be done using lambda expressions or an `AuthorizationManager` object.

  • How can one implement form-based authentication in Spring Security?

    -Form-based authentication can be implemented in Spring Security by using the `formLogin()` method on the `HttpSecurity` object. This method can be customized with default settings using `withDefaults()` or by specifying custom configurations.

  • What is HTTP Basic authentication and how is it enabled in Spring Security?

    -HTTP Basic authentication is a simple authentication scheme built into the HTTP protocol, where credentials are sent in the HTTP headers. In Spring Security, it can be enabled by calling the `httpBasic()` method on the `HttpSecurity` object, which can also be customized with default settings.

  • Why might one want to disable the default logout functionality in a customized Spring Security setup?

    -One might want to disable the default logout functionality in a customized Spring Security setup because when implementing custom authentication mechanisms, the default logout may not work as expected. Developers may need to implement their own logout logic that integrates with their custom authentication system.

  • What is the session creation policy and how does it relate to making a session stateless in Spring Security?

    -The session creation policy in Spring Security determines when and how sessions are created. To make a session stateless, one can set the session creation policy to 'stateless' using the `sessionManagement()` method on the `HttpSecurity` object. This means that the server does not need to maintain session information between requests.

  • How can one make HTTP requests stateless to avoid CSRF issues in Spring Security?

    -To make HTTP requests stateless and avoid CSRF issues, one can configure the session management in Spring Security to be stateless by setting the session creation policy to 'stateless'. This requires passing credentials with every request, effectively making each request independent of any session state.

  • What is the builder pattern mentioned in the script and how does it simplify Spring Security configuration?

    -The builder pattern mentioned in the script is a method of constructing an object step by step, often used in configurations. In Spring Security, it simplifies the configuration by allowing developers to chain method calls on a single object, making the code more readable and easier to manage.

Outlines

00:00

🛠️ Customizing Spring Security Configuration

The video script begins with an explanation of how to configure Spring Security by default and then customize it. The speaker details the process of setting up Spring Security with default configurations through dependency inclusion and then modifying the application properties for username and password. The focus then shifts to connecting with a database and disabling form login. The script emphasizes the creation of a separate config class to customize the security filter chain, explaining the importance of marking the class with the @Configuration annotation and using@EnableWebSecurity to override default configurations. The speaker also demonstrates how to create a security filter chain bean and the necessity of using HTTP security to build the custom filter chain, including handling errors related to missing exception signatures.

05:00

🔒 Disabling CSRF and Restricting Access for Authentication

This paragraph delves into the process of disabling Cross-Site Request Forgery (CSRF) protection in Spring Security, highlighting the changes in Spring six that require different syntax for disabling CSRF using either Lambda or imperative style. The speaker illustrates how to enforce authentication for every HTTP request, ensuring no one can access any page without proper credentials. The summary includes the steps to enable form login using customizer with default settings and the implications of disabling CSRF on browser-based login forms. It also touches on enabling HTTP basic authentication for API access through Postman and the importance of understanding the underlying configurations for securing applications.

10:02

🔄 Implementing Stateless Sessions and HTTP Basic Authentication

The speaker discusses the concept of stateless sessions in Spring Security and how to implement them by modifying the session management configuration. By setting the session creation policy to stateless, the application no longer relies on session IDs for maintaining user state, which simplifies the authentication process, especially with HTTP basic authentication. The script covers the challenges of using stateless sessions with form login in browsers and how to work around them by disabling form login and using HTTP basic authentication instead. It also explains the use of builder patterns for configuring HTTP security settings in a readable and organized manner.

15:03

🛑 Troubleshooting Configuration Issues and Anticipating Future Enhancements

In the final paragraph, the speaker encounters issues with the application's configuration settings and decides to comment out the current setup to understand what happens behind the scenes. The explanation includes the technical details of the HTTP security object and how CSRF, authorize requests, and HTTP basic configurations work. The speaker simplifies the understanding by breaking down the interface implementations and method definitions required for each security feature. The paragraph concludes with a look ahead to future videos that will cover more advanced topics such as integrating with databases, using JWT, and other security enhancements, inviting viewers to share their excitement for the upcoming content.

Mindmap

Keywords

💡Spring Security

Spring Security is a powerful and highly customizable authentication and access-control framework used in Java applications. In the context of the video, it is used to secure a project by providing default configurations and allowing for further customization. The script discusses how to implement Spring Security and then customize it beyond the default settings, such as changing the security filter chain and disabling CSRF protection.

💡Configuration Class

A configuration class in Spring Framework is a class where beans are defined for dependency injection. The video script describes creating a specific configuration class within a 'config' package to customize Spring Security settings. This class is marked with the @Configuration annotation to indicate that it contains bean definitions for Spring to manage.

💡Security Filter Chain

The security filter chain in Spring Security is a series of filters applied to an application's HTTP requests to enforce security rules. The script explains how to customize this chain by creating a method that returns a SecurityFilterChain object, allowing the developer to define their own security rules rather than using the default ones provided by Spring Security.

💡@EnableWebSecurity

@EnableWebSecurity is an annotation used in Spring Security configuration to indicate that the annotated class will provide the security configuration for web applications. In the video, it is used to enable custom security configurations instead of the default ones provided by Spring Security.

💡HTTP Security

HTTP Security is a builder in Spring Security that allows for the customization of security configurations for HTTP requests. The script discusses using the HTTP security builder to configure various aspects of security, such as disabling CSRF protection and requiring authentication for all requests.

💡CSRF (Cross-Site Request Forgery)

CSRF is a type of security vulnerability that allows an attacker to submit a malicious request from a different website on behalf of an authenticated user. The video script explains how to disable CSRF protection in Spring Security by using the .csrf().disable() method on the HTTP security builder, which is a step taken to customize the security configuration for specific application needs.

💡Form Login

Form login is a method of authentication where users submit their credentials through an HTML form. The script describes how to enable form login in Spring Security using the .formLogin() method on the HTTP security builder, allowing for a traditional login page to be used for authentication.

💡HTTP Basic

HTTP Basic is a simple authentication scheme built into the HTTP protocol where the client sends credentials with each request. The video script mentions enabling HTTP Basic authentication in Spring Security for use with tools like Postman, allowing API access without a login form by including the credentials in the request headers.

💡Builder Pattern

The builder pattern is a design pattern used to construct complex objects step by step. In the context of the video, the builder pattern is used with the HTTP security builder to chain multiple configuration methods together, making the code more readable and organized by applying different settings to the same object.

💡Stateless

Stateless in the context of web applications and security means that the server does not store any session information between requests. The script discusses making the application stateless by setting the session creation policy to stateless, which is useful for certain security scenarios and allows the application to work without maintaining server-side session data.

💡Session Management

Session management is the process of handling sessions in a web application, which includes creating, using, and invalidating sessions. The video script explains configuring session management in Spring Security by using the .sessionManagement() method on the HTTP security builder to specify how sessions should be handled, such as setting the session creation policy to stateless.

Highlights

Introduction of configuring the application with Spring Security and its default settings.

Explanation of how to change application properties for username and password customization.

Desire to connect with a database and disable form login for enhanced security settings.

Step-by-step guide on creating a custom security configuration class in Spring.

The importance of marking a class as a configuration class with the @Configuration annotation.

Enabling web security by overriding the default configuration with @EnableWebSecurity.

Creating a method to return a custom Security Filter Chain object.

Using HTTP security to build the custom security filter chain.

Disabling CSRF protection with the new approach in Spring 6.

Authorizing HTTP requests to require authentication for every request.

Demonstration of the application working without security leading to unauthorized access.

Enabling form login with customizer and default properties.

Testing form login functionality and its successful implementation.

Explanation of HTTP basic authentication and its necessity for Postman access.

Making the application work with HTTP basic authentication in Postman.

Discussion on disabling form login for direct API access and its implications.

Making the application stateless to handle CSRF by using session creation policy.

Understanding the builder pattern to apply multiple settings to the HTTP security object.

Final demonstration of the application with customized security settings working as expected.

Conclusion and anticipation for upcoming videos on advanced security topics like JWT.

Transcripts

play00:00

so till this point we were able to

play00:01

configure the application right and we

play00:04

are basically able to implement Spring

play00:06

Security but then most of the settings

play00:09

are default right so we implemented

play00:11

Spring Security by saying hey I got the

play00:13

dependency this project is secured by

play00:15

Spring Security and Spring Security says

play00:18

okay since you are using me let me give

play00:20

you some default configuration and then

play00:22

we have changed something okay so

play00:24

basically we went to application

play00:25

properties we we set the username

play00:27

password we have done some changes but

play00:30

then we want to do more we want to

play00:32

connect with database we want to uh

play00:34

disable let's say I don't want to go go

play00:36

for a form login so there are a lot of

play00:38

settings which you have to do and then

play00:39

we'll do that step by step in this video

play00:42

let's see how do we configure that how

play00:44

do we create our own settings how do we

play00:47

change the way the security filter chain

play00:50

works so to do that of course we want to

play00:53

change the way it works right by default

play00:55

Spring Security provides you a filter

play00:57

chain there are lot of filters which

play00:59

comes into and then it will check for

play01:01

the defaults but now I want to customize

play01:03

it I want to have my own filter chain

play01:06

and the way you can do that is by

play01:08

creating a config class see when you

play01:10

talk about spring framework and if you

play01:12

want to customize something or when you

play01:14

want to have your own configuration you

play01:16

create a separate Class A config class

play01:19

and then you define beans there which

play01:21

will inject the object so now what I

play01:23

will do is to achieve that I will create

play01:26

a class but then I will create that

play01:28

class in a package I will name this your

play01:30

package as config and in this package I

play01:33

want to have that class which is the

play01:36

security config so this is the class we

play01:38

have and in this class we have to do

play01:41

that first of all I want to say that

play01:43

this is a configuration file to Spring

play01:46

and to do that you will say

play01:47

configuration now your spring knows that

play01:50

this is a configuration class and I have

play01:51

to search for the configuration here

play01:53

next I don't want to go for the default

play01:55

Spring Security configuration I want to

play01:57

implement it here so to do that you will

play01:59

say enable web security Now by doing

play02:03

this you are saying hey don't go for the

play02:04

default flow go with the flow which I

play02:06

mentioned here so by doing this we are

play02:08

doing two things first we are saying

play02:09

this is the configuration and we are

play02:11

saying that go with this configuration

play02:14

now what I want to change see by default

play02:16

it will work for the security filter

play02:17

chain I want to customize it so in order

play02:19

to do that what you have to do is you

play02:20

have to return a bean for security

play02:23

filter chain so let's do that so I will

play02:26

create a method which will return you

play02:28

the object of

play02:30

security filter chain which is coming

play02:33

from uh springf framework. security.

play02:36

web. security filter chain and then we

play02:38

can give any method name here so I will

play02:40

say security filter chain and this will

play02:42

basically give you the object of

play02:43

security chain but how so of course you

play02:46

have to return the object of security

play02:47

filter chain now who will give you this

play02:50

so there is a type called HTTP security

play02:54

have to use this let's create reference

play02:56

for it called HTTP you can also say HTP

play02:58

security but HTTP is a small word so it

play03:00

makes sense and I will be using that

play03:03

because sttp has a method called build

play03:06

so basically the build here Returns the

play03:08

object of security filter chain now it

play03:11

is giving you some error here is because

play03:13

we have to add the exception throw

play03:16

exception here a signature now this will

play03:18

do so what we are doing is we are saying

play03:20

hey Spring Security don't go for the

play03:22

default this is a security chain you

play03:24

have to go for so this is a filter chain

play03:26

follow this and you're good to go now

play03:28

since we have not specified find any

play03:30

filter here by default no filter is

play03:33

applied I will show you so what I will

play03:34

do is I will just uh restart this in

play03:37

fact you know let's comment the bean tag

play03:39

and let's restart then or maybe we have

play03:41

to also comment enable security so I've

play03:44

commented both enable web security and

play03:46

the bean here that means this is still

play03:48

not applicable what we are doing and I

play03:50

want to check without those things if I

play03:53

hit the URL you can see it is still

play03:55

enabling the login form so that means

play03:57

spring secur is still implemented but if

play03:59

I do this and if I UNC commmand this B

play04:02

now and if I restart so let's see if

play04:05

disables the default configuration so

play04:07

let's go back here and instead of

play04:09

hitting the login I will hit the URL and

play04:11

it's working without login it is working

play04:13

so if I refresh it is still there that

play04:16

means security is not implemented now we

play04:18

are bypassing all the security it's

play04:21

something like you're buying a lock and

play04:24

you have not closing it properly or

play04:26

maybe you're not even locking it so we

play04:29

don't want to do that right so let's

play04:30

Implement so how do we secure it how do

play04:32

we provide that uh layer the first thing

play04:34

I want to do is I will Implement

play04:36

different security I want to achieve

play04:38

that login form I want to maybe uh send

play04:40

the request through the post Postman uh

play04:43

but the first thing I want to do is I

play04:44

want to disable the csrf so let's do

play04:46

that so I will go back to http because

play04:48

that's the object which using which you

play04:50

are creating this build right so we have

play04:52

to make some changes in this object of

play04:54

HTTP which is the object of HTTP

play04:56

security and in this the first thing I

play04:58

will do is I will say see

play05:00

srf I want to disable this right in the

play05:02

initial days it was very simple you can

play05:04

simply say uh csrf do disable but it

play05:07

will not work now in Spring six uh

play05:09

things are bit different now I will show

play05:11

you this in two steps one using the

play05:14

Lambda syntax and one without it with

play05:16

lambra it becomes very easy to read and

play05:18

write but then if you want to understand

play05:19

what is happening behind the scene we'll

play05:20

go for imperative as well so here I'm

play05:23

I'm saying hp. csrf and to disable it I

play05:26

will use customizer again I'm not

play05:28

explaining that now I will do that bit

play05:30

after some time so I will say customizer

play05:33

do disable so what we doing is we are

play05:35

disabling the csrf the next thing I want

play05:37

is even if you write this you're still

play05:39

you will still not get the login form I

play05:41

want it to be authorized so if I don't

play05:44

authorize it anyone can go there and

play05:46

they can log in so even if I open a new

play05:48

incognitive mode here so if I go to

play05:51

incognitive mode and if I say local SC

play05:53

880 you can still open it so there's no

play05:55

login restriction now and to achieve

play05:56

that what I will do is I will say http

play05:59

do

play06:01

authorize HTTP request for every request

play06:04

and people who are good with lamb

play06:06

expression they know what I'm writing

play06:07

but in case if you're not familiar I

play06:09

will show you the imperative style so

play06:11

request dot any request should be

play06:13

authenticated so by doing this what we

play06:15

are doing is no one should be able to

play06:17

access any page without authentication

play06:19

so now after making those changes if I

play06:21

restart my uh ID or the application go

play06:25

back to the browser Ander refresh and

play06:27

now you can see it says access to local

play06:29

host was denied that means

play06:31

authentication is applied here so now

play06:34

that means you have to enter username

play06:35

password but then where you will do that

play06:37

even if I do that in Postman and let's

play06:39

try to fetch the homepage here and if I

play06:42

say send you can see it says forbidden

play06:44

even if I pass the values you can see

play06:46

I'm passing the username password it

play06:48

still says forbidden because you are

play06:51

sending username password but no way we

play06:54

are using it here so how do we do that

play06:56

first of all I want to enable the form

play06:58

login so yesterday P dot you can use

play07:01

something called a form login using

play07:04

customizer dot with default so it will

play07:07

pick up the default properties and it

play07:08

will Implement form login now just by

play07:11

saying form login let's see what happens

play07:13

so let's go back here and refresh and

play07:15

you can see we got a form login now is

play07:17

it working let's try so I will say naven

play07:19

teliscope and it's working you can see

play07:21

if I refresh it works so that means the

play07:24

form login is been implemented so

play07:26

whatever customization want you want to

play07:27

do you can do that in The Code by using

play07:29

the this HTP object but what about if I

play07:31

try to do that from Postman let's try

play07:33

from Postman and if I say send okay we

play07:35

got this status okay and we are happy

play07:37

about it but look at the response

play07:39

response is basically a login form why

play07:42

we got login form here it's because we

play07:45

are saying form login that's why we got

play07:47

it so if you want to do that from the

play07:50

postman in that case you have to add one

play07:52

more uh thing here which is HTTP do HTTP

play07:56

basic you have to implement this or you

play07:57

have to enable this for the postman for

play07:59

the rest access rest API access so let's

play08:03

go back here and see if that Postman is

play08:04

working now says send and you got it so

play08:08

you can see we got the page cool so we

play08:11

have used two things one is form login

play08:13

for the browser and one is HTTP basic so

play08:16

form login still works now log out will

play08:17

not work since we're implementing our

play08:19

own it is expecting you to have your own

play08:21

login page so log out will not work or

play08:23

it will it will sign out directly it

play08:24

will not give you login form so now if I

play08:27

do naven and the list

play08:30

okay wrong

play08:32

password so you can see it is working so

play08:35

basically uh by doing this we are

play08:37

enabling that that feature of course you

play08:39

can disable your form login if you want

play08:41

uh you can directly use your Postman to

play08:43

access it uh next thing I want to do is

play08:45

I want to make sure that you don't see

play08:48

we are disabling this CSF right now why

play08:50

we disabling it is because in one of the

play08:52

video we have talked about different

play08:55

ways of handling csrf one of them is

play08:57

what if you make your HTTP stateless and

play09:00

if you do that you don't have to worry

play09:02

about the session ID so how do you make

play09:04

it stateless is you have to say HTTP do

play09:06

session management and you will take the

play09:08

session object and you will say session

play09:11

dot how do you specify that you want to

play09:14

go for stateless or stateful in that

play09:16

case you have to use something called a

play09:18

session creation policy so there's

play09:20

something called session creation policy

play09:22

in this you have to say session creation

play09:23

policy so you can see we have different

play09:25

options we got never if required

play09:27

stateless I want to go for stateless

play09:30

and job done so by doing it stateless

play09:33

what we are doing is see the problem

play09:35

with this is you can't login from your

play09:37

browser with the login form is because

play09:39

for every request you have to pass the

play09:41

credentials and when you have a form

play09:43

login let me show you what on what I'm

play09:45

talking about uh so if I refresh and if

play09:48

I say naven and Tesco so it will give

play09:52

you login form again because now you are

play09:53

accessing a new resource a new resource

play09:55

is a new session right new request so

play09:57

you have to pass this detail every time

play10:00

but with Postman this will work so if I

play10:01

go to postman if I say send you can see

play10:04

this is working and every time you send

play10:06

a request you will get a new session ID

play10:08

if you can see the number is changing

play10:09

here so that's how you get the new

play10:10

session ID right but if you want this to

play10:13

work on a browser what you can do is you

play10:15

can disable your form login because then

play10:17

you have to maintain your own sessions

play10:18

and now if I restart let's see what

play10:20

happened with the browser enter okay so

play10:22

you can see we got a popup not the login

play10:24

for a popup and in this popup you can

play10:26

say nin Tesco enter and now it will work

play10:30

so not with the form login but HTTP

play10:32

basic will give you this popup refresh

play10:35

and I mean if you say enter you will get

play10:37

new S ID every time cool right so that's

play10:40

how basically you can uh do this but

play10:42

again the problem is how all these

play10:44

things are working so what I will do is

play10:46

time being I will just comment this

play10:49

everything and let's see what is

play10:51

happening behind the scene so comment

play10:54

why is not a commenting here

play10:56

comment am I using a wrong uh I don't

play10:59

know shortcut is that maybe they have

play11:01

done some changes so now let's

play11:03

understand what is happening behind the

play11:05

scene see we understand what is this

play11:07

object HTTP SEC HTTP object right which

play11:09

is HTP security and we also know that

play11:11

method object will have some methods

play11:13

right so we got csrf and in this we are

play11:15

passing this customizer and disabled so

play11:17

what is this thing to understand this

play11:19

thing let's do that in a imper

play11:20

imperative way so I will say HTP Dot and

play11:23

I will use this method which is csrf and

play11:26

if You observe csrf you can see what it

play11:28

takes it takes the object of customizer

play11:30

in fact that's a big name can't even

play11:32

copy this so yeah we have a big name

play11:34

customizer uh in in the I mean the type

play11:37

it takes is csrf configure the type and

play11:40

again the type is HTTP security I hope I

play11:42

will remember this U so I will say

play11:45

customizer so that's a part of security

play11:48

config package and this will take

play11:51

csrf

play11:53

configur and uh it was checking

play11:55

something I forgot so let me just say

play11:57

control space again I don't want to do

play12:00

dictate uh HTTP security object so

play12:02

inside this it should be HTTP security

play12:05

so this is a type you have to work with

play12:07

Okay and then let's create the object of

play12:08

it because csrf method takes the object

play12:12

of customizer of type csrf configure of

play12:14

type HTP security and let's give a name

play12:17

to it I will say cust or CSF cust cust

play12:21

CSF equal to and then you have to say

play12:23

new and the object for this now the

play12:27

customizer itself is interace you can

play12:29

see we have the interface and the method

play12:31

name is customize that means if you are

play12:33

using this if you want to create the

play12:35

object of it we have to create a method

play12:38

you have to define the method of it

play12:39

using Anonymous in a class okay and in

play12:41

this particular method you you using

play12:43

this object I will name I will not use

play12:45

the name as HTTP configurer uh csrf

play12:48

configurer I can use any other name I

play12:50

will say customizer I mean you can use

play12:53

usern name name right so the name I'm

play12:55

using here is customizer and using this

play12:58

object I can do whatever I want now so

play13:00

using customizer you can you can use

play13:02

different methods what we are using it

play13:04

for is disable right now this is the way

play13:08

you create the object of customizer csrf

play13:10

configure HTTP security because this

play13:12

object you to pass inside HTTP do csrf

play13:16

I'll pass this object and your job is

play13:18

done so by doing all these things you

play13:21

are disabling your csrf oh let the task

play13:23

right now since this is an interface and

play13:25

this is a functional interface if I show

play13:28

you configure this is a function

play13:29

interface which means you can use lambra

play13:32

here and people who are familiar with

play13:33

lambra they know how to create lambra

play13:35

it's very simple you create uh you

play13:37

remove the extra stuff and I mean if you

play13:39

don't know lambra just search for lambra

play13:40

expression on YouTube or telis lambra

play13:44

Java lambra that should make sense so

play13:46

you can replace the entire code this

play13:48

this entire code with one line so this

play13:50

line here which I'm which I've written

play13:52

is equal to this number of

play13:55

lines oh we can do the same thing for

play13:58

authorized request let's let's try that

play14:00

uh HTTP do

play14:02

authorize HTTP request and look what it

play14:05

is asking you for look at the name

play14:07

customizer authorization manager request

play14:10

matcher registry and then you have to

play14:12

create object for this and then you have

play14:14

to pass it in the method like authorized

play14:16

HTTP request same goes for HTTP basic

play14:19

let's try for that HTTP basic it is

play14:22

asking you for the object of again

play14:24

customizer but since we want to go for

play14:26

default configuration we are not

play14:28

changing anything we are saying

play14:29

customizer do with defaults so it will

play14:31

pick up the default settings and that's

play14:32

how it is working behind the scene and

play14:35

now you know what is happening behind so

play14:37

for all the methods here same thing you

play14:39

have to implement certain uh interface

play14:41

and Define the method and pass the

play14:43

object here uh another thing which you

play14:45

can do here is uh instead of doing all

play14:48

these things one by one you can use a

play14:50

builder pattern what I'm saying is

play14:53

remove this semicolon and remove this

play14:55

HTTP so you can just add at the end

play14:58

something like this so HTTP dot

play15:02

csrf that particular setting then

play15:05

authorize request and then I will just

play15:09

put that below somewhere okay what's

play15:11

wrong with my uh ID settings all the

play15:15

settings have been

play15:16

changed this is weird Okay I'll just say

play15:20

enter here remove the semicolon and now

play15:23

you can put this after this so you can

play15:26

see for one object you are applying

play15:29

different settings I can remove this and

play15:31

the same thing goes for this part cut

play15:35

and paste it here just that you don't

play15:37

have to put semicolon at the end and put

play15:39

it together if it is Big you can just

play15:42

Center here to make it more readable and

play15:44

remove this so by doing this you are

play15:46

making it more readable and in proper

play15:50

sequence so this is this is called build

play15:52

a pattern so for this object you are

play15:54

doing this then you are doing this you

play15:56

know we when you have a belt in the

play15:57

factory where the object passes from one

play16:00

machine to other machine same thing is

play16:02

happening here one object is passing to

play16:04

different methods and customizing it in

play16:07

fact what you can also do is you can

play16:08

directly say return here then you don't

play16:10

have to write return again it's just

play16:12

that at the end have to say

play16:15

dot build even this looks cool so that's

play16:19

it uh let me just run this after making

play16:21

all those changes I hope this will work

play16:23

if you see a Waring here just that it is

play16:25

saying you to use method reference in of

play16:26

lambra uh you can replace this uh method

play16:29

reference of Lambda to Method reference

play16:31

even that works okay this start done

play16:33

let's verify from the postman H this

play16:37

you're getting the response maybe also

play16:39

try to work with students if you're

play16:41

getting all the students and you got it

play16:43

okay let's also see if the post request

play16:46

is working so I will say post you can

play16:47

see we have a post request here uh I'm

play16:50

sending the headers okay we are sending

play16:53

CSF token let's let's let's not send it

play16:56

and done you can see it is working so

play16:58

even without the token it is working

play17:00

because we are disabling the csrf

play17:03

request or csrf setting so yeah that's

play17:05

it from this where we talked about

play17:07

different settings or different things

play17:08

you can do the reason we are doing this

play17:10

is because in the upcoming videos we

play17:11

have to do a lot of changes right

play17:13

working with the uh user username

play17:15

password coming from database working

play17:17

with uh JWT and different stuff so I

play17:21

hope you're excited for the entire

play17:22

series I'm enjoying it I hope you are if

play17:25

you are let me know in the comments and

play17:27

see you in the next video bye-bye right

Rate This

5.0 / 5 (0 votes)

الوسوم ذات الصلة
Spring SecurityWeb ConfigCustom FiltersSecurity ChainCSRF DisableForm LoginHTTP BasicStateless AuthSession MgmtBuilder Pattern
هل تحتاج إلى تلخيص باللغة الإنجليزية؟