first working version
This commit is contained in:
parent
7292491328
commit
4b2787344a
@ -1,11 +1,17 @@
|
|||||||
package com.example.nto.config;
|
package com.example.nto.config;
|
||||||
|
|
||||||
|
import com.example.nto.model.entity.Employee;
|
||||||
|
import com.example.nto.repository.EmployeeRepository;
|
||||||
import lombok.RequiredArgsConstructor;
|
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.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
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.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.authentication.configuration.AuthenticationConfiguration;
|
||||||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
||||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
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.bcrypt.BCryptPasswordEncoder;
|
||||||
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
|
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
|
||||||
import org.springframework.security.web.SecurityFilterChain;
|
import org.springframework.security.web.SecurityFilterChain;
|
||||||
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
@RequiredArgsConstructor
|
|
||||||
@EnableGlobalMethodSecurity(prePostEnabled = true)
|
@EnableGlobalMethodSecurity(prePostEnabled = true)
|
||||||
public class SecurityConfig {
|
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
|
@Bean
|
||||||
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
||||||
return http.csrf(AbstractHttpConfigurer::disable)
|
return http.csrf(AbstractHttpConfigurer::disable)
|
||||||
.authorizeHttpRequests(auth -> auth.requestMatchers(new AntPathRequestMatcher("/api/auth")).permitAll().anyRequest().authenticated())
|
.authorizeHttpRequests(auth -> auth.requestMatchers(new AntPathRequestMatcher("/api/auth")).permitAll()
|
||||||
.formLogin(AbstractAuthenticationFilterConfigurer::permitAll)
|
.anyRequest().authenticated())
|
||||||
.getOrBuild();
|
.httpBasic(Customizer.withDefaults())
|
||||||
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public AuthenticationProvider authenticationProvider() {
|
public AuthenticationProvider authenticationProvider() {
|
||||||
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
|
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
|
||||||
provider.setUserDetailsService(userDetailsService);
|
provider.setUserDetailsService(userDetailsService());
|
||||||
provider.setPasswordEncoder(passwordEncoder());
|
provider.setPasswordEncoder(passwordEncoder());
|
||||||
return provider;
|
return provider;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public AuthenticationManager authenticationManager(final AuthenticationConfiguration config) throws Exception {
|
public AuthenticationManager authenticationManager(HttpSecurity http) throws Exception {
|
||||||
return config.getAuthenticationManager();
|
AuthenticationManagerBuilder authenticationManagerBuilder =
|
||||||
|
http.getSharedObject(AuthenticationManagerBuilder.class);
|
||||||
|
authenticationManagerBuilder.userDetailsService(userDetailsService());
|
||||||
|
return authenticationManagerBuilder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
@ -28,14 +28,21 @@ public class EmployeeController {
|
|||||||
employeeService.updateVisit(login, code.getValue());
|
employeeService.updateVisit(login, code.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PreAuthorize("hasAuthority('ROLE_ADMIN')")
|
||||||
@PostMapping("/add")
|
@PostMapping("/add")
|
||||||
public void add(@RequestBody final Employee employee) {
|
public void add(@RequestBody final Employee employee) {
|
||||||
employeeService.addEmployee(employee);
|
employeeService.addEmployee(employee);
|
||||||
}
|
}
|
||||||
|
|
||||||
// @PreAuthorize("hasAuthority('ROLE_ADMIN')")
|
@PreAuthorize("hasAuthority('ROLE_ADMIN')")
|
||||||
@GetMapping("/ban")
|
@PutMapping("/ban")
|
||||||
public void ban(@RequestParam final String login) {
|
public void ban(@RequestParam final String login) {
|
||||||
employeeService.banEmployee(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 addEmployee(Employee employee);
|
||||||
|
|
||||||
void banEmployee(String login);
|
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.EmployeeBannedException;
|
||||||
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.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.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@ -74,4 +67,12 @@ public class EmployeeServiceImpl implements EmployeeService {
|
|||||||
employee.setIsBanned(true);
|
employee.setIsBanned(true);
|
||||||
employeeRepository.save(employee);
|
employeeRepository.save(employee);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void unbanEmployee(String login) {
|
||||||
|
employeeExists(login);
|
||||||
|
var employee = getEmployee(login);
|
||||||
|
employee.setIsBanned(false);
|
||||||
|
employeeRepository.save(employee);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user