Skip to content

Commit b73ffee

Browse files
committed
setup spring security
1 parent 8f3c385 commit b73ffee

19 files changed

+846
-12
lines changed

ARCHITECTURE.md

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
# Architecture
2+
<pre>
3+
+---------------------------------------------------------+
4+
| Client Application |
5+
+---------------------------------------------------------+
6+
|
7+
| Sends HTTP requests to API endpoints
8+
|
9+
+---------------------------------------------------------+
10+
| API Controllers/Endpoints |
11+
+---------------------------------------------------------+
12+
|
13+
| Handles incoming HTTP requests
14+
|
15+
+---------------------------------------------------------+
16+
| Security |
17+
+---------------------------------------------------------+
18+
|
19+
| Authenticates and authorizes requests
20+
|
21+
+---------------------------------------------------------+
22+
| Service Layer |
23+
+---------------------------------------------------------+
24+
|
25+
| Implements business logic and data manipulation
26+
|
27+
+---------------------------------------------------------+
28+
| Caching |
29+
+---------------------------------------------------------+
30+
|
31+
| Stores frequently accessed data in cache
32+
|
33+
+---------------------------------------------------------+
34+
| Data Access Layer (Repositories) |
35+
+---------------------------------------------------------+
36+
|
37+
| Interacts with the PostgreSQL database using Spring Data JPA
38+
|
39+
+---------------------------------------------------------+
40+
| PostgreSQL Database |
41+
+---------------------------------------------------------+
42+
|
43+
| Stores and retrieves data using SQL queries
44+
|
45+
+---------------------------------------------------------+
46+
47+
</pre>
48+
49+
50+
In this schema, the client application sends HTTP requests to API endpoints defined in the API Controllers/Endpoints. The API Controllers handle these requests and invoke the appropriate methods in the Service Layer. The Service Layer contains the business logic and performs data manipulation as needed. It interacts with the Data Access Layer, which includes repositories implemented using Spring Data JPA. The Data Access Layer communicates with the PostgreSQL database, storing and retrieving data using SQL queries.
51+
52+
The Security component is added to authenticate and authorize requests, ensuring proper access control to the API endpoints. The Caching component stores frequently accessed data in cache, which helps improve performance and reduce database load. The Messaging component allows the system to send and receive messages asynchronously, facilitating communication between different parts of the application or with external systems.
53+
54+
55+
## Libraries:
56+
57+
- Spring Boot Starter Web: This library includes everything you need to build a web application with Spring Boot, including embedded web server support, Spring MVC, and related components.
58+
59+
- Spring Boot Starter Data JPA: This library provides support for Java Persistence API (JPA) to interact with relational databases. It includes Spring Data JPA, Hibernate, and related dependencies.
60+
61+
- PostgreSQL Driver: You will need the PostgreSQL JDBC driver to connect your Spring Boot application with the PostgreSQL database. The driver allows your application to communicate with the database and execute SQL queries.
62+
63+
- Spring Boot Starter Test: This library includes testing dependencies for writing unit tests and integration tests for your Spring Boot application.
64+
65+
- JSON Web Token (JWT) Library: If you want to implement JWT-based authentication in your application, you will need a JWT library like jjwt. ()
66+
67+
## Standard in use:
68+
69+
- JSON Web Token Authentication
70+
71+
<pre>
72+
+---------------------------------------------------------+
73+
| Client Application |
74+
+---------------------------------------------------------+
75+
|
76+
| Sends login request with credentials
77+
|
78+
+---------------------------------------------------------+
79+
| Authentication Controller |
80+
+---------------------------------------------------------+
81+
|
82+
| Authenticates user credentials
83+
|
84+
+---------------------------------------------------------+
85+
| UserDetailsService Implementation |
86+
+---------------------------------------------------------+
87+
|
88+
| Retrieves user details from the database
89+
|
90+
+---------------------------------------------------------+
91+
| Security Configuration |
92+
+---------------------------------------------------------+
93+
|
94+
| Configures authentication manager, JWT filter, and endpoints
95+
|
96+
+---------------------------------------------------------+
97+
| JwtTokenProvider |
98+
+---------------------------------------------------------+
99+
|
100+
| Generates and validates JWT tokens
101+
|
102+
+---------------------------------------------------------+
103+
| API Endpoints/Controllers |
104+
+---------------------------------------------------------+
105+
|
106+
| Secured API endpoints
107+
|
108+
+---------------------------------------------------------+
109+
| Database |
110+
+---------------------------------------------------------+
111+
</pre>
112+
113+
In this schema, the client application sends a login request with user credentials. The Authentication Controller authenticates the user credentials and retrieves user details from the UserDetailsService implementation, which interacts with the database using Spring Data JPA. The Security Configuration class configures the authentication manager, JWT filter, and the secured API endpoints. The JwtTokenProvider generates and validates JWT tokens. The API endpoints/controllers handle the secured API requests. The database stores the user information.
114+
115+
Please note that this is a simplified schema to illustrate the major components involved. The actual implementation may involve additional classes, configurations, and interactions between components.
116+
117+
118+
119+
# Diagram illustrating the flow of JWT authentication
120+
121+
<pre>
122+
+---------------------------------------------------------+
123+
| Client Application |
124+
+---------------------------------------------------------+
125+
|
126+
| 1. Login Request (with credentials)
127+
|
128+
v
129+
+---------------------------------------------------------+
130+
| Authentication Server |
131+
+---------------------------------------------------------+
132+
|
133+
| 2. Authenticate User Credentials
134+
|
135+
v
136+
+---------------------------------------------------------+
137+
| User Repository |
138+
+---------------------------------------------------------+
139+
|
140+
| 3. Retrieve User Details
141+
|
142+
v
143+
+---------------------------------------------------------+
144+
| Generate JWT and Response |
145+
+---------------------------------------------------------+
146+
|
147+
| 4. Generate JWT with User Details
148+
|
149+
v
150+
+---------------------------------------------------------+
151+
| Client Application |
152+
+---------------------------------------------------------+
153+
|
154+
| 5. Receive JWT and store locally
155+
|
156+
v
157+
+---------------------------------------------------------+
158+
| Subsequent API Requests |
159+
+---------------------------------------------------------+
160+
|
161+
| 6. Include JWT in Header
162+
| (Authorization: Bearer <token>)
163+
v
164+
+---------------------------------------------------------+
165+
| Authentication Server |
166+
+---------------------------------------------------------+
167+
|
168+
| 7. Validate JWT and authorize access
169+
|
170+
v
171+
+---------------------------------------------------------+
172+
| Process Request and Respond |
173+
+---------------------------------------------------------+
174+
|
175+
| 8. Process Request and Respond
176+
|
177+
v
178+
+---------------------------------------------------------+
179+
| Client Application |
180+
+---------------------------------------------------------+
181+
|
182+
| 9. Receive Response
183+
|
184+
v
185+
</pre>
186+
187+
In this diagram:
188+
189+
- The client application sends a login request with credentials to the authentication server.
190+
- The authentication server verifies and authenticates the user's credentials.
191+
- The authentication server retrieves the user's details from the user repository.
192+
- The authentication server generates a JWT (JSON Web Token) containing the user's details.
193+
- The client application receives the JWT and stores it locally (e.g., in local storage or memory).
194+
- For subsequent API requests, the client application includes the JWT in the request header as "Authorization: Bearer <token>".
195+
- The authentication server validates the JWT to ensure its authenticity and authorizes access based on the user's role or permissions.
196+
- The authentication server processes the API request and generates a response.
197+
- The client application receives the response from the authentication server.
198+
199+
Please note that this is a simplified flow, and the actual implementation may involve additional steps or variations based on your specific requirements and application architecture.

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Learning sandbox
2+
3+
This is a learning project aimed at familiarizing myself with a new technology stack that includes Java, Spring Boot, and PostgreSQL.
4+
5+
## Brief introduction to these tecnologies:
6+
7+
- ### Java:
8+
Java is a popular, general-purpose programming language that was developed by Sun Microsystems (now owned by Oracle). It is known for its "write once, run anywhere" principle, meaning that Java code can run on any platform with a Java Virtual Machine (JVM). Java is widely used for developing a variety of applications, including desktop, web, mobile, and enterprise software.
9+
10+
- ### Spring Boot:
11+
Spring Boot is a framework built on top of the Spring Framework, which simplifies the development of Java applications. It provides a set of tools and conventions that make it easier to create standalone, production-ready Spring applications with minimal configuration. Spring Boot promotes convention over configuration and offers a wide range of features for building web applications, microservices, and RESTful APIs. It also includes embedded servers, dependency management, and auto-configuration capabilities.
12+
13+
- ### PostgreSQL:
14+
PostgreSQL is a powerful open-source relational database management system (RDBMS). It is known for its robustness, scalability, and compliance with SQL standards. PostgreSQL supports a wide range of advanced features, including transactions, ACID properties, data integrity, and extensibility. It is commonly used as a backend database for web applications, enterprise systems, and data-intensive projects. PostgreSQL is known for its reliability and is often favored for its ability to handle complex data models and high-volume workloads.
15+
16+
### Conclusion
17+
In combination, Java, Spring Boot, and PostgreSQL provide a powerful stack for building scalable and reliable applications. Java serves as the programming language, Spring Boot provides a streamlined development framework, and PostgreSQL offers a robust and feature-rich database solution. Together, they enable developers to create efficient, maintainable, and scalable applications for various use cases.
18+
19+
20+
Architecture description in Architecture.md

application.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
jwt.secret=kydodev

pom.xml

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,57 @@
1212
<artifactId>lab</artifactId>
1313
<version>0.0.1-SNAPSHOT</version>
1414
<name>lab</name>
15-
<description>Demo project for Spring Boot</description>
15+
<description>SkiaLab: project for Spring Boot</description>
1616
<properties>
1717
<java.version>20</java.version>
1818
</properties>
1919
<dependencies>
20-
2120
<dependency>
2221
<groupId>org.springframework.boot</groupId>
2322
<artifactId>spring-boot-starter-actuator</artifactId>
2423
</dependency>
25-
2624
<dependency>
2725
<groupId>org.springframework.boot</groupId>
2826
<artifactId>spring-boot-starter-web</artifactId>
2927
</dependency>
30-
3128
<dependency>
3229
<groupId>org.springframework.boot</groupId>
3330
<artifactId>spring-boot-starter-test</artifactId>
3431
<scope>test</scope>
3532
</dependency>
36-
37-
3833
<dependency>
3934
<groupId>org.springframework.boot</groupId>
4035
<artifactId>spring-boot-devtools</artifactId>
4136
<scope>runtime</scope>
4237
</dependency>
4338
<dependency>
44-
<groupId>org.projectlombok</groupId>
45-
<artifactId>lombok</artifactId>
46-
<scope>annotationProcessor</scope>
39+
<groupId>io.jsonwebtoken</groupId>
40+
<artifactId>jjwt-api</artifactId>
41+
<version>0.11.5</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-test</artifactId>
46+
<scope>test</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.postgresql</groupId>
50+
<artifactId>postgresql</artifactId>
51+
<scope>runtime</scope>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-starter-data-jpa</artifactId>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.springframework.boot</groupId>
59+
<artifactId>spring-boot-starter-validation</artifactId>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.springframework.boot</groupId>
63+
<artifactId>spring-boot-starter-security</artifactId>
4764
</dependency>
4865
</dependencies>
49-
5066
<build>
5167
<plugins>
5268
<plugin>
@@ -55,5 +71,5 @@
5571
</plugin>
5672
</plugins>
5773
</build>
58-
74+
5975
</project>

src/main/java/com/skia/lab/StorageController.java

Whitespace-only changes.

src/main/java/com/skia/lab/HomeController.java renamed to src/main/java/com/skia/lab/controllers/HomeController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.skia.lab;
1+
package com.skia.lab.controllers;
22

33
import org.springframework.web.bind.annotation.GetMapping;
44
import org.springframework.web.bind.annotation.RestController;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package com.skia.lab.controllers;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.skia.lab.models;
2+
3+
public enum ERole {
4+
ROLE_VIEWWR,
5+
ROLE_USER,
6+
ROLE_MODERATOR,
7+
ROLE_ADMIN,
8+
ROLE_GOD
9+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.skia.lab.models;
2+
3+
import jakarta.persistence.*;
4+
5+
@Entity
6+
@Table(name = "roles")
7+
public class Role {
8+
@Id
9+
@GeneratedValue(strategy = GenerationType.IDENTITY)
10+
private Integer id;
11+
12+
@Enumerated(EnumType.STRING)
13+
@Column(length = 20)
14+
private ERole name;
15+
16+
public Role() {
17+
18+
}
19+
20+
public Role(ERole name) {
21+
this.name = name;
22+
}
23+
24+
public Integer getId() {
25+
return id;
26+
}
27+
28+
public void setId(Integer id) {
29+
this.id = id;
30+
}
31+
32+
public ERole getName() {
33+
return name;
34+
}
35+
36+
public void setName(ERole name) {
37+
this.name = name;
38+
}
39+
}

0 commit comments

Comments
 (0)