the cringe

This commit is contained in:
Konstantin 2025-02-19 16:35:50 +03:00
parent 9cccc1fe60
commit bc6ca99907
10 changed files with 103 additions and 24 deletions

View File

@ -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();

View File

@ -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);
}
}

View File

@ -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());

View File

@ -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;
}

View File

@ -0,0 +1,7 @@
package com.example.nto.service;
import com.example.nto.model.dto.AuthCredentials;
public interface AuthService {
void auth(AuthCredentials authCredentials);
}

View File

@ -0,0 +1,7 @@
package com.example.nto.service;
import com.example.nto.model.entity.Employee;
public interface EmployeeCredentialsService {
}

View File

@ -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);
}

View File

@ -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()));
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}