Security
- security is for protecting your data and business logic inside your web applications, and data
- it's essential for web application(it always need, no matter what a business logic is)
- there are different types of security
- firewalls
- https
- ssl
- authentication
- authorization
- Using Security we should also avoid most common security attacks like CSRF, Broken Authentication inside our application
Why Spring Security
- you could add Security code without update business logic
- open source framework no need money
- can handle common security vulnerabilities like CSRF, CORs
- supports various standards of security to implement authentication, like using username/password authentication, JWT tokens, OAuth2, OpenId etc.
Servlets & Filters
- spring security works on filters
- servelt
- sclient send https request but server can't understand it
- so, servlet container like apache tomcat convert https request to java code then server can understand it
- and servelt container also convert server response message to https message
- filter
- filter is special kinds of servelt, that we can use intercept every request, response
- so using the same filters, Spring security enforce security based on our configurations inside a web application
![](https://velog.velcdn.com/images/kgb/post/177bc136-d6aa-4235-90c0-5fbae19ac502/image.png)
Spring Security Internal flow
- essential is Spring Security works by filter chain
![](https://velog.velcdn.com/images/kgb/post/4c1c9498-2a10-4228-99b5-0fcf7429d482/image.png)
- user enter requests!
- spring secufiry filters intercept each request work together to identify if Authentication is required or not
- if authentication is required, accordingly navigate the user to login page or use the existing details stored during initial authentication
- Autehntication
- Filter like UsernamePasswordAuthenticationFilter will extract username/password for HTTP request prepare Authentication type object
- Authentication Manager
- Once received request from filter, it delegates the validating of the user details to the authentication providers available
- since there can be multiple providers inside an app, it is the responsibility of the AutehnticationManager to manage all the authentication providers available.
- AuthenticationProvider
- AuthenticationProviders has all the core logic of validating user details for authentication
- UserDetailsManager/UserDetailService
- helps in retrieving, creating, updating, deleting User Details from the DB systems.
- PasswordEncoder
- helping encoding hashing passwords.
- SecurityContext
- once the request has been authenticated, the Authentication will usually be stored in a thread-local SecurityContext managed by the SecurityContextHolder.
- this helps during the upcoming requests from the same user
![](https://velog.velcdn.com/images/kgb/post/87f3e05f-748f-47a9-941b-fb6ed53a249e/image.png)
The Way Spring Security handle multiple request
- once user logined(Authentication ok) then security do not request another authentication for user
- before you login web application cookie has session id
![](https://velog.velcdn.com/images/kgb/post/1d5b1453-85d7-49b8-bc4a-80b734c3a1b3/image.png)
- after login session id change
![](https://velog.velcdn.com/images/kgb/post/59e531af-aab2-423a-b141-09399816f1da/image.png)
- if you use same cookie, spring security think this user is authenticated user, and skip login
- if session id removed or changed you need to login again
유익한 글이었습니다.