Skip to content

Commit 6939d05

Browse files
committed
Add Spring security and define user account entites and link them to student or instructor
1 parent 2ef55d1 commit 6939d05

19 files changed

+354
-53
lines changed

build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ configurations {
2626

2727
dependencies {
2828
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
29+
implementation 'org.springframework.boot:spring-boot-starter-security'
30+
implementation 'org.springframework.boot:spring-boot-starter-validation'
2931
implementation 'org.springframework.boot:spring-boot-starter-web'
32+
3033
runtimeOnly 'org.postgresql:postgresql'
3134

3235
developmentOnly 'org.springframework.boot:spring-boot-devtools'

db/000-init.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1+
\i /docker-entrypoint-initdb.d/schema/user_account.sql
12
\i /docker-entrypoint-initdb.d/schema/student.sql
23
\i /docker-entrypoint-initdb.d/schema/instructor.sql
34
\i /docker-entrypoint-initdb.d/schema/course.sql
4-
\i /docker-entrypoint-initdb.d/schema/enrollment.sql
5+
\i /docker-entrypoint-initdb.d/schema/enrollment.sql

db/001-demo-data.sql

Lines changed: 71 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,85 @@
1-
-- Students
2-
INSERT INTO student (first_name, last_name, email)
3-
VALUES ('Alice', 'Smith', 'alice1@example.com'),
4-
('Bob', 'Johnson', 'bob2@example.com'),
5-
('Carol', 'Lee', 'carol3@example.com'),
6-
('David', 'Kim', 'david4@example.com'),
7-
('Eva', 'Brown', 'eva5@example.com'),
8-
('Frank', 'White', 'frank6@example.com'),
9-
('Grace', 'Green', 'grace7@example.com'),
10-
('Henry', 'Black', 'henry8@example.com'),
11-
('Ivy', 'Young', 'ivy9@example.com'),
12-
('Jack', 'Hall', 'jack10@example.com'),
13-
('Kara', 'King', 'kara11@example.com'),
14-
('Liam', 'Scott', 'liam12@example.com'),
15-
('Mia', 'Adams', 'mia13@example.com'),
16-
('Noah', 'Baker', 'noah14@example.com'),
17-
('Olivia', 'Clark', 'olivia15@example.com'),
18-
('Paul', 'Davis', 'paul16@example.com'),
19-
('Quinn', 'Evans', 'quinn17@example.com'),
20-
('Ruby', 'Foster', 'ruby18@example.com'),
21-
('Sam', 'Gray', 'sam19@example.com'),
22-
('Tina', 'Harris', 'tina20@example.com');
1+
-- User accounts for students. All passwords are the same: 'password123' hashed
2+
INSERT INTO user_account (password_hash, email_address, role)
3+
VALUES ('$2a$10$JPQ/pnZqC8efUOi3M9ZqReeNDR7IkA1Ry973r.IK020zHSuP4P.KC', 'alice1@example.com', 'student'),
4+
('$2a$10$JPQ/pnZqC8efUOi3M9ZqReeNDR7IkA1Ry973r.IK020zHSuP4P.KC', 'bob2@example.com', 'student'),
5+
('$2a$10$JPQ/pnZqC8efUOi3M9ZqReeNDR7IkA1Ry973r.IK020zHSuP4P.KC', 'carol3@example.com', 'student'),
6+
('$2a$10$JPQ/pnZqC8efUOi3M9ZqReeNDR7IkA1Ry973r.IK020zHSuP4P.KC', 'david4@example.com', 'student'),
7+
('$2a$10$JPQ/pnZqC8efUOi3M9ZqReeNDR7IkA1Ry973r.IK020zHSuP4P.KC', 'eva5@example.com', 'student'),
8+
('$2a$10$JPQ/pnZqC8efUOi3M9ZqReeNDR7IkA1Ry973r.IK020zHSuP4P.KC', 'frank6@example.com', 'student'),
9+
('$2a$10$JPQ/pnZqC8efUOi3M9ZqReeNDR7IkA1Ry973r.IK020zHSuP4P.KC', 'grace7@example.com', 'student'),
10+
('$2a$10$JPQ/pnZqC8efUOi3M9ZqReeNDR7IkA1Ry973r.IK020zHSuP4P.KC', 'henry8@example.com', 'student'),
11+
('$2a$10$JPQ/pnZqC8efUOi3M9ZqReeNDR7IkA1Ry973r.IK020zHSuP4P.KC', 'ivy9@example.com', 'student'),
12+
('$2a$10$JPQ/pnZqC8efUOi3M9ZqReeNDR7IkA1Ry973r.IK020zHSuP4P.KC', 'jack10@example.com', 'student'),
13+
('$2a$10$JPQ/pnZqC8efUOi3M9ZqReeNDR7IkA1Ry973r.IK020zHSuP4P.KC', 'kara11@example.com', 'student'),
14+
('$2a$10$JPQ/pnZqC8efUOi3M9ZqReeNDR7IkA1Ry973r.IK020zHSuP4P.KC', 'liam12@example.com', 'student'),
15+
('$2a$10$JPQ/pnZqC8efUOi3M9ZqReeNDR7IkA1Ry973r.IK020zHSuP4P.KC', 'mia13@example.com', 'student'),
16+
('$2a$10$JPQ/pnZqC8efUOi3M9ZqReeNDR7IkA1Ry973r.IK020zHSuP4P.KC', 'noah14@example.com', 'student'),
17+
('$2a$10$JPQ/pnZqC8efUOi3M9ZqReeNDR7IkA1Ry973r.IK020zHSuP4P.KC', 'olivia15@example.com', 'student'),
18+
('$2a$10$JPQ/pnZqC8efUOi3M9ZqReeNDR7IkA1Ry973r.IK020zHSuP4P.KC', 'paul16@example.com', 'student'),
19+
('$2a$10$JPQ/pnZqC8efUOi3M9ZqReeNDR7IkA1Ry973r.IK020zHSuP4P.KC', 'quinn17@example.com', 'student'),
20+
('$2a$10$JPQ/pnZqC8efUOi3M9ZqReeNDR7IkA1Ry973r.IK020zHSuP4P.KC', 'ruby18@example.com', 'student'),
21+
('$2a$10$JPQ/pnZqC8efUOi3M9ZqReeNDR7IkA1Ry973r.IK020zHSuP4P.KC', 'sam19@example.com', 'student'),
22+
('$2a$10$JPQ/pnZqC8efUOi3M9ZqReeNDR7IkA1Ry973r.IK020zHSuP4P.KC', 'tina20@example.com', 'student'),
23+
('$2a$10$JPQ/pnZqC8efUOi3M9ZqReeNDR7IkA1Ry973r.IK020zHSuP4P.KC', 'alan@example.com', 'instructor'),
24+
('$2a$10$JPQ/pnZqC8efUOi3M9ZqReeNDR7IkA1Ry973r.IK020zHSuP4P.KC', 'brenda@example.com', 'instructor'),
25+
('$2a$10$JPQ/pnZqC8efUOi3M9ZqReeNDR7IkA1Ry973r.IK020zHSuP4P.KC', 'charles@example.com', 'instructor'),
26+
('$2a$10$JPQ/pnZqC8efUOi3M9ZqReeNDR7IkA1Ry973r.IK020zHSuP4P.KC', 'diana@example.com', 'instructor'),
27+
('$2a$10$JPQ/pnZqC8efUOi3M9ZqReeNDR7IkA1Ry973r.IK020zHSuP4P.KC', 'edward@example.com', 'instructor');
28+
29+
30+
-- Students (with user_id references)
31+
INSERT INTO student (student_id, first_name, last_name, enrollment_date)
32+
VALUES (1, 'Alice', 'Smith', '2023-09-01'),
33+
(2, 'Bob', 'Johnson', '2023-09-01'),
34+
(3, 'Carol', 'Lee', '2023-09-01'),
35+
(4, 'David', 'Kim', '2023-09-01'),
36+
(5, 'Eva', 'Brown', '2023-09-01'),
37+
(6, 'Frank', 'White', '2023-09-01'),
38+
(7, 'Grace', 'Green', '2023-09-01'),
39+
(8, 'Henry', 'Black', '2023-09-01'),
40+
(9, 'Ivy', 'Young', '2023-09-01'),
41+
(10, 'Jack', 'Hall', '2023-09-01'),
42+
(11, 'Kara', 'King', '2023-09-01'),
43+
(12, 'Liam', 'Scott', '2023-09-01'),
44+
(13, 'Mia', 'Adams', '2023-09-01'),
45+
(14, 'Noah', 'Baker', '2023-09-01'),
46+
(15, 'Olivia', 'Clark', '2023-09-01'),
47+
(16, 'Paul', 'Davis', '2023-09-01'),
48+
(17, 'Quinn', 'Evans', '2023-09-01'),
49+
(18, 'Ruby', 'Foster', '2023-09-01'),
50+
(19, 'Sam', 'Gray', '2023-09-01'),
51+
(20, 'Tina', 'Harris', '2023-09-01');
52+
2353

2454
-- Instructors
25-
INSERT INTO instructor (first_name, last_name, email)
26-
VALUES ('Alan', 'Murray', 'alan@example.com'),
27-
('Brenda', 'Stone', 'brenda@example.com'),
28-
('Charles', 'Ford', 'charles@example.com'),
29-
('Diana', 'Wells', 'diana@example.com'),
30-
('Edward', 'Lane', 'edward@example.com');
55+
INSERT INTO instructor (instructor_id, first_name, last_name, hire_date)
56+
VALUES (21, 'Alan', 'Murray', '2021-04-01'),
57+
(22, 'Brenda', 'Stone', '2023-09-01'),
58+
(23, 'Charles', 'Ford', '2020-01-01'),
59+
(24, 'Diana', 'Wells', '2019-02-01'),
60+
(25, 'Edward', 'Lane', '2018-03-01');
3161

3262
-- Courses
3363
INSERT INTO course (name, description, instructor_id, start_date, end_date, max_enrollments)
34-
VALUES ('Introduction to Calculus', 'Fundamental concepts of calculus including limits, derivatives, and integrals.', 1,
64+
VALUES ('Introduction to Calculus', 'Fundamental concepts of calculus including limits, derivatives, and integrals.',
65+
21,
3566
'2024-09-01', '2024-12-15', 30),
36-
('General Physics I', 'Mechanics, motion, energy, and basic physical laws.', 2, '2024-09-01', '2024-12-15', 28),
37-
('Organic Chemistry', 'Structure, properties, and reactions of organic compounds.', 3, '2024-09-01',
67+
('General Physics I', 'Mechanics, motion, energy, and basic physical laws.', 22, '2024-09-01', '2024-12-15', 28),
68+
('Organic Chemistry', 'Structure, properties, and reactions of organic compounds.', 23, '2024-09-01',
3869
'2024-12-15', 25),
39-
('Human Biology', 'Overview of human anatomy, physiology, and genetics.', 4, '2024-09-01', '2024-12-15', 32),
40-
('World History: 1500-Present', 'Major global events and trends from 1500 to the present.', 5, '2024-09-01',
70+
('Human Biology', 'Overview of human anatomy, physiology, and genetics.', 24, '2024-09-01', '2024-12-15', 32),
71+
('World History: 1500-Present', 'Major global events and trends from 1500 to the present.', 25, '2024-09-01',
4172
'2024-12-15', 50),
42-
('British Literature', 'Study of classic and modern British literary works.', 1, '2024-09-01', '2024-12-15', 22),
43-
('Fundamentals of Drawing', 'Techniques and principles of drawing for beginners.', 2, '2024-09-01', '2024-12-15',
73+
('British Literature', 'Study of classic and modern British literary works.', 21, '2024-09-01', '2024-12-15',
74+
22),
75+
('Fundamentals of Drawing', 'Techniques and principles of drawing for beginners.', 22, '2024-09-01',
76+
'2024-12-15',
4477
18),
45-
('Music Theory and Composition', 'Basics of music theory and introductory composition.', 3, '2024-09-01',
78+
('Music Theory and Composition', 'Basics of music theory and introductory composition.', 23, '2024-09-01',
4679
'2024-12-15', 20),
47-
('Introduction to Programming with Java', 'Core Java programming concepts and hands-on projects.', 4,
80+
('Introduction to Programming with Java', 'Core Java programming concepts and hands-on projects.', 24,
4881
'2024-09-01', '2024-12-15', 35),
49-
('Principles of Microeconomics', 'Microeconomic theory, market structures, and consumer behavior.', 5,
82+
('Principles of Microeconomics', 'Microeconomic theory, market structures, and consumer behavior.', 25,
5083
'2024-09-01', '2024-12-15', 18);
5184

5285
-- Enrollments

db/schema/course.sql

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
CREATE TABLE course
22
(
3-
course_id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
4-
name VARCHAR(255) NOT NULL,
5-
description TEXT,
6-
instructor_id INTEGER REFERENCES instructor (instructor_id) ON DELETE SET NULL,
7-
start_date DATE,
8-
end_date DATE,
9-
max_enrollments INT CHECK (max_enrollments > 0)
3+
course_id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
4+
name VARCHAR(255) NOT NULL,
5+
description TEXT,
6+
instructor_id INTEGER REFERENCES instructor (instructor_id) ON DELETE SET NULL,
7+
start_date DATE,
8+
end_date DATE,
9+
max_enrollments INT CHECK (max_enrollments > 0)
1010
);

db/schema/enrollment.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
-- An enrollment is a student taking a course.
12
CREATE TABLE enrollment
23
(
34
enrollment_id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,

db/schema/instructor.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
CREATE TABLE instructor
22
(
3-
instructor_id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
3+
instructor_id INTEGER PRIMARY KEY,
44
first_name VARCHAR NOT NULL,
55
last_name VARCHAR NOT NULL,
6-
email VARCHAR(255)
6+
hire_date DATE NOT NULL DEFAULT current_date,
7+
FOREIGN KEY (instructor_id) REFERENCES user_account (user_id) ON DELETE CASCADE
78
);

db/schema/student.sql

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
CREATE TABLE student
22
(
3-
student_id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
4-
first_name VARCHAR NOT NULL,
5-
last_name VARCHAR NOT NULL,
6-
email VARCHAR(255)
3+
student_id INTEGER PRIMARY KEY,
4+
first_name VARCHAR NOT NULL,
5+
last_name VARCHAR NOT NULL,
6+
enrollment_date DATE NOT NULL DEFAULT current_date,
7+
FOREIGN KEY (student_id) REFERENCES user_account (user_id) ON DELETE CASCADE
78
);

db/schema/user_account.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- User account (used for user authentication and authorization)
2+
CREATE TYPE role AS ENUM ('instructor', 'student');
3+
4+
CREATE TABLE user_account
5+
(
6+
user_id BIGSERIAL PRIMARY KEY,
7+
password_hash VARCHAR(60) NOT NULL,
8+
email_address VARCHAR(100) UNIQUE NOT NULL,
9+
role role NOT NULL
10+
);

docker-compose.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ services:
1010
POSTGRES_DB: coursehub
1111
ports:
1212
- "5432:5432"
13+
redis:
14+
image: redis:7
15+
container_name: redis
16+
ports:
17+
- "6379:6379"
1318
course-hub:
1419
image: ghcr.io/hackyourfuture/course-hub-backend:latest
1520
container_name: course-hub
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package net.hackyourfuture.coursehub;
2+
3+
import net.hackyourfuture.coursehub.service.UserAuthenticationService;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.security.authentication.AuthenticationManager;
7+
import org.springframework.security.config.Customizer;
8+
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
9+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
10+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
11+
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
12+
import org.springframework.security.core.userdetails.UserDetailsService;
13+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
14+
import org.springframework.security.crypto.password.PasswordEncoder;
15+
import org.springframework.security.web.SecurityFilterChain;
16+
17+
@Configuration
18+
@EnableWebSecurity
19+
public class SecurityConfig {
20+
@Bean
21+
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
22+
return http.csrf(AbstractHttpConfigurer::disable)
23+
.authorizeHttpRequests(auth -> auth.requestMatchers("/", "/login", "/register")
24+
.permitAll()
25+
.anyRequest()
26+
.authenticated())
27+
.httpBasic(Customizer.withDefaults())
28+
.formLogin(Customizer.withDefaults())
29+
.logout(Customizer.withDefaults())
30+
.build();
31+
}
32+
33+
@Bean
34+
public PasswordEncoder passwordEncoder() {
35+
return new BCryptPasswordEncoder();
36+
}
37+
38+
@Bean
39+
public UserDetailsService userDetailsService(UserAuthenticationService userAuthenticationService) {
40+
return userAuthenticationService;
41+
}
42+
43+
@Bean
44+
public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration)
45+
throws Exception {
46+
return authenticationConfiguration.getAuthenticationManager();
47+
}
48+
}

0 commit comments

Comments
 (0)