this video series is very much helpful source
We can think about Spring Security like the Guard of our Web App. Spring Security is an application framework that helps us do application level security. It gives the following features:
- Login/Logout
- Allow/block URLs to certain users(certain roles)
- Allow/block access to URLs to logged in users
Spring Security handles the most common vulnerabilites. If we include spring security to our webapp we can be saved from:
- Session fixation
- Click jacking
- Click Site Request Forgery (CSRF)
All these vulnerabilites are handled by Spring Security, so we don't need to give any focus to them and concentrate on the development.
- Username/password authentication
- SSO/Okta/LDAP --> single sign on stuff
- App level Authorization
- Intra App Authorization like OAuth
- Microservice security (using tokens, JWT)
- Method Level Security
- Authentication
- Authorization
- Principal
- Granted Authority
- Roles
We can think the Spring security like the guard of our web app. So, All the works that a guard do to protect his house, are same to spring security's feature.
To enter the house we need to tell the guard about the question "Who are you?"
Normally a ID will tell the guard about my information. Similaryly a userID and password is the key for the authentication of the system. Authentication can be of many types:
-
Knowledge based Authentication: by authorize myself with some userID or password that comes from memory is called the knowledged based authentication. It has some merits and demerits as well. The main advantage of it is it's simple. But it is not fully safe cause anybody can know this and by using this he can enter into the system.
-
Possession Based Authentication: To authenticate in this way, we have to give something to the system so that it can match myself. They can be:
- OTP
- Text message/phone call
- Keycard & Badges
- Access token device
-
Multi Factor Authentication: This is the combination of both knowledge based and Possession based authentication.
Authorization basically means the allowance from the security guard. Like, I cannot do everything in front of the security guard. So the main summary:
- It answers the question "Can the user do this?"
- Before authorize we have to authenticat the users first.
Currently logged in users are called the prinipal to Spring Security
Principal is a person who you've identified through the process of authentication
It answers the question of How does the Authorization happen?
- Bunch of permissions of certain API's are given to a particular user
- This permission list can be called the term granted authority
- Role is the group of authority
The main difference between Role and Granted Authority is:
| Granted Authority | Role |
|---|---|
| Fine Grained grouping of permissions | Coarse grained grouping of permissions |
- Adds mandatory authentication for all URLs. Except Error page
- Adds a Login form
- Handles any Login error
- Creates a user and sets default password. We can see the password in the
console. We can also change it according to our own. to change it we have to change it toresource/application.propertiesand add
spring.security.user.name=foo
spring.security.user.password=bar
Add Dependancy --> spring-boot-starter-security
By adding this dependancy spring will work. But this happens?
This are done by Filters
Filters are the basic concept of Servelet Appications. All the app we run are servelet applcations. Individual Servelets are responsible for individual APIs.
Details:
- When a use hits into a link or make a request to the server, The Servelet Container will match from the list of servelet and choose it.
- Then the method of that particular servelet runs
- Filters are between this request and servelts. They can intercept all the requests. (As a result, We can see every user logs/ we can see if there is particular header file present in the request or not.)
- Servelets are mapped to URLs, filter can be applied to all URLs.
Spring securityalso adds another filter and allow all the request to allow or deny.
