checkpoint1 2.0
This commit is contained in:
parent
712dad45f7
commit
f5f797a9cf
@ -2,8 +2,9 @@ package com.example.nto;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
|
||||
|
||||
@SpringBootApplication
|
||||
@SpringBootApplication(exclude = { SecurityAutoConfiguration.class })
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(App.class, args);
|
||||
|
||||
@ -1,33 +1,35 @@
|
||||
package com.example.nto.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
public class WebSecurityConfig {
|
||||
|
||||
@Bean
|
||||
public PasswordEncoder passwordEncoder() {
|
||||
return new BCryptPasswordEncoder();
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http
|
||||
.csrf(csrf -> csrf.disable())
|
||||
.authorizeHttpRequests((authorize) -> authorize
|
||||
.requestMatchers("/h2-console").permitAll()
|
||||
.requestMatchers("/register").permitAll()
|
||||
.anyRequest().authenticated()
|
||||
);
|
||||
return http.build();
|
||||
}
|
||||
}
|
||||
//package com.example.nto.controller.config;
|
||||
//
|
||||
//import org.springframework.context.annotation.Bean;
|
||||
//import org.springframework.context.annotation.Configuration;
|
||||
//import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
//import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
//import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
//import org.springframework.security.web.SecurityFilterChain;
|
||||
//
|
||||
//@Configuration
|
||||
//@EnableWebSecurity
|
||||
//public class WebSecurityConfig {
|
||||
// @Bean
|
||||
// public PasswordEncoder passwordEncoder() {
|
||||
// return new BCryptPasswordEncoder();
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
// http
|
||||
// .csrf(csrf -> csrf.disable())
|
||||
// .authorizeHttpRequests((authorize) -> authorize
|
||||
// .requestMatchers("/h2-console").permitAll()
|
||||
// .requestMatchers("/index.html").permitAll()
|
||||
// .requestMatchers("/register").permitAll()
|
||||
// .anyRequest().authenticated()
|
||||
// );
|
||||
//
|
||||
// return http.build();
|
||||
// }
|
||||
//
|
||||
//
|
||||
//
|
||||
//}
|
||||
|
||||
@ -3,6 +3,7 @@ package com.example.nto.controller;
|
||||
|
||||
import com.example.nto.controller.dto.EmployeeDto;
|
||||
import com.example.nto.controller.dto.EmployeeRegisterDto;
|
||||
import com.example.nto.entity.Employee;
|
||||
import com.example.nto.service.EmployeeService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@ -15,20 +16,17 @@ public class EmployeeController {
|
||||
|
||||
private final EmployeeService employeeService;
|
||||
|
||||
@GetMapping("/{username}/auth")
|
||||
@ResponseStatus(code = HttpStatus.OK)
|
||||
public void login(@PathVariable String username) {
|
||||
employeeService.auth(username);
|
||||
}
|
||||
|
||||
@GetMapping("/{username}/info")
|
||||
@ResponseStatus(code = HttpStatus.OK)
|
||||
public EmployeeDto getByCode(@PathVariable String username) {
|
||||
public EmployeeDto getByUsername(@PathVariable String username) {
|
||||
return employeeService.getByUsername(username);
|
||||
}
|
||||
|
||||
@GetMapping("/register")
|
||||
@PostMapping("/register")
|
||||
@ResponseStatus(code = HttpStatus.CREATED)
|
||||
public void register(EmployeeRegisterDto employeeRegisterDto) {employeeService.register(employeeRegisterDto);}
|
||||
public Employee registerEmployee(EmployeeRegisterDto employeeRegisterDto) {
|
||||
return employeeService.register(employeeRegisterDto);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -16,7 +16,7 @@ import java.util.TreeMap;
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class EmployeeDto {
|
||||
private String name;
|
||||
private String username;
|
||||
private String photoUrl;
|
||||
private Map<LocalDate, PlaceDto> booking;
|
||||
|
||||
@ -26,6 +26,6 @@ public class EmployeeDto {
|
||||
dtoTreeMap.put(booking.getDate(), PlaceDto.toDto(booking.getPlace()));
|
||||
}
|
||||
|
||||
return new EmployeeDto(employee.getName(), employee.getPhotoUrl(), dtoTreeMap);
|
||||
return new EmployeeDto(employee.getUsername(), employee.getPhotoUrl(), dtoTreeMap);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,16 +1,21 @@
|
||||
package com.example.nto.controller.dto;
|
||||
|
||||
import com.example.nto.entity.Booking;
|
||||
import com.example.nto.entity.Employee;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class EmployeeRegisterDto {
|
||||
private String name;
|
||||
private String username;
|
||||
private String password;
|
||||
}
|
||||
|
||||
@ -17,15 +17,12 @@ import java.util.List;
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Table(name = "employee")
|
||||
public class Employee {
|
||||
public class Employee implements UserDetails{
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
@Column(name = "name")
|
||||
private String name;
|
||||
|
||||
@Column(name = "username")
|
||||
private String username;
|
||||
|
||||
@ -38,4 +35,8 @@ public class Employee {
|
||||
@OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
||||
private List<Booking> bookingList;
|
||||
|
||||
@Override
|
||||
public Collection<? extends GrantedAuthority> getAuthorities() {
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +0,0 @@
|
||||
package com.example.nto.exception;
|
||||
|
||||
public class PasswordNotCorrectException extends RuntimeException {
|
||||
public PasswordNotCorrectException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@ -28,11 +28,6 @@ public class GlobalExceptionHandler {
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.CONFLICT);
|
||||
}
|
||||
|
||||
@ExceptionHandler(PasswordNotCorrectException.class)
|
||||
public ResponseEntity<String> handlePasswordNotCorrectException(PasswordNotCorrectException e) {
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.CONFLICT);
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<String> handleGenericException(Exception e) {
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
|
||||
|
||||
@ -2,11 +2,10 @@ package com.example.nto.service;
|
||||
|
||||
import com.example.nto.controller.dto.EmployeeDto;
|
||||
import com.example.nto.controller.dto.EmployeeRegisterDto;
|
||||
import com.example.nto.entity.Employee;
|
||||
|
||||
public interface EmployeeService {
|
||||
EmployeeDto getByUsername(String username);
|
||||
|
||||
void auth(String username);
|
||||
|
||||
void register(EmployeeRegisterDto employeeRegisterDto);
|
||||
Employee register(EmployeeRegisterDto employeeRegisterDto);
|
||||
}
|
||||
|
||||
@ -30,15 +30,13 @@ public class BookingServiceImpl implements BookingService {
|
||||
private final BookingRepository bookingRepository;
|
||||
private final EmployeeRepository employeeRepository;
|
||||
private final PlaceRepository placeRepository;
|
||||
private final EmployeeService employeeService;
|
||||
|
||||
@Value("${booking.days-ahead}")
|
||||
private int daysAhead;
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public Map<LocalDate, List<PlaceDto>> getFreePlace(String username) {
|
||||
employeeService.auth(username);
|
||||
public Map<LocalDate, List<PlaceDto>> getFreePlace(String code) {
|
||||
|
||||
List<Place> allPlaces = placeRepository.findAll();
|
||||
|
||||
|
||||
@ -5,7 +5,6 @@ import com.example.nto.controller.dto.EmployeeRegisterDto;
|
||||
import com.example.nto.entity.Employee;
|
||||
import com.example.nto.exception.EmployeeAlreadyExistsException;
|
||||
import com.example.nto.exception.EmployeeNotFoundException;
|
||||
import com.example.nto.exception.PasswordNotCorrectException;
|
||||
import com.example.nto.repository.EmployeeRepository;
|
||||
import com.example.nto.service.EmployeeService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@ -18,7 +17,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
public class EmployeeServiceImpl implements EmployeeService {
|
||||
|
||||
private final EmployeeRepository employeeRepository;
|
||||
private PasswordEncoder passwordEncoder;
|
||||
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
@ -28,30 +27,17 @@ public class EmployeeServiceImpl implements EmployeeService {
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public void auth(String username) {
|
||||
if (employeeRepository.findByUsername(username).isEmpty()) {
|
||||
throw new EmployeeNotFoundException("Employee with " + username + " username not found!");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(EmployeeRegisterDto employeeRegisterDto) {
|
||||
public Employee register(EmployeeRegisterDto employeeRegisterDto) {
|
||||
if (employeeRepository.findByUsername(employeeRegisterDto.getUsername()).isPresent()) {
|
||||
throw new EmployeeAlreadyExistsException("Employee with " + employeeRegisterDto.getUsername() + " username already exist");
|
||||
};
|
||||
throw new EmployeeAlreadyExistsException("Employee with " + employeeRegisterDto.getUsername() + " username");
|
||||
}
|
||||
|
||||
Employee employee = new Employee();
|
||||
|
||||
if (passwordEncoder.encode(employee.getPassword()).length() < 8) {
|
||||
throw new PasswordNotCorrectException("The password is too short!!!");
|
||||
}
|
||||
|
||||
employee.setName(employeeRegisterDto.getName());
|
||||
employee.setUsername(employeeRegisterDto.getUsername());
|
||||
employee.setPassword(passwordEncoder.encode(employeeRegisterDto.getPassword()));
|
||||
employee.setPassword(employeeRegisterDto.getPassword());
|
||||
|
||||
employeeRepository.save(employee);
|
||||
return employeeRepository.save(employee);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
|
||||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd
|
||||
http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
|
||||
|
||||
<changeSet id="2026-25-02--0004-authority" author="ggaydaenko">
|
||||
<preConditions onFail="MARK_RAN">
|
||||
<not>
|
||||
<tableExists tableName="authority"/>
|
||||
</not>
|
||||
</preConditions>
|
||||
|
||||
<createTable tableName="authority">
|
||||
<column name="id" type="BIGINT" autoIncrement="true">
|
||||
<constraints primaryKey="true" nullable="false"/>
|
||||
</column>
|
||||
|
||||
<column name="authority" type="VARCHAR(100)">
|
||||
<constraints nullable="false" unique="true"/>
|
||||
</column>
|
||||
</createTable>
|
||||
</changeSet>
|
||||
|
||||
</databaseChangeLog>
|
||||
@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
|
||||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd
|
||||
http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
|
||||
|
||||
<changeSet id="2026-25-02--0004-authority-data" author="ggaydaenko">
|
||||
<loadData tableName="authority" file="db.changelog/data/csv/2026-25-02--0004-authority-data.csv"
|
||||
separator=";"
|
||||
quotchar='"'
|
||||
encoding="UTF-8"/>
|
||||
</changeSet>
|
||||
</databaseChangeLog>
|
||||
@ -0,0 +1,3 @@
|
||||
authority
|
||||
ROLE_USER
|
||||
ROLE_ROOM
|
||||
|
Loading…
x
Reference in New Issue
Block a user