register with password
This commit is contained in:
parent
7c01a29e3f
commit
8d16d2d456
19
pom.xml
19
pom.xml
@ -52,25 +52,6 @@
|
|||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-security</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.security</groupId>
|
|
||||||
<artifactId>spring-security-test</artifactId>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -1,89 +1,92 @@
|
|||||||
package com.example.nto.controller;
|
package com.example.nto.controller;
|
||||||
|
|
||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.entity.Employee;
|
||||||
import com.example.nto.entity.User;
|
|
||||||
import com.example.nto.service.EmployeeService;
|
import com.example.nto.service.EmployeeService;
|
||||||
import com.example.nto.service.UserService;
|
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder; // Импортируйте PasswordEncoder
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
//я поменял на BAD_REQUEST 06.12.24 23:00
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api")
|
@RequestMapping("/api")//база
|
||||||
public class EmployeeController {
|
public class EmployeeController {
|
||||||
private final EmployeeService employeeService;
|
private final EmployeeService employeeService;
|
||||||
private final UserService userService;
|
|
||||||
private final PasswordEncoder passwordEncoder; // Добавленное поле
|
|
||||||
|
|
||||||
public EmployeeController(EmployeeService employeeService, UserService userService, PasswordEncoder passwordEncoder) {
|
public EmployeeController(EmployeeService employeeService) {
|
||||||
|
|
||||||
this.employeeService = employeeService;
|
this.employeeService = employeeService;
|
||||||
this.userService = userService;
|
|
||||||
this.passwordEncoder = passwordEncoder; // Инициализация поля
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@PreAuthorize("hasRole('ADMIN')")
|
|
||||||
@GetMapping("/admin/employees")
|
|
||||||
public ResponseEntity<List<Employee>> getAllEmployees() {
|
|
||||||
List<Employee> employees = employeeService.findAll();
|
|
||||||
return ResponseEntity.ok(employees);
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping("/register")
|
@GetMapping("/{login}/auth")//auth
|
||||||
public ResponseEntity<?> registerUser(@RequestBody User user) {
|
|
||||||
userService.saveUser(user);
|
|
||||||
return ResponseEntity.status(HttpStatus.CREATED).body("User registered successfully");
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/{login}/auth") // auth
|
|
||||||
public ResponseEntity<?> authenticate(@PathVariable String login) {
|
public ResponseEntity<?> authenticate(@PathVariable String login) {
|
||||||
Optional<Employee> employee = employeeService.findByLogin(login);
|
Optional<Employee> employee = employeeService.findByLogin(login);
|
||||||
if (employee.isPresent()) {
|
if (employee.isPresent()) {
|
||||||
return ResponseEntity.ok("Valid login");
|
return ResponseEntity.ok("Valid login");
|
||||||
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid login");
|
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid login");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{login}/info")
|
@GetMapping("/{login}/info")//info
|
||||||
public ResponseEntity<?> getInfo(@PathVariable String login) {
|
public ResponseEntity<?> getInfo(@PathVariable String login) {
|
||||||
Optional<Employee> employee = employeeService.findByLogin(login);
|
Optional<Employee> employee = employeeService.findByLogin(login);
|
||||||
if (employee.isPresent()) {
|
if (employee.isPresent()) {
|
||||||
return ResponseEntity.ok(employee.get());
|
return ResponseEntity.ok(employee.get());
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid login");
|
||||||
|
|
||||||
}
|
}
|
||||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid login");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@PatchMapping("/{login}/open") // open
|
|
||||||
|
|
||||||
|
@PatchMapping("/{login}/open")//open
|
||||||
public ResponseEntity<?> openDoor(@PathVariable String login, @RequestBody Map<String, Long> payload) {
|
public ResponseEntity<?> openDoor(@PathVariable String login, @RequestBody Map<String, Long> payload) {
|
||||||
Long code = payload.get("value");
|
Long code = payload.get("value");
|
||||||
if (code == null) {
|
if (code == null) {
|
||||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid payload");
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid payload");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Optional<Employee> employee = employeeService.findByLogin(login);
|
Optional<Employee> employee = employeeService.findByLogin(login);
|
||||||
if (employee.isEmpty()) {
|
if (employee.isEmpty()) {
|
||||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid login");
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid login");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (employeeService.validateCode(login, code)) {
|
if (employeeService.validateCode(login, code)) {
|
||||||
return ResponseEntity.ok("Door opened");
|
return ResponseEntity.ok("Door opened");
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid code");
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid code");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@PostMapping("/auth") // auth
|
||||||
|
public ResponseEntity<?> authenticate(@RequestBody Map<String, String> payload) {
|
||||||
|
String login = payload.get("login");
|
||||||
|
String password = payload.get("password");
|
||||||
|
|
||||||
@GetMapping("/auth")
|
Optional<Employee> employee = employeeService.findByLogin(login);
|
||||||
public ResponseEntity<?> authenticate(@RequestParam String username, @RequestParam String password) {
|
if (employee.isPresent() && employee.get().getPassword().equals(password)) {
|
||||||
Optional<User> optionalUser = userService.findByUsername(username); // Исправление
|
|
||||||
if (optionalUser.isPresent() && passwordEncoder.matches(password, optionalUser.get().getPassword())) { // Исправление
|
|
||||||
return ResponseEntity.ok("Valid login");
|
return ResponseEntity.ok("Valid login");
|
||||||
|
} else {
|
||||||
|
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid login or password");
|
||||||
}
|
}
|
||||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid login");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// made by truettwo and maks ))
|
@ -23,5 +23,7 @@ public class Employee {
|
|||||||
@Column(name = "last_visit")
|
@Column(name = "last_visit")
|
||||||
private LocalDateTime lastVisit;
|
private LocalDateTime lastVisit;
|
||||||
|
|
||||||
// Геттеры и сеттеры для login, name, и других полей
|
private String password; // Добавлено поле для пароля
|
||||||
|
|
||||||
|
// Геттеры и сеттеры для login, name, password и других полей
|
||||||
}
|
}
|
@ -9,13 +9,12 @@ CREATE TABLE IF NOT EXISTS employee (
|
|||||||
);
|
);
|
||||||
|
|
||||||
-- Вставка данных в таблицу employee
|
-- Вставка данных в таблицу employee
|
||||||
INSERT INTO employee (id, login, name, photo, position, last_visit)
|
INSERT INTO employee (id, login, name, photo, position, last_visit, password)
|
||||||
VALUES
|
VALUES
|
||||||
(1, 'pivanov', 'Иванов Петр Федорович', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Разработчик', '2024-02-12T08:30'),
|
(1, 'pivanov', 'Иванов Петр Федорович', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Разработчик', '2024-02-12T08:30', 'password123'),
|
||||||
(2, 'ipetrov', 'Петров Иван Константинович', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Аналитик', '2024-02-13T08:35'),
|
(2, 'ipetrov', 'Петров Иван Константинович', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Аналитик', '2024-02-13T08:35', 'password456'),
|
||||||
(3, 'asemenov', 'Семенов Анатолий Анатольевич', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Разработчик', '2024-02-13T08:31'),
|
(3, 'asemenov', 'Семенов Анатолий Анатольевич', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Разработчик', '2024-02-13T08:31', 'password789'),
|
||||||
(4, 'afedorov', 'Федоров Александр Сергеевич', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Тестировщик', '2024-02-12T08:36');
|
(4, 'afedorov', 'Федоров Александр Сергеевич', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Тестировщик', '2024-02-12T08:36', 'password000');
|
||||||
|
|
||||||
-- Создание таблицы code
|
-- Создание таблицы code
|
||||||
CREATE TABLE IF NOT EXISTS code (
|
CREATE TABLE IF NOT EXISTS code (
|
||||||
value BIGINT
|
value BIGINT
|
||||||
|
Loading…
x
Reference in New Issue
Block a user