diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 1bcd102..6461633 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -5,18 +5,27 @@
-
+
+
+
+
+
-
-
-
-
+
+
+
+
+
@@ -29,18 +38,19 @@
- {
+ "keyToString": {
+ "Application.App.executor": "Run",
+ "RunOnceActivity.ShowReadmeOnStart": "true",
+ "RunOnceActivity.git.unshallow": "true",
+ "git-widget-placeholder": "main",
+ "kotlin-language-version-configured": "true",
+ "last_opened_file_path": "C:/Users/User/Desktop/NTO-2024-Backend",
+ "settings.editor.selected.configurable": "MavenSettings"
}
-}]]>
+}
-
+
@@ -103,4 +113,26 @@
+
+
+
+
+ file://$PROJECT_DIR$/src/main/java/com/example/nto/entity/Code.java
+ 20
+
+
+
+
+ file://$PROJECT_DIR$/src/main/java/com/example/nto/controller/EmployeeController.java
+ 29
+
+
+
+ file://$PROJECT_DIR$/src/main/java/com/example/nto/controller/EmployeeController.java
+ 12
+
+
+
+
+
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 88282ee..7310309 100644
--- a/pom.xml
+++ b/pom.xml
@@ -25,18 +25,30 @@
org.projectlombok
lombok
+
+ org.postgresql
+ postgresql
+
+
+ org.thymeleaf.extras
+ thymeleaf-extras-springsecurity6
+
org.springframework.boot
spring-boot-starter-web
- com.h2database
- h2
+ org.springframework.boot
+ spring-boot-starter-security
org.springframework.boot
spring-boot-starter-data-jpa
+
+ org.springframework.security
+ spring-security-test
+
org.springdoc
springdoc-openapi-ui
diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java
index b9d8f74..3562a9c 100644
--- a/src/main/java/com/example/nto/controller/EmployeeController.java
+++ b/src/main/java/com/example/nto/controller/EmployeeController.java
@@ -5,13 +5,11 @@ import com.example.nto.repository.CodeRepository;
import com.example.nto.service.EmployeeService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PatchMapping;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.*;
import java.util.Optional;
+@RestController
public class EmployeeController {
private final EmployeeService employeeService;
private final CodeRepository codeRepository;
diff --git a/src/main/java/com/example/nto/entity/Employee.java b/src/main/java/com/example/nto/entity/Employee.java
index 2d230ad..fd4102d 100644
--- a/src/main/java/com/example/nto/entity/Employee.java
+++ b/src/main/java/com/example/nto/entity/Employee.java
@@ -4,12 +4,14 @@ import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
+import org.springframework.security.core.GrantedAuthority;
+import org.springframework.security.core.userdetails.UserDetails;
-import javax.persistence.Entity;
-import javax.persistence.GeneratedValue;
-import javax.persistence.GenerationType;
-import javax.persistence.Id;
+import javax.persistence.*;
import java.time.LocalDateTime;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
@Entity
@Data
@@ -23,10 +25,16 @@ public class Employee {
private String login;
private String name;
+ private String password;
+ private String role;
private String photo;
private String position;
private LocalDateTime lastVisit;
+
+// @OneToMany(mappedBy = "employee")
+// private List adminsEmployees = new ArrayList<>();
+
public long getId() {
return id;
}
@@ -74,4 +82,28 @@ public class Employee {
public void setLastVisit(LocalDateTime lastVisit) {
this.lastVisit = lastVisit;
}
+
+ public String getPassword() {
+ return password;
+ }
+
+ public void setPassword(String password) {
+ this.password = password;
+ }
+
+ public String getRole() {
+ return role;
+ }
+
+ public void setRole(String role) {
+ this.role = role;
+ }
+
+// public List getAdminsEmployees() {
+// return adminsEmployees;
+// }
+//
+// public void setAdminsEmployees(List adminsEmployees) {
+// this.adminsEmployees = adminsEmployees;
+// }
}
diff --git a/src/main/java/com/example/nto/websecurity/CustomUserDetails.java b/src/main/java/com/example/nto/websecurity/CustomUserDetails.java
new file mode 100644
index 0000000..42e81a8
--- /dev/null
+++ b/src/main/java/com/example/nto/websecurity/CustomUserDetails.java
@@ -0,0 +1,53 @@
+package com.example.nto.websecurity;
+
+import com.example.nto.entity.Employee;
+import org.springframework.security.core.GrantedAuthority;
+import org.springframework.security.core.userdetails.UserDetails;
+
+import java.util.Collection;
+import java.util.List;
+
+
+public class CustomUserDetails implements UserDetails {
+
+ private Employee employee;
+
+ public CustomUserDetails(Employee employee) {
+ this.employee = employee;
+ }
+
+ @Override
+ public Collection extends GrantedAuthority> getAuthorities() {
+ return List.of();
+ }
+
+ @Override
+ public String getPassword() {
+ return employee.getPassword();
+ }
+
+ @Override
+ public String getUsername() {
+ return employee.getLogin();
+ }
+
+ @Override
+ public boolean isAccountNonExpired() {
+ return true;
+ }
+
+ @Override
+ public boolean isAccountNonLocked() {
+ return true;
+ }
+
+ @Override
+ public boolean isCredentialsNonExpired() {
+ return true;
+ }
+
+ @Override
+ public boolean isEnabled() {
+ return true;
+ }
+}
diff --git a/src/main/java/com/example/nto/websecurity/UserDetailsServiceImpl.java b/src/main/java/com/example/nto/websecurity/UserDetailsServiceImpl.java
new file mode 100644
index 0000000..f8b3bb6
--- /dev/null
+++ b/src/main/java/com/example/nto/websecurity/UserDetailsServiceImpl.java
@@ -0,0 +1,21 @@
+package com.example.nto.websecurity;
+
+import com.example.nto.entity.Employee;
+import com.example.nto.repository.EmployeeRepository;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.core.userdetails.UserDetails;
+import org.springframework.security.core.userdetails.UserDetailsService;
+import org.springframework.security.core.userdetails.UsernameNotFoundException;
+
+public class UserDetailsServiceImpl implements UserDetailsService {
+
+
+ @Autowired
+ private EmployeeRepository repository;
+
+ @Override
+ public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
+ Employee employee = repository.findByLogin(s).orElseThrow(() -> new UsernameNotFoundException(s));
+ return new CustomUserDetails(employee);
+ }
+}
diff --git a/src/main/java/com/example/nto/websecurity/WebSecurityConfig.java b/src/main/java/com/example/nto/websecurity/WebSecurityConfig.java
new file mode 100644
index 0000000..fa58cba
--- /dev/null
+++ b/src/main/java/com/example/nto/websecurity/WebSecurityConfig.java
@@ -0,0 +1,48 @@
+package com.example.nto.websecurity;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
+import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
+import org.springframework.security.config.annotation.web.builders.HttpSecurity;
+import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
+import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
+import org.springframework.security.core.userdetails.UserDetailsService;
+import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
+
+@Configuration
+@EnableWebSecurity
+public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
+ @Bean
+ public UserDetailsService userDetailsService() {
+ return new UserDetailsServiceImpl();
+ }
+
+ @Bean
+ public BCryptPasswordEncoder passwordEncoder() {
+ return new BCryptPasswordEncoder();
+ }
+ @Bean
+ public DaoAuthenticationProvider authenticationProvider() {
+ DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
+ authProvider.setUserDetailsService(userDetailsService());
+ authProvider.setPasswordEncoder(passwordEncoder());
+
+ return authProvider;
+ }
+
+ @Override
+ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
+ auth.authenticationProvider(authenticationProvider());
+ }
+
+ @Override
+ protected void configure(HttpSecurity http) throws Exception {
+ http.authorizeRequests()
+ .anyRequest().authenticated()
+ .and()
+ .formLogin().permitAll()
+ .and()
+ .logout().permitAll();
+ }
+}
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index c6bfd72..e2d20e2 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -1,12 +1,11 @@
spring:
datasource:
- url: jdbc:h2:mem:testdb
+ url: jdbc:postgresql://localhost:5432/postgres
+ username: postgres
+ password: MobileDev
+ driver-class-name: org.postgresql.Driver
- h2:
- console:
- #enabled: false
- enabled: true
jpa:
#generate-ddl: false