Skip to content

Commit

Permalink
Cleaning the project of unnecessary files and adding descriptions des…
Browse files Browse the repository at this point in the history
…cribing individual code elements
  • Loading branch information
iwokonl committed Mar 1, 2024
1 parent afa6f7b commit ea59076
Show file tree
Hide file tree
Showing 19 changed files with 27 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public class PasswordEncoderConfig {

@Bean
public BCryptPasswordEncoder passwordEncoder() {
public BCryptPasswordEncoder passwordEncoder() { // Twożymy beana który będzie odpowiedzialny za szyfrowanie hasła
return new BCryptPasswordEncoder();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class RestExeptionHandler {
@ExceptionHandler(AppExeption.class)
@ResponseBody
public ResponseEntity<ErrorDto> handleAppExeption(AppExeption e) {
return ResponseEntity.status(e.getHttpStatus()).body(new ErrorDto(e.getMessage()));
public ResponseEntity<ErrorDto> handleAppExeption(AppExeption e) { // metoda obsługująca wyjątki
return ResponseEntity.status(e.getHttpStatus()).body(new ErrorDto(e.getMessage())); // zwracamy status błędu i wiadomość
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
public class SecurityConfig {

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
httpSecurity.csrf(AbstractHttpConfigurer::disable)
.sessionManagement(customizer -> customizer.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(request ->
request.requestMatchers(HttpMethod.POST, "/login", "/register").permitAll()
.anyRequest().authenticated());
public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception { // Konfiguracja zabezpieczeń
httpSecurity.csrf(AbstractHttpConfigurer::disable) // Ochrona CSRF wyłączona
.sessionManagement(customizer -> customizer.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) // Wyłączenie zarządzania sesją
.authorizeHttpRequests(request -> // Konfiguracja zabezpieczeń
request.requestMatchers(HttpMethod.POST, "/login", "/register").permitAll() // Pozwala na wykonywanie zapytań POST na adresach: /login, /register
.anyRequest().authenticated()); // Wymaga autoryzacji dla pozostałych zapytań
return httpSecurity.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,27 @@
public class WebConfig {

@Bean
public FilterRegistrationBean crosFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("http://localhost:4200");
config.setAllowedHeaders(Arrays.asList(
public FilterRegistrationBean crosFilter() { // Rejestracja filtra zabezpieczeń CORS
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); // Konfiguracja zabezpieczeń CORS
CorsConfiguration config = new CorsConfiguration(); // Konfiguracja zabezpieczeń CORS
config.setAllowCredentials(true); // Zezwala na wykonywanie zapytań z poziomu przeglądarki
config.addAllowedOrigin("http://localhost:4200"); // Zezwala na wykonywanie zapytań z adresu: http://localhost:4200
config.setAllowedHeaders(Arrays.asList( // Zezwala na wykonywanie zapytań z nagłówkami typu: AUTHORIZATION, CONTENT_TYPE, ACCEPT
HttpHeaders.AUTHORIZATION,
HttpHeaders.CONTENT_TYPE,
HttpHeaders.ACCEPT

));
config.setAllowedMethods(Arrays.asList(
config.setAllowedMethods(Arrays.asList( // Zezwala na wykonywanie zapytań typu GET, POST, PUT, DELETE
HttpMethod.GET.name(),
HttpMethod.POST.name(),
HttpMethod.PUT.name(),
HttpMethod.DELETE.name()
));
config.setMaxAge(3600L);
source.registerCorsConfiguration("/**", config);
FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
bean.setOrder(-102);
config.setMaxAge(3600L); // Ustawia czas życia zapytania na 3600 sekund
source.registerCorsConfiguration("/**", config); // Rejestruje konfigurację zabezpieczeń CORS dla wszystkich adresów
FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source)); // Rejestruje filtr zabezpieczeń CORS
bean.setOrder(-102); // Ustawia priorytet filtra na -102 (Aby wykonał się ostatni)
return bean;
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pl.zeto.backend.VMC.dto;

public record CredentialsDto(String username, char[] password) {
public record CredentialsDto(String username, char[] password) { // Niemutowalna klasa przechowująca dane logowania
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pl.zeto.backend.VMC.dto;

public record ErrorDto (String message) {
public record ErrorDto (String message) { // Niemutowalna klasa przechowująca dane błędu
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@

@Getter
@Setter
public class RegistrationRequest {
public class RegistrationRequest { // Klasa przechowująca dane rejestracji
private String username;
private String firstName;
private String lastName;
private String email;
private String password;

// Getters and setters
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package pl.zeto.backend.VMC.dto;

public record SignUpDto (String firstName, String lastName,String email,String username, char[] password){
public record SignUpDto (String firstName, String lastName,String email,String username, char[] password){ // Niemutowalna klasa przechowująca dane rejestracji

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@AllArgsConstructor
@Builder
@NoArgsConstructor
public class UserDto {
public class UserDto { // Klasa przechowująca dane użytkownika
private Long id;
private Role role;
private String username;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.springframework.http.HttpStatus;

public class AppExeption extends RuntimeException {
public class AppExeption extends RuntimeException { // Klasa wyjątku aplikacji
private final HttpStatus httpStatus;
public AppExeption(String message, HttpStatus httpStatus) {
super(message);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@Mapper(componentModel="spring", uses= UserService.class)
@Component
public interface UserMapper {
public interface UserMapper { // Interfejs mapujący obiekty użytkownika na obiekty DTO
UserDto toUserDto(AppUser appUser);
@Mapping(target = "password", ignore = true)
AppUser signUpToUserr(SignUpDto signUpDto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ public interface UserRepo extends JpaRepository<AppUser, Long> {

Optional<AppUser> findByUsername(String username);

AppUser findByUsernameOrUsername(String username, String username2);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.userdetails.User;
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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pl.zeto.backend.VMC.dto.CredentialsDto;
import pl.zeto.backend.VMC.dto.SignUpDto;
import pl.zeto.backend.VMC.dto.UserDto;
Expand All @@ -21,11 +14,8 @@
import pl.zeto.backend.VMC.model.AppUser;
import pl.zeto.backend.VMC.model.Role;
import pl.zeto.backend.VMC.repository.UserRepo;
import pl.zeto.backend.VMC.repository.UsersRepo;

import java.beans.Transient;
import java.nio.CharBuffer;
import java.util.ArrayList;
import java.util.Optional;

@Service
Expand All @@ -34,7 +24,6 @@ public class UserService {
private static final Logger logger = LoggerFactory.getLogger(UserService.class);

private final UserRepo userRepository;
private final UsersRepo usersRepos;
private final BCryptPasswordEncoder passwordEncoder;
private final UserMapper userMapper;
private final AccountService accountService;
Expand All @@ -51,19 +40,6 @@ public UserDto login(CredentialsDto credentialsDto){
throw new AppExeption("Invalid password", HttpStatus.BAD_REQUEST);
}


public AppUser addUser(AppUser user) {

user.setRole(Role.USER);
user.setPassword(passwordEncoder.encode(user.getPassword()));
return userRepository.save(user);
}

public AppUser getUser(Long id) {
return userRepository.findById(id).orElse(null);
}


public UserDto register(SignUpDto signUpDto) {
Optional<AppUser> oUser = userRepository.findByUsername(signUpDto.username());

Expand Down

0 comments on commit ea59076

Please sign in to comment.