checkpoint3
This commit is contained in:
parent
f5f797a9cf
commit
3f7e768a5e
@ -2,9 +2,8 @@ package com.example.nto;
|
|||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
|
|
||||||
|
|
||||||
@SpringBootApplication(exclude = { SecurityAutoConfiguration.class })
|
@SpringBootApplication
|
||||||
public class App {
|
public class App {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(App.class, args);
|
SpringApplication.run(App.class, args);
|
||||||
|
|||||||
@ -1,35 +1,40 @@
|
|||||||
//package com.example.nto.controller.config;
|
package com.example.nto.config;
|
||||||
//
|
|
||||||
//import org.springframework.context.annotation.Bean;
|
import com.example.nto.filter.BaseAuthFilter;
|
||||||
//import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Bean;
|
||||||
//import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
import org.springframework.context.annotation.Configuration;
|
||||||
//import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
import org.springframework.security.config.Customizer;
|
||||||
//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
//import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||||
//import org.springframework.security.web.SecurityFilterChain;
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
//
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
//@Configuration
|
import org.springframework.security.web.SecurityFilterChain;
|
||||||
//@EnableWebSecurity
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
||||||
//public class WebSecurityConfig {
|
|
||||||
// @Bean
|
@Configuration
|
||||||
// public PasswordEncoder passwordEncoder() {
|
@EnableWebSecurity
|
||||||
// return new BCryptPasswordEncoder();
|
public class WebSecurityConfig {
|
||||||
// }
|
@Bean
|
||||||
//
|
public PasswordEncoder passwordEncoder() {
|
||||||
// @Bean
|
return new BCryptPasswordEncoder();
|
||||||
// public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
}
|
||||||
// http
|
|
||||||
// .csrf(csrf -> csrf.disable())
|
@Bean
|
||||||
// .authorizeHttpRequests((authorize) -> authorize
|
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||||
// .requestMatchers("/h2-console").permitAll()
|
return
|
||||||
// .requestMatchers("/index.html").permitAll()
|
http
|
||||||
// .requestMatchers("/register").permitAll()
|
.csrf(csrf -> csrf.disable())
|
||||||
// .anyRequest().authenticated()
|
.authorizeHttpRequests((authorize) -> authorize
|
||||||
// );
|
.requestMatchers("/h2-console").permitAll()
|
||||||
//
|
.requestMatchers("/register").permitAll()
|
||||||
// return http.build();
|
.anyRequest().authenticated()).
|
||||||
// }
|
addFilterBefore(new BaseAuthFilter(), UsernamePasswordAuthenticationFilter.class).
|
||||||
//
|
httpBasic(Customizer.withDefaults())
|
||||||
//
|
.build();
|
||||||
//
|
|
||||||
//}
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@ -1,14 +1,21 @@
|
|||||||
package com.example.nto.controller;
|
package com.example.nto.controller;
|
||||||
|
|
||||||
|
|
||||||
import com.example.nto.controller.dto.EmployeeDto;
|
import com.example.nto.controller.dto.EmployeeDto;
|
||||||
import com.example.nto.controller.dto.EmployeeRegisterDto;
|
|
||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.entity.Employee;
|
||||||
import com.example.nto.service.EmployeeService;
|
import com.example.nto.service.EmployeeService;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.apache.tomcat.util.net.openssl.ciphers.Authentication;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.security.core.context.SecurityContext;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.security.web.server.authorization.AuthorizationContext;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static com.example.nto.controller.dto.EmployeeDto.toDto;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("api")
|
@RequestMapping("api")
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@ -16,17 +23,14 @@ public class EmployeeController {
|
|||||||
|
|
||||||
private final EmployeeService employeeService;
|
private final EmployeeService employeeService;
|
||||||
|
|
||||||
@GetMapping("/{username}/info")
|
@GetMapping("/login")
|
||||||
@ResponseStatus(code = HttpStatus.OK)
|
public Employee login(Authentication auth) {
|
||||||
public EmployeeDto getByUsername(@PathVariable String username) {
|
return employeeService.getByUsername((auth.getDeclaringClass()).getName());
|
||||||
return employeeService.getByUsername(username);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/register")
|
@PostMapping("/register")
|
||||||
@ResponseStatus(code = HttpStatus.CREATED)
|
public void register(@RequestBody Map<String, String> body) {
|
||||||
public Employee registerEmployee(EmployeeRegisterDto employeeRegisterDto) {
|
employeeService.register(body.get("login"), body.get("password"));
|
||||||
return employeeService.register(employeeRegisterDto);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,7 +17,7 @@ import java.util.List;
|
|||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@Table(name = "employee")
|
@Table(name = "employee")
|
||||||
public class Employee implements UserDetails{
|
public class Employee implements UserDetails {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
|||||||
@ -0,0 +1,7 @@
|
|||||||
|
package com.example.nto.exception;
|
||||||
|
|
||||||
|
public class NoSuchUsernameException extends RuntimeException {
|
||||||
|
public NoSuchUsernameException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -28,6 +28,11 @@ public class GlobalExceptionHandler {
|
|||||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.CONFLICT);
|
return new ResponseEntity<>(e.getMessage(), HttpStatus.CONFLICT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(NoSuchUsernameException.class)
|
||||||
|
public ResponseEntity<String> handleNoSuchUsernameException(NoSuchUsernameException e) {
|
||||||
|
return new ResponseEntity<>(e.getMessage(), HttpStatus.CONFLICT);
|
||||||
|
}
|
||||||
|
|
||||||
@ExceptionHandler(Exception.class)
|
@ExceptionHandler(Exception.class)
|
||||||
public ResponseEntity<String> handleGenericException(Exception e) {
|
public ResponseEntity<String> handleGenericException(Exception e) {
|
||||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
|
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
|
||||||
|
|||||||
36
src/main/java/com/example/nto/filter/BaseAuthFilter.java
Normal file
36
src/main/java/com/example/nto/filter/BaseAuthFilter.java
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package com.example.nto.filter;
|
||||||
|
|
||||||
|
import jakarta.servlet.FilterChain;
|
||||||
|
import jakarta.servlet.ServletException;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.web.filter.OncePerRequestFilter;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Base64;
|
||||||
|
|
||||||
|
public class BaseAuthFilter extends OncePerRequestFilter {
|
||||||
|
@Override
|
||||||
|
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
|
||||||
|
String header = request.getHeader("Authorisation");
|
||||||
|
if (header != null && header.startsWith("Base")) {
|
||||||
|
try {
|
||||||
|
String base64Token = header.substring(5);
|
||||||
|
byte[] decoded = Base64.getDecoder().decode(base64Token);
|
||||||
|
String credentials = new String(decoded, StandardCharsets.UTF_8);
|
||||||
|
String[] values = credentials.split(" ", 2);
|
||||||
|
String username = values[0];
|
||||||
|
String password = values[1];
|
||||||
|
|
||||||
|
UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(username, password, new ArrayList<>());
|
||||||
|
SecurityContextHolder.getContext().setAuthentication(auth);
|
||||||
|
} catch (Exception e) {
|
||||||
|
filterChain.doFilter(request, response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,11 +1,9 @@
|
|||||||
package com.example.nto.service;
|
package com.example.nto.service;
|
||||||
|
|
||||||
import com.example.nto.controller.dto.EmployeeDto;
|
|
||||||
import com.example.nto.controller.dto.EmployeeRegisterDto;
|
|
||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.entity.Employee;
|
||||||
|
|
||||||
public interface EmployeeService {
|
public interface EmployeeService {
|
||||||
EmployeeDto getByUsername(String username);
|
Employee getByUsername(String username);
|
||||||
|
|
||||||
Employee register(EmployeeRegisterDto employeeRegisterDto);
|
void register(String login, String password);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,23 @@
|
|||||||
|
package com.example.nto.service.impl;
|
||||||
|
|
||||||
|
import com.example.nto.entity.Employee;
|
||||||
|
import com.example.nto.exception.NoSuchUsernameException;
|
||||||
|
import com.example.nto.repository.EmployeeRepository;
|
||||||
|
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 java.util.ArrayList;
|
||||||
|
|
||||||
|
public class CustomUserDetailsServiceImpl implements UserDetailsService {
|
||||||
|
@Autowired private EmployeeRepository employeeRepository;
|
||||||
|
@Override
|
||||||
|
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||||
|
Employee employee = employeeRepository.findByUsername(username).orElseThrow(() -> new NoSuchUsernameException("No employee with " + username + " username!!!"));
|
||||||
|
|
||||||
|
return new org.springframework.security.core.userdetails.User(employee.getUsername(),
|
||||||
|
employee.getPassword(),
|
||||||
|
new ArrayList<>());
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,43 +1,37 @@
|
|||||||
package com.example.nto.service.impl;
|
package com.example.nto.service.impl;
|
||||||
|
|
||||||
import com.example.nto.controller.dto.EmployeeDto;
|
|
||||||
import com.example.nto.controller.dto.EmployeeRegisterDto;
|
|
||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.entity.Employee;
|
||||||
import com.example.nto.exception.EmployeeAlreadyExistsException;
|
import com.example.nto.exception.EmployeeAlreadyExistsException;
|
||||||
import com.example.nto.exception.EmployeeNotFoundException;
|
import com.example.nto.exception.NoSuchUsernameException;
|
||||||
import com.example.nto.repository.EmployeeRepository;
|
import com.example.nto.repository.EmployeeRepository;
|
||||||
import com.example.nto.service.EmployeeService;
|
import com.example.nto.service.EmployeeService;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class EmployeeServiceImpl implements EmployeeService {
|
public class EmployeeServiceImpl implements EmployeeService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
private final EmployeeRepository employeeRepository;
|
private final EmployeeRepository employeeRepository;
|
||||||
|
private final PasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(readOnly = true)
|
public Employee getByUsername(String username) {
|
||||||
public EmployeeDto getByUsername(String username) {
|
return employeeRepository.findByUsername(username).orElseThrow(() -> new NoSuchUsernameException("No employee with " + username + " username!!!"));
|
||||||
return employeeRepository.findByUsername(username).map(EmployeeDto::toDto)
|
|
||||||
.orElseThrow(() -> new EmployeeNotFoundException("Employee with " + username + " code not found!"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Employee register(EmployeeRegisterDto employeeRegisterDto) {
|
public void register(String login, String password) {
|
||||||
if (employeeRepository.findByUsername(employeeRegisterDto.getUsername()).isPresent()) {
|
if (employeeRepository.findByUsername(login).isPresent()){
|
||||||
throw new EmployeeAlreadyExistsException("Employee with " + employeeRegisterDto.getUsername() + " username");
|
throw new EmployeeAlreadyExistsException("Username is already exists");
|
||||||
}
|
}
|
||||||
|
|
||||||
Employee employee = new Employee();
|
Employee employee = new Employee();
|
||||||
employee.setUsername(employeeRegisterDto.getUsername());
|
employee.setUsername(login);
|
||||||
employee.setPassword(employeeRegisterDto.getPassword());
|
employee.setPassword(passwordEncoder.encode(password));
|
||||||
|
|
||||||
return employeeRepository.save(employee);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user