the cringe
This commit is contained in:
parent
9cccc1fe60
commit
bc6ca99907
@ -3,8 +3,10 @@ package com.example.nto.config;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
import org.springframework.security.authentication.AuthenticationProvider;
|
import org.springframework.security.authentication.AuthenticationProvider;
|
||||||
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
|
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.builders.HttpSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configurers.AbstractAuthenticationFilterConfigurer;
|
import org.springframework.security.config.annotation.web.configurers.AbstractAuthenticationFilterConfigurer;
|
||||||
@ -39,6 +41,11 @@ public class SecurityConfig {
|
|||||||
return provider;
|
return provider;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public AuthenticationManager authenticationManager(final AuthenticationConfiguration config) throws Exception {
|
||||||
|
return config.getAuthenticationManager();
|
||||||
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public PasswordEncoder passwordEncoder() {
|
public PasswordEncoder passwordEncoder() {
|
||||||
return new BCryptPasswordEncoder();
|
return new BCryptPasswordEncoder();
|
||||||
|
21
src/main/java/com/example/nto/controller/AuthController.java
Normal file
21
src/main/java/com/example/nto/controller/AuthController.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -14,18 +14,13 @@ public class EmployeeController {
|
|||||||
|
|
||||||
private final EmployeeService employeeService;
|
private final EmployeeService employeeService;
|
||||||
|
|
||||||
@GetMapping("/auth")
|
// @PreAuthorize("hasAuthority('User', 'Admin')")
|
||||||
public void auth(@RequestParam final String login, @RequestParam final String password) {
|
|
||||||
employeeService.auth(login, password);
|
|
||||||
}
|
|
||||||
|
|
||||||
@PreAuthorize("hasAuthority('User', 'Admin')")
|
|
||||||
@GetMapping("/info")
|
@GetMapping("/info")
|
||||||
public Employee info(@RequestParam final String login) {
|
public Employee info(@RequestParam final String login) {
|
||||||
return employeeService.getEmployee(login);
|
return employeeService.getEmployee(login);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PreAuthorize("hasAuthority('User', 'Admin')")
|
// @PreAuthorize("hasAuthority('User', 'Admin')")
|
||||||
@PatchMapping("/open")
|
@PatchMapping("/open")
|
||||||
public void open(@RequestParam final String login, @RequestBody final Code code) {
|
public void open(@RequestParam final String login, @RequestBody final Code code) {
|
||||||
employeeService.updateVisit(login, code.getValue());
|
employeeService.updateVisit(login, code.getValue());
|
||||||
|
11
src/main/java/com/example/nto/model/dto/AuthCredentials.java
Normal file
11
src/main/java/com/example/nto/model/dto/AuthCredentials.java
Normal 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;
|
||||||
|
}
|
7
src/main/java/com/example/nto/service/AuthService.java
Normal file
7
src/main/java/com/example/nto/service/AuthService.java
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package com.example.nto.service;
|
||||||
|
|
||||||
|
import com.example.nto.model.dto.AuthCredentials;
|
||||||
|
|
||||||
|
public interface AuthService {
|
||||||
|
void auth(AuthCredentials authCredentials);
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.example.nto.service;
|
||||||
|
|
||||||
|
import com.example.nto.model.entity.Employee;
|
||||||
|
|
||||||
|
public interface EmployeeCredentialsService {
|
||||||
|
|
||||||
|
}
|
@ -5,13 +5,10 @@ import org.springframework.security.core.userdetails.UserDetails;
|
|||||||
|
|
||||||
public interface EmployeeService {
|
public interface EmployeeService {
|
||||||
|
|
||||||
void auth(String login, String password);
|
|
||||||
|
|
||||||
Employee getEmployee(String login);
|
Employee getEmployee(String login);
|
||||||
|
|
||||||
void updateVisit(String login, long value);
|
void updateVisit(String login, long value);
|
||||||
|
|
||||||
void addEmployee(Employee employee);
|
void addEmployee(Employee employee);
|
||||||
|
|
||||||
UserDetails loadUserByUsername(String username);
|
|
||||||
}
|
}
|
||||||
|
@ -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()));
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -7,6 +7,7 @@ import com.example.nto.service.EmployeeService;
|
|||||||
import com.example.nto.service.exception.CodeNotFoundException;
|
import com.example.nto.service.exception.CodeNotFoundException;
|
||||||
import com.example.nto.service.exception.EmployeeNotFoundException;
|
import com.example.nto.service.exception.EmployeeNotFoundException;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
@ -19,7 +20,7 @@ import java.time.LocalDateTime;
|
|||||||
|
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class EmployeeServiceImpl implements EmployeeService, UserDetailsService {
|
public class EmployeeServiceImpl implements EmployeeService {
|
||||||
|
|
||||||
private final EmployeeRepository employeeRepository;
|
private final EmployeeRepository employeeRepository;
|
||||||
|
|
||||||
@ -27,14 +28,6 @@ public class EmployeeServiceImpl implements EmployeeService, UserDetailsService
|
|||||||
|
|
||||||
private final CodeRepository codeRepository;
|
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
|
@Override
|
||||||
public Employee getEmployee(final String login) {
|
public Employee getEmployee(final String login) {
|
||||||
if (!employeeRepository.existsByLogin(login)) {
|
if (!employeeRepository.existsByLogin(login)) {
|
||||||
@ -61,9 +54,4 @@ public class EmployeeServiceImpl implements EmployeeService, UserDetailsService
|
|||||||
employee.setPassword(passwordEncoder.encode(employee.getPassword()));
|
employee.setPassword(passwordEncoder.encode(employee.getPassword()));
|
||||||
employeeRepository.save(employee);
|
employeeRepository.save(employee);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
|
||||||
return employeeRepository.findEmployeeByLogin(username);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user