diff --git a/pom.xml b/pom.xml index a7a2240aac6d1062475349b5f0b30bc5b0871b38..16624f351c417dff6a2c802da189d215ae9ad266 100644 --- a/pom.xml +++ b/pom.xml @@ -57,6 +57,10 @@ <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-data-jpa</artifactId> + </dependency> </dependencies> <build> diff --git a/src/main/java/jez/authentication2/controllers/MainController.java b/src/main/java/jez/authentication2/controllers/MainController.java index 04ef8243b305e3e0642fc29af8b8172453c6a3fb..68d70f68b6dc736e3834726a7209a7a7442ab5a1 100644 --- a/src/main/java/jez/authentication2/controllers/MainController.java +++ b/src/main/java/jez/authentication2/controllers/MainController.java @@ -11,12 +11,12 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import jez.authentication2.services.NotesService; -import jez.authentication2.services.UserService; +import jez.authentication2.services.UserService2; @Controller public class MainController { @Autowired - UserService userService; + UserService2 userService; @Autowired NotesService notesService; @@ -39,9 +39,9 @@ public class MainController { String username = authentication.getName(); String notes = notesService.getNotes(username, note_id); - System.out.println(notes); - System.out.println(note_id); - System.out.println(userService.getUser(username)); + // System.out.println(notes); + // System.out.println(note_id); + // System.out.println(userService.getUser(username)); model.addAttribute("username", username); model.addAttribute("notes", notes); diff --git a/src/main/java/jez/authentication2/controllers/RegistrationController.java b/src/main/java/jez/authentication2/controllers/RegistrationController.java index 7a74ed953c87869660cc327a4c06c7d11cf023d4..0f37893a9c1452becc2d27c81f9c20ce299cf1e0 100644 --- a/src/main/java/jez/authentication2/controllers/RegistrationController.java +++ b/src/main/java/jez/authentication2/controllers/RegistrationController.java @@ -10,12 +10,12 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import jez.authentication2.entities.User; import jez.authentication2.exceptions.DuplicateUsernameException; -import jez.authentication2.services.UserService; +import jez.authentication2.services.UserService2; @Controller public class RegistrationController { @Autowired - UserService userService; + UserService2 userService; @GetMapping("/register") public String displayRegistrationPage(Model model) @@ -30,7 +30,7 @@ public class RegistrationController { user.setEnabled(true); try { - userService.addUser(user); + userService.saveUser(user); } catch (DuplicateUsernameException e) { e.printStackTrace(); System.out.println("duplicate"); diff --git a/src/main/java/jez/authentication2/entities/User.java b/src/main/java/jez/authentication2/entities/User.java index aa050ea1f6d3e493ffc8c4780867b18d789d407f..06e8c2c8724af47c01946267f5e4fb4e24308fcf 100644 --- a/src/main/java/jez/authentication2/entities/User.java +++ b/src/main/java/jez/authentication2/entities/User.java @@ -1,58 +1,59 @@ package jez.authentication2.entities; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "users") public class User { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; private String username; private String password; private boolean enabled; - private String text; - - public User(String username, String password, boolean enabled, String text) { + + public User(Integer id, String username, String password, boolean enabled, String text) { + this.id = id; this.username = username; this.password = password; this.enabled = enabled; - this.text = text; } - - public User() { + public User() {} + public Integer getId() { + return id; + } + public void setId(Integer id) { + this.id = id; } - public String getUsername() { return username; } - public void setUsername(String username) { this.username = username; } - public String getPassword() { return password; } - public void setPassword(String password) { this.password = password; } - public boolean isEnabled() { return enabled; } - public void setEnabled(boolean enabled) { this.enabled = enabled; } - - public String getText() { - return text; - } - - public void setText(String text) { - this.text = text; - } - @Override public String toString() { - return "User [username=" + username + ", password=" + password + ", enabled=" + enabled - + ", text=" + text + "]"; + return "User [id=" + id + ", username=" + username + ", password=" + password + ", enabled=" + + enabled + "]"; } - + + + } diff --git a/src/main/java/jez/authentication2/repositories/UserRepository.java b/src/main/java/jez/authentication2/repositories/UserRepository.java new file mode 100644 index 0000000000000000000000000000000000000000..c211a8f58fe482edf43310236aaf8d4a1a46a1f4 --- /dev/null +++ b/src/main/java/jez/authentication2/repositories/UserRepository.java @@ -0,0 +1,8 @@ +package jez.authentication2.repositories; + +import org.springframework.data.jpa.repository.JpaRepository; +import jez.authentication2.entities.User; + +public interface UserRepository extends JpaRepository<User, Integer> { + +} diff --git a/src/main/java/jez/authentication2/services/UserService.java b/src/main/java/jez/authentication2/services/UserService.java index 7d1db2d9ea90002e8c7f3dced795840ad9238fc5..61435ff68c20e31e858bf4570e9bfa7d24ea71c0 100644 --- a/src/main/java/jez/authentication2/services/UserService.java +++ b/src/main/java/jez/authentication2/services/UserService.java @@ -1,51 +1,51 @@ -package jez.authentication2.services; +// package jez.authentication2.services; -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import javax.sql.DataSource; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import jez.authentication2.entities.User; -import jez.authentication2.exceptions.DuplicateUsernameException; +// import java.sql.Connection; +// import java.sql.ResultSet; +// import java.sql.SQLException; +// import java.sql.Statement; +// import javax.sql.DataSource; +// import org.springframework.beans.factory.annotation.Autowired; +// import org.springframework.stereotype.Service; +// import jez.authentication2.entities.User; +// import jez.authentication2.exceptions.DuplicateUsernameException; -@Service -public class UserService { - @Autowired - DataSource dataSource; +// @Service +// public class UserService { +// @Autowired +// DataSource dataSource; - public void addUser(User user) throws DuplicateUsernameException, SQLException { - Connection conn = dataSource.getConnection(); - Statement statement = conn.createStatement(); +// public void addUser(User user) throws DuplicateUsernameException, SQLException { +// Connection conn = dataSource.getConnection(); +// Statement statement = conn.createStatement(); - try { - statement.executeUpdate(""" - INSERT INTO users (username, password, enabled) VALUES ('%s', '%s', '%s'); - """.formatted(user.getUsername(), user.getPassword(), user.isEnabled())); - } catch (SQLException e) { - e.printStackTrace(); - if (e.getErrorCode() == 0) { - throw new DuplicateUsernameException(); - } - } +// try { +// statement.executeUpdate(""" +// INSERT INTO users (username, password, enabled) VALUES ('%s', '%s', '%s'); +// """.formatted(user.getUsername(), user.getPassword(), user.isEnabled())); +// } catch (SQLException e) { +// e.printStackTrace(); +// if (e.getErrorCode() == 0) { +// throw new DuplicateUsernameException(); +// } +// } - statement.executeUpdate(""" - INSERT INTO authorities (username, authority) VALUES ('%s', '%s'); - """.formatted(user.getUsername(), "ROLE_USER")); - statement.executeUpdate(""" - INSERT INTO notes (username, notes) - values ('%s', 'write notes here'); - """.formatted(user.getUsername())); - } +// statement.executeUpdate(""" +// INSERT INTO authorities (username, authority) VALUES ('%s', '%s'); +// """.formatted(user.getUsername(), "ROLE_USER")); +// statement.executeUpdate(""" +// INSERT INTO notes (username, notes) +// values ('%s', 'write notes here'); +// """.formatted(user.getUsername())); +// } - public User getUser(String username) throws SQLException { - Connection conn = dataSource.getConnection(); - Statement statement = conn.createStatement(); - ResultSet result = statement - .executeQuery("SELECT username, password, enabled, notes FROM users NATURAL JOIN notes WHERE username='%s';".formatted(username)); - result.next(); - return new User(result.getString(1), result.getString(2), result.getBoolean(3), - result.getString(4)); - } -} +// public User getUser(String username) throws SQLException { +// Connection conn = dataSource.getConnection(); +// Statement statement = conn.createStatement(); +// ResultSet result = statement +// .executeQuery("SELECT username, password, enabled, notes FROM users NATURAL JOIN notes WHERE username='%s';".formatted(username)); +// result.next(); +// return new User(result.getString(1), result.getString(2), result.getBoolean(3), +// result.getString(4)); +// } +// } diff --git a/src/main/java/jez/authentication2/services/UserService2.java b/src/main/java/jez/authentication2/services/UserService2.java new file mode 100644 index 0000000000000000000000000000000000000000..7b0c56bd105f5827cf23789a273fcca19ffd8528 --- /dev/null +++ b/src/main/java/jez/authentication2/services/UserService2.java @@ -0,0 +1,21 @@ +package jez.authentication2.services; + +import java.sql.SQLException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import jez.authentication2.entities.User; +import jez.authentication2.exceptions.DuplicateUsernameException; +import jez.authentication2.repositories.UserRepository; + +@Service +public class UserService2 { + // @Autowired + // DataSource dataSource; + @Autowired + UserRepository userRepository; + + public void saveUser(User user) throws DuplicateUsernameException, SQLException { + userRepository.save(user); + } + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 6978d3898e6dc0b0590c064839a481f023812da6..6122db2457c6c53cdcc1e06abefc95e2bd140d6b 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,6 +1,6 @@ -# spring.jpa.properspringties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect +spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect # spring.jpa.hibernate.ddl-auto=none -# spring.jpa.hibernate.show-sql=true +spring.jpa.show-sql=true spring.datasource.url=jdbc:postgresql://localhost:5432/spring spring.datasource.username=postgres diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index 869b83e8be36a8956adf9d1afa49041bd0e7456f..ec46de87084e0789ddd508df5d2c62a2d2c11fd3 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -1,10 +1,10 @@ insert into users (username, password, enabled) values ('user', 'password', true); -insert into notes (username, notes) -values ('user', 'some notes ...1'); -insert into notes (username, notes) -values ('user', 'some notes ...2'); +insert into notes (user_id, notes) +values (1, 'some notes ...1'); +insert into notes (user_id, notes) +values (1, 'some notes ...2'); insert into users (username, password, enabled) values ('admin', 'password', true); @@ -13,4 +13,4 @@ insert into authorities (username, authority) values ('user', 'ROLE_USER'); insert into authorities (username, authority) -values ('admin', 'ROLE_ADMIN'); \ No newline at end of file +values ('admin', 'ROLE_ADMIN'); diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql index 4322dd834262f766d249de54cbf07be535a2e89e..cc02de542fd68844e4767316c8ad2e97c894c97e 100644 --- a/src/main/resources/schema.sql +++ b/src/main/resources/schema.sql @@ -1,6 +1,7 @@ drop table if exists users cascade; create table users( - username varchar(50) not null primary key, + id serial primary key, + username varchar(50) not null unique, password varchar(50) not null, enabled boolean not null ); @@ -16,8 +17,8 @@ drop table if exists notes cascade; create table notes ( id serial not null primary key, notes text, - username varchar(50), - foreign key(username) references users(username) + user_id int not null, + foreign key(user_id) references users(id) ); -- constraint fk_authorities_users