Spring Security
Spring Security
==============
Spring Security
==============
- Spring Security is a framework that belongs to application-level security and
provides multiple readymade filters to enable security filter.
Authentication
- It is first level to checking whether the person is right or not to access our
application on the basis of username and password.
- Authentication = Identification[username] + verification[password]
- Verifying the user credential like user Login details.
Authorization
- It is second level to checking the permission, when an authenticated users try to
access a particular service of our application.
- Validating User Role, can this user access specified functionality/service
- Role is nothing but designations given to the users
===================
Authorization Levels
==================
1. permitAll: A URL/Path can be accessed by every one for this no login/ no role is
required.
Example: /home, /login, /contactUs, /aboutUs
Reference: https://www.baeldung.com/security-spring
https://reflectoring.io/spring-security/
Spring Security [JAAS]
3. hasAuthority: Login must and valid Role is required (after login which service
he/she can access).
Example: /approveLoan => Login and Role: Manager
4. hasAnyAuthority: Login must and valid Roles is required (one end point/services
can access either customer or admin)
==========================================
How to secure/authorization specific URL Patterns
==========================================
1. @Controller
2. @RequestMapping("/bank")
3. public class BankOperationController {
4.
5. @GetMapping("/")
6. public String showHome() {…}
7.
8. @GetMapping("/offers")
9. public String showOffers() {…}
10.
11. @GetMapping("/approveLoan")
12. public String loans() {…}
13. }
Reference: https://www.baeldung.com/security-spring
https://reflectoring.io/spring-security/
Spring Security [JAAS]
requestMatchers(“/bank**”).hasRole(“MANAGER”) means MANGER can access “/”,
“/offers”, “/approveLoan” any services.
requestMatchers(“/emp*”).hasAuthority(“ADMIN”) means any service can access
those endpoints are started with /emp
In Spring or spring boot environment, we can apply security on MVC apps or ReST
application 3 approaches -
----------------------------------------
Using Default Authentication
---------------------------------------
To secure our spring boot application we need to add below starter in pom.xml file
1. <dependency>
2. <groupId>org.springframework.boot</groupId>
3. <artifactId>spring-boot-starter-security</artifactId>
4. </dependency>
Note: When we add this dependency in pom.xml file then by default our application will
be secured with basic authentication and authorization for all the HTTP methods of our
Reference: https://www.baeldung.com/security-spring
https://reflectoring.io/spring-security/
Spring Security [JAAS]
application. It will generate random password to access our application that will be print
on console.
When we access our application URL in browser then it will display "Login Form" to
authenticate our request. So, we need to use below credentials to access our
application.
Username: user
Password: on console
----------------------------------------------------------------------
Using properties file as authentication info provider
----------------------------------------------------------------------
To set our own credential we need to configure spring security credential in
application.properties file .
Spring.security.user.name = <SetUserName>
Spring.security.user.password = <SetPassword>
----------------------------------------------------------------------
Using InMemory DB as authentication info provider
----------------------------------------------------------------------
- Storing user credential details in temporary memory (RAM level), no database is
used. This is only for testing purpose.
Reference: https://www.baeldung.com/security-spring
https://reflectoring.io/spring-security/
Spring Security [JAAS]
==========================
Application with InMemory
=========================
1. Application Name: 01.SpringSecurity-InMemoryAuth
2. Dependencies:
Spring Web,
Spring Security
Thymeleaf
3. Define Controller class: HomeController
4. In src/main/resources/templates create html pages
5. Define Java Based configuration Class with name SecurityConfiguration with
@Configuration
@EnableWebSecurity
And define two methods one is for Authentication and other is for Authorization
Reference: https://www.baeldung.com/security-spring
https://reflectoring.io/spring-security/
Spring Security [JAAS]
14. }
===================
Data Confidentiality
==================
- Data will be transferred on the network but it must be transferred by encrypting
data.
- So, we do not store plain text as password. Store Password as encoded.
PasswordEncoder:
- If we want to encode user password before store into DB then we can use
PasswordEncoder that is provided by Spring security.
Example:
===========================================
Spring Security Application with JDBCAuthentication
===========================================
Step #01: Create two tables one is for user and other is for authorities by using below
queries –
CREATE TABLE USERS (
USERNAME NVARCHAR2(128) PRIMARY KEY,
PASSWORD NVARCHAR2(128) NOT NULL,
ENABLED CHAR(1) CHECK (ENABLED IN ('Y','N') ) NOT NULL
);
CREATE TABLE AUTHORITIES (
USERNAME NVARCHAR2(128) NOT NULL,
AUTHORITY NVARCHAR2(128) NOT NULL
);
Reference: https://www.baeldung.com/security-spring
https://reflectoring.io/spring-security/
Spring Security [JAAS]
insert into users values ('user', '$2a$12$cDgq/OPn7tyRYQWwft5ptu/8Lh55TQYC/CyYYQCqK4YdQz.wkg5cK', ‘Y’);
And define two methods one is for Authentication and other is for Authorization
OAuth 2.x
Reference: https://www.baeldung.com/security-spring
https://reflectoring.io/spring-security/