Skip to content
Snippets Groups Projects
Commit 5b7fbeae authored by SajtosKifli's avatar SajtosKifli
Browse files

add bcrypt based password encryption

parent e8903f07
No related branches found
No related tags found
1 merge request!2add bcrypt based password encryption
...@@ -38,8 +38,6 @@ ...@@ -38,8 +38,6 @@
<groupId>jakarta.persistence</groupId> <groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId> <artifactId>jakarta.persistence-api</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
...@@ -55,6 +53,10 @@ ...@@ -55,6 +53,10 @@
<artifactId>maven-model</artifactId> <artifactId>maven-model</artifactId>
<version>${maven-model.version}</version> <version>${maven-model.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
package hu.pazmany; package hu.pazmany;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
public class UserService { public class UserService {
private final UserRepository userRepository; private final UserRepository userRepository;
private final BCryptPasswordEncoder passwordEncoder;
public UserService(UserRepository userRepository) { public UserService(UserRepository userRepository, BCryptPasswordEncoder passwordEncoder) {
this.userRepository = userRepository; this.userRepository = userRepository;
this.passwordEncoder = passwordEncoder;
} }
public String authenticate(String username, String password) { public String authenticate(String username, String password) {
User user = userRepository.findByUsername(username); User user = userRepository.findByUsername(username);
if (user != null && user.getPassword().equals(password)) { if (user != null && passwordEncoder.matches(password, user.getPassword())) {
return user.getFullName(); return user.getFullName();
} }
return "Helytelen felhasználónév vagy jelszó!"; return "Helytelen felhasználónév vagy jelszó!";
...@@ -23,7 +25,7 @@ public class UserService { ...@@ -23,7 +25,7 @@ public class UserService {
User user = new User(); User user = new User();
user.setFullName(fullName); user.setFullName(fullName);
user.setUsername(username); user.setUsername(username);
user.setPassword(password); user.setPassword(passwordEncoder.encode(password));
userRepository.save(user); userRepository.save(user);
} }
} }
package hu.pazmany.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@Configuration
public class SecurityConfig {
@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
...@@ -23,16 +23,23 @@ public class DocumentRepositoryTests { ...@@ -23,16 +23,23 @@ public class DocumentRepositoryTests {
@Test @Test
public void testFindAllByUser() { public void testFindAllByUser() {
// Create a new User
User user = new User(); User user = new User();
user.setUsername("testUser"); user.setUsername("testUser");
user.setPassword("testPassword"); user.setPassword("testPassword");
// Save the User
userRepository.save(user); userRepository.save(user);
// Create a new Subject
Subject subject = new Subject(); Subject subject = new Subject();
subject.setSubjectId("testId"); subject.setSubjectId("testId");
subject.setName("testName"); subject.setName("testName");
// Save the Subject
subjectRepository.save(subject); subjectRepository.save(subject);
// Create and save Documents
int n = 4; int n = 4;
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
Document document = new Document(); Document document = new Document();
...@@ -43,9 +50,13 @@ public class DocumentRepositoryTests { ...@@ -43,9 +50,13 @@ public class DocumentRepositoryTests {
documentRepository.save(document); documentRepository.save(document);
} }
// Retrieve all Documents by User
List<Document> documents = documentRepository.findAllByUser(user); List<Document> documents = documentRepository.findAllByUser(user);
// Assert that the retrieved Documents has the correct size
assertThat(documents).hasSize(n); assertThat(documents).hasSize(n);
// Assert that the retrieved Documents are all from the same User
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
assertThat(documents.get(i).getUser().getUsername()).isEqualTo(user.getUsername()); assertThat(documents.get(i).getUser().getUsername()).isEqualTo(user.getUsername());
} }
...@@ -53,16 +64,23 @@ public class DocumentRepositoryTests { ...@@ -53,16 +64,23 @@ public class DocumentRepositoryTests {
@Test @Test
public void testFindAllBySubject() { public void testFindAllBySubject() {
// Create a new Subject
Subject subject = new Subject(); Subject subject = new Subject();
subject.setSubjectId("testId"); subject.setSubjectId("testId");
subject.setName("testName"); subject.setName("testName");
// Save the Subject
subjectRepository.save(subject); subjectRepository.save(subject);
// Create a new User
User user = new User(); User user = new User();
user.setUsername("testUser"); user.setUsername("testUser");
user.setPassword("testPassword"); user.setPassword("testPassword");
// Save the User
userRepository.save(user); userRepository.save(user);
// Create and save Documents
int n = 4; int n = 4;
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
Document document = new Document(); Document document = new Document();
...@@ -73,9 +91,13 @@ public class DocumentRepositoryTests { ...@@ -73,9 +91,13 @@ public class DocumentRepositoryTests {
documentRepository.save(document); documentRepository.save(document);
} }
// Retrieve all Documents by Subject
List<Document> documents = documentRepository.findAllBySubject(subject); List<Document> documents = documentRepository.findAllBySubject(subject);
// Assert that the retrieved Documents has the correct size
assertThat(documents).hasSize(n); assertThat(documents).hasSize(n);
// Assert that the retrieved Documents are all from the same Subject
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
assertThat(documents.get(i).getSubject().getSubjectId()).isEqualTo(subject.getSubjectId()); assertThat(documents.get(i).getSubject().getSubjectId()).isEqualTo(subject.getSubjectId());
} }
...@@ -83,25 +105,36 @@ public class DocumentRepositoryTests { ...@@ -83,25 +105,36 @@ public class DocumentRepositoryTests {
@Test @Test
public void testFindById() { public void testFindById() {
// Create a new Subject
Subject subject = new Subject(); Subject subject = new Subject();
subject.setSubjectId("testId"); subject.setSubjectId("testId");
subject.setName("testName"); subject.setName("testName");
// Save the Subject
subjectRepository.save(subject); subjectRepository.save(subject);
// Create a new User
User user = new User(); User user = new User();
user.setUsername("testUser"); user.setUsername("testUser");
user.setPassword("testPassword"); user.setPassword("testPassword");
// Save the User
userRepository.save(user); userRepository.save(user);
// Create the Document
Document document = new Document(); Document document = new Document();
document.setUser(user); document.setUser(user);
document.setSubject(subject); document.setSubject(subject);
document.setTitle("testTitle"); document.setTitle("testTitle");
document.setFilepath("testFilepath"); document.setFilepath("testFilepath");
// Save the Document
documentRepository.save(document); documentRepository.save(document);
// Retrieve the Document
Document found = documentRepository.findById(document.getId()).get(); Document found = documentRepository.findById(document.getId()).get();
// Assert that the retrieved Document is the same as the one we saved
assertThat(found.getTitle()).isEqualTo(document.getTitle()); assertThat(found.getTitle()).isEqualTo(document.getTitle());
} }
} }
\ No newline at end of file
...@@ -17,24 +17,33 @@ public class SubjectRepositoryTests { ...@@ -17,24 +17,33 @@ public class SubjectRepositoryTests {
@Test @Test
public void testFindBySubjectId() { public void testFindBySubjectId() {
// Create a new Subject
Subject subject = new Subject(); Subject subject = new Subject();
subject.setSubjectId("testId"); subject.setSubjectId("testId");
subject.setName("testName"); subject.setName("testName");
// Save the Subject
subjectRepository.save(subject); subjectRepository.save(subject);
// Retrieve the Subject
Subject found = subjectRepository.findBySubjectId(subject.getSubjectId()); Subject found = subjectRepository.findBySubjectId(subject.getSubjectId());
// Assert that the retrieved Subject is the same as the one we saved
// Ids have to be compared, as the objects differ, even if their data does not // Ids have to be compared, as the objects differ, even if their data does not
assertThat(found.getSubjectId()).isEqualTo(subject.getSubjectId()); assertThat(found.getSubjectId()).isEqualTo(subject.getSubjectId());
} }
@Test @Test
public void testFindAll() { public void testFindAll() {
// Create a new Subject
Subject subject = new Subject(); Subject subject = new Subject();
subject.setSubjectId("testId"); subject.setSubjectId("testId");
subject.setName("testName"); subject.setName("testName");
// Save the Subject
subjectRepository.save(subject); subjectRepository.save(subject);
// Create and save more Subjects
int n = 4; int n = 4;
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
Subject tempSubject = new Subject(); Subject tempSubject = new Subject();
...@@ -43,10 +52,13 @@ public class SubjectRepositoryTests { ...@@ -43,10 +52,13 @@ public class SubjectRepositoryTests {
subjectRepository.save(tempSubject); subjectRepository.save(tempSubject);
} }
// Retrieve all Subjects
List<Subject> subjects = subjectRepository.findAll(); List<Subject> subjects = subjectRepository.findAll();
// Assert that the retrieved Subjects has the correct size
assertThat(subjects).hasSize(n+1); assertThat(subjects).hasSize(n+1);
// Assert that the retrieved Subjects contain the one we saved
assertThat(subjects.get(0).getSubjectId()).isEqualTo(subject.getSubjectId()); assertThat(subjects.get(0).getSubjectId()).isEqualTo(subject.getSubjectId());
} }
} }
\ No newline at end of file
package hu.pazmany;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.test.annotation.DirtiesContext;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
public class UserServiceTests {
@Autowired
private UserService userService;
@Autowired
private UserRepository userRepository;
@Autowired
private BCryptPasswordEncoder passwordEncoder;
@Test
public void testRegister() {
// Register a new user
userService.register("Test User", "testUser", "testPassword");
// Retrieve the user
User user = userRepository.findByUsername("testUser");
// Assert that the user was saved and the password was encrypted
assertThat(user).isNotNull();
assertThat(user.getUsername()).isEqualTo("testUser");
assertThat(passwordEncoder.matches("testPassword", user.getPassword())).isTrue();
}
@Test
public void testAuthenticate() {
// Register a new user
userService.register("Test User", "testUser", "testPassword");
// Authenticate the user
String fullName = userService.authenticate("testUser", "testPassword");
// Assert that the user was authenticated successfully
assertThat(fullName).isEqualTo("Test User");
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment