first working version

This commit is contained in:
Konstantin 2025-02-20 11:47:10 +03:00
parent 7292491328
commit 4b2787344a
4 changed files with 48 additions and 17 deletions

@ -1,11 +1,17 @@
package com.example.nto.config;
import com.example.nto.model.entity.Employee;
import com.example.nto.repository.EmployeeRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.sql.init.dependency.DependsOnDatabaseInitialization;
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.Customizer;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
@ -16,36 +22,51 @@ import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig {
private final UserDetailsService userDetailsService;
@Autowired
private EmployeeRepository employeeRepository;
@Bean
@DependsOnDatabaseInitialization
public UserDetailsService userDetailsService(){
InMemoryUserDetailsManager inMemoryUserDetailsManager = new InMemoryUserDetailsManager();
for (Employee employee : employeeRepository.findAll()) {
inMemoryUserDetailsManager.createUser(employee);
}
return inMemoryUserDetailsManager;
}
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
return http.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(auth -> auth.requestMatchers(new AntPathRequestMatcher("/api/auth")).permitAll().anyRequest().authenticated())
.formLogin(AbstractAuthenticationFilterConfigurer::permitAll)
.getOrBuild();
.authorizeHttpRequests(auth -> auth.requestMatchers(new AntPathRequestMatcher("/api/auth")).permitAll()
.anyRequest().authenticated())
.httpBasic(Customizer.withDefaults())
.build();
}
@Bean
public AuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setUserDetailsService(userDetailsService);
provider.setUserDetailsService(userDetailsService());
provider.setPasswordEncoder(passwordEncoder());
return provider;
}
@Bean
public AuthenticationManager authenticationManager(final AuthenticationConfiguration config) throws Exception {
return config.getAuthenticationManager();
public AuthenticationManager authenticationManager(HttpSecurity http) throws Exception {
AuthenticationManagerBuilder authenticationManagerBuilder =
http.getSharedObject(AuthenticationManagerBuilder.class);
authenticationManagerBuilder.userDetailsService(userDetailsService());
return authenticationManagerBuilder.build();
}
@Bean

@ -28,14 +28,21 @@ public class EmployeeController {
employeeService.updateVisit(login, code.getValue());
}
@PreAuthorize("hasAuthority('ROLE_ADMIN')")
@PostMapping("/add")
public void add(@RequestBody final Employee employee) {
employeeService.addEmployee(employee);
}
// @PreAuthorize("hasAuthority('ROLE_ADMIN')")
@GetMapping("/ban")
@PreAuthorize("hasAuthority('ROLE_ADMIN')")
@PutMapping("/ban")
public void ban(@RequestParam final String login) {
employeeService.banEmployee(login);
}
@PreAuthorize("hasAuthority('ROLE_ADMIN')")
@PutMapping("/unban")
public void unban(@RequestParam final String login) {
employeeService.unbanEmployee(login);
}
}

@ -14,4 +14,6 @@ public interface EmployeeService {
void addEmployee(Employee employee);
void banEmployee(String login);
void unbanEmployee(String login);
}

@ -8,13 +8,6 @@ import com.example.nto.service.exception.CodeNotFoundException;
import com.example.nto.service.exception.EmployeeBannedException;
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;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
@ -74,4 +67,12 @@ public class EmployeeServiceImpl implements EmployeeService {
employee.setIsBanned(true);
employeeRepository.save(employee);
}
@Override
public void unbanEmployee(String login) {
employeeExists(login);
var employee = getEmployee(login);
employee.setIsBanned(false);
employeeRepository.save(employee);
}
}