Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgraded to Java 17, Spring Boot 3, Added PostgreSQL database and Symmetric key encryption #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<version>3.2.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.kriscfoster</groupId>
Expand All @@ -14,7 +14,7 @@
<name>school</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
Expand All @@ -26,16 +26,27 @@
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-devtools</artifactId>-->
<!-- <scope>runtime</scope>-->
<!-- <optional>true</optional>-->
<!-- </dependency>-->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/kriscfoster/school/WebConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.kriscfoster.school;

import org.h2.server.web.WebServlet;
//import org.h2.server.web.WebServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WebConfiguration {
@Bean
ServletRegistrationBean h2ServletRegistration() {
ServletRegistrationBean registrationBean = new ServletRegistrationBean( new WebServlet());
registrationBean.addUrlMappings("/console/*");
return registrationBean;
}
// @Bean
// ServletRegistrationBean h2ServletRegistration() {
// ServletRegistrationBean registrationBean = new ServletRegistrationBean( new WebServlet());
// registrationBean.addUrlMappings("/console/*");
// return registrationBean;
// }
}
10 changes: 9 additions & 1 deletion src/main/java/com/kriscfoster/school/student/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.kriscfoster.school.subject.Subject;
import jakarta.persistence.*;
import org.hibernate.annotations.ColumnTransformer;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

Expand All @@ -14,6 +15,13 @@ public class Student {
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;

private static final String symkey = "secret-key-12345";

@Column(name = "name", columnDefinition = "bytea")
@ColumnTransformer(
read = "PGP_SYM_DECRYPT(name,'"+symkey+"')",
write = "PGP_SYM_ENCRYPT (?, '"+symkey+"')"
)
private String name;

@JsonIgnore
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/kriscfoster/school/subject/Subject.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import com.kriscfoster.school.student.Student;
import com.kriscfoster.school.teacher.Teacher;
import jakarta.persistence.*;
import org.hibernate.annotations.ColumnTransformer;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

Expand All @@ -14,6 +15,13 @@ public class Subject {
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;

private static final String symkey = "secret-key-12345";

@Column(name = "name", columnDefinition = "bytea")
@ColumnTransformer(
read = "PGP_SYM_DECRYPT(name,'"+symkey+"')",
write = "PGP_SYM_ENCRYPT (?, '"+symkey+"')"
)
private String name;

@ManyToMany
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/kriscfoster/school/teacher/Teacher.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.kriscfoster.school.subject.Subject;
import jakarta.persistence.*;
import org.hibernate.annotations.ColumnTransformer;

import javax.persistence.*;
import java.util.Set;

@Entity
Expand All @@ -17,6 +18,13 @@ public class Teacher {
@OneToMany(mappedBy = "teacher")
private Set<Subject> subjects;

private static final String symkey = "secret-key-12345";

@Column(name = "name", columnDefinition = "bytea")
@ColumnTransformer(
read = "PGP_SYM_DECRYPT(name,'"+symkey+"')",
write = "PGP_SYM_ENCRYPT (?, '"+symkey+"')"
)
private String name;

public Long getId() {
Expand Down
16 changes: 13 additions & 3 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb
# h2 console is available at http://localhost:8080/console
spring.datasource.url=jdbc:postgresql://localhost:5436/student
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.format_sql=true

server.error.include-message=always

app.name=DemoApp

key=mykey