From bc6ca99907659999be3d74f0503a937b8da6dfef Mon Sep 17 00:00:00 2001 From: Konstantin Date: Wed, 19 Feb 2025 16:35:50 +0300 Subject: [PATCH] the cringe --- .../example/nto/config/SecurityConfig.java | 7 ++++++ .../nto/controller/AuthController.java | 21 ++++++++++++++++ .../nto/controller/EmployeeController.java | 9 ++----- .../nto/model/dto/AuthCredentials.java | 11 ++++++++ .../com/example/nto/service/AuthService.java | 7 ++++++ .../service/EmployeeCredentialsService.java | 7 ++++++ .../example/nto/service/EmployeeService.java | 3 --- .../nto/service/impl/AuthServiceImpl.java | 21 ++++++++++++++++ .../impl/EmployeeCredentialsServiceImpl.java | 25 +++++++++++++++++++ .../nto/service/impl/EmployeeServiceImpl.java | 16 ++---------- 10 files changed, 103 insertions(+), 24 deletions(-) create mode 100644 src/main/java/com/example/nto/controller/AuthController.java create mode 100644 src/main/java/com/example/nto/model/dto/AuthCredentials.java create mode 100644 src/main/java/com/example/nto/service/AuthService.java create mode 100644 src/main/java/com/example/nto/service/EmployeeCredentialsService.java create mode 100644 src/main/java/com/example/nto/service/impl/AuthServiceImpl.java create mode 100644 src/main/java/com/example/nto/service/impl/EmployeeCredentialsServiceImpl.java diff --git a/src/main/java/com/example/nto/config/SecurityConfig.java b/src/main/java/com/example/nto/config/SecurityConfig.java index 6356ffa..ff17a81 100644 --- a/src/main/java/com/example/nto/config/SecurityConfig.java +++ b/src/main/java/com/example/nto/config/SecurityConfig.java @@ -3,8 +3,10 @@ package com.example.nto.config; import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.AuthenticationProvider; import org.springframework.security.authentication.dao.DaoAuthenticationProvider; +import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration; 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.configurers.AbstractAuthenticationFilterConfigurer; @@ -39,6 +41,11 @@ public class SecurityConfig { return provider; } + @Bean + public AuthenticationManager authenticationManager(final AuthenticationConfiguration config) throws Exception { + return config.getAuthenticationManager(); + } + @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); diff --git a/src/main/java/com/example/nto/controller/AuthController.java b/src/main/java/com/example/nto/controller/AuthController.java new file mode 100644 index 0000000..9c69481 --- /dev/null +++ b/src/main/java/com/example/nto/controller/AuthController.java @@ -0,0 +1,21 @@ +package com.example.nto.controller; + +import com.example.nto.model.dto.AuthCredentials; +import com.example.nto.service.AuthService; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/api") +public class AuthController { + private final AuthService authService; + + @PostMapping("/auth") + public void auth(@RequestParam final AuthCredentials authCredentials) { + authService.auth(authCredentials); + } +} diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java index fa39f01..1884aaf 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -14,18 +14,13 @@ public class EmployeeController { private final EmployeeService employeeService; - @GetMapping("/auth") - public void auth(@RequestParam final String login, @RequestParam final String password) { - employeeService.auth(login, password); - } - - @PreAuthorize("hasAuthority('User', 'Admin')") +// @PreAuthorize("hasAuthority('User', 'Admin')") @GetMapping("/info") public Employee info(@RequestParam final String login) { return employeeService.getEmployee(login); } - @PreAuthorize("hasAuthority('User', 'Admin')") +// @PreAuthorize("hasAuthority('User', 'Admin')") @PatchMapping("/open") public void open(@RequestParam final String login, @RequestBody final Code code) { employeeService.updateVisit(login, code.getValue()); diff --git a/src/main/java/com/example/nto/model/dto/AuthCredentials.java b/src/main/java/com/example/nto/model/dto/AuthCredentials.java new file mode 100644 index 0000000..a42a71c --- /dev/null +++ b/src/main/java/com/example/nto/model/dto/AuthCredentials.java @@ -0,0 +1,11 @@ +package com.example.nto.model.dto; + +import lombok.Builder; +import lombok.Data; + +@Builder +@Data +public class AuthCredentials { + private String login; + private String password; +} diff --git a/src/main/java/com/example/nto/service/AuthService.java b/src/main/java/com/example/nto/service/AuthService.java new file mode 100644 index 0000000..5306b87 --- /dev/null +++ b/src/main/java/com/example/nto/service/AuthService.java @@ -0,0 +1,7 @@ +package com.example.nto.service; + +import com.example.nto.model.dto.AuthCredentials; + +public interface AuthService { + void auth(AuthCredentials authCredentials); +} diff --git a/src/main/java/com/example/nto/service/EmployeeCredentialsService.java b/src/main/java/com/example/nto/service/EmployeeCredentialsService.java new file mode 100644 index 0000000..a88058b --- /dev/null +++ b/src/main/java/com/example/nto/service/EmployeeCredentialsService.java @@ -0,0 +1,7 @@ +package com.example.nto.service; + +import com.example.nto.model.entity.Employee; + +public interface EmployeeCredentialsService { + +} diff --git a/src/main/java/com/example/nto/service/EmployeeService.java b/src/main/java/com/example/nto/service/EmployeeService.java index d18caaa..0741f3f 100644 --- a/src/main/java/com/example/nto/service/EmployeeService.java +++ b/src/main/java/com/example/nto/service/EmployeeService.java @@ -5,13 +5,10 @@ import org.springframework.security.core.userdetails.UserDetails; public interface EmployeeService { - void auth(String login, String password); - Employee getEmployee(String login); void updateVisit(String login, long value); void addEmployee(Employee employee); - UserDetails loadUserByUsername(String username); } diff --git a/src/main/java/com/example/nto/service/impl/AuthServiceImpl.java b/src/main/java/com/example/nto/service/impl/AuthServiceImpl.java new file mode 100644 index 0000000..4130648 --- /dev/null +++ b/src/main/java/com/example/nto/service/impl/AuthServiceImpl.java @@ -0,0 +1,21 @@ +package com.example.nto.service.impl; + +import com.example.nto.model.dto.AuthCredentials; +import com.example.nto.model.entity.Employee; +import com.example.nto.service.AuthService; +import com.example.nto.service.EmployeeCredentialsService; +import lombok.RequiredArgsConstructor; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class AuthServiceImpl implements AuthService { + + private final AuthenticationManager authenticationManager; + + public void auth(AuthCredentials authCredentials) { + authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(authCredentials.getLogin(), authCredentials.getPassword())); + } +} diff --git a/src/main/java/com/example/nto/service/impl/EmployeeCredentialsServiceImpl.java b/src/main/java/com/example/nto/service/impl/EmployeeCredentialsServiceImpl.java new file mode 100644 index 0000000..cc75d45 --- /dev/null +++ b/src/main/java/com/example/nto/service/impl/EmployeeCredentialsServiceImpl.java @@ -0,0 +1,25 @@ +package com.example.nto.service.impl; + +import com.example.nto.model.entity.Employee; +import com.example.nto.repository.EmployeeRepository; +import com.example.nto.service.EmployeeCredentialsService; +import com.example.nto.service.EmployeeService; +import com.example.nto.service.exception.EmployeeNotFoundException; +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; +import org.springframework.stereotype.Service; + +@Service +public class EmployeeCredentialsServiceImpl implements EmployeeCredentialsService, UserDetailsService { + + @Autowired + private EmployeeRepository employeeRepository; + + @Override + public UserDetails loadUserByUsername(String login) throws UsernameNotFoundException { + return employeeRepository.findEmployeeByLogin(login); + } + +} diff --git a/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java b/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java index 3bb8fd3..139d745 100644 --- a/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java @@ -7,6 +7,7 @@ import com.example.nto.service.EmployeeService; import com.example.nto.service.exception.CodeNotFoundException; import com.example.nto.service.exception.EmployeeNotFoundException; import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.userdetails.UserDetails; @@ -19,7 +20,7 @@ import java.time.LocalDateTime; @Service @RequiredArgsConstructor -public class EmployeeServiceImpl implements EmployeeService, UserDetailsService { +public class EmployeeServiceImpl implements EmployeeService { private final EmployeeRepository employeeRepository; @@ -27,14 +28,6 @@ public class EmployeeServiceImpl implements EmployeeService, UserDetailsService private final CodeRepository codeRepository; - private final AuthenticationManager authenticationManager; - - @Override - public void auth(final String login, final String password) { - final Employee employee = getEmployee(login); - authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(employee.getUsername(), employee.getPassword())); - } - @Override public Employee getEmployee(final String login) { if (!employeeRepository.existsByLogin(login)) { @@ -61,9 +54,4 @@ public class EmployeeServiceImpl implements EmployeeService, UserDetailsService employee.setPassword(passwordEncoder.encode(employee.getPassword())); employeeRepository.save(employee); } - - @Override - public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { - return employeeRepository.findEmployeeByLogin(username); - } }