diff --git a/src/main/java/com/example/nto/config/WebSecurityConfig.java b/src/main/java/com/example/nto/config/WebSecurityConfig.java new file mode 100644 index 0000000..e6a5338 --- /dev/null +++ b/src/main/java/com/example/nto/config/WebSecurityConfig.java @@ -0,0 +1,48 @@ +package com.example.nto.config; + +import lombok.RequiredArgsConstructor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +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.WebSecurityConfigurerAdapter; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; + + +@Configuration +@EnableWebSecurity +@RequiredArgsConstructor +public class WebSecurityConfig extends WebSecurityConfigurerAdapter { + + private final UserDetailsService userDetailsService; + + @Override + protected void configure(HttpSecurity http) throws Exception { + http + .csrf().disable() + .authorizeRequests() + .antMatchers("/h2-console/**").permitAll() + .antMatchers("/api/user/register").permitAll() + .antMatchers("/api/user/email/{email}").permitAll() + .antMatchers("/api/user/**").hasAnyAuthority("ROLE_ADMIN", "ROLE_USER") + .anyRequest().authenticated() + .and() + .httpBasic() + .and() + .headers().frameOptions().disable(); + } + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); + } + + @Bean + public PasswordEncoder passwordEncoder() { + return new BCryptPasswordEncoder(); + } + + +} diff --git a/src/main/java/com/example/nto/exception/AuthorityNotFoundException.java b/src/main/java/com/example/nto/exception/AuthorityNotFoundException.java new file mode 100644 index 0000000..6f971e1 --- /dev/null +++ b/src/main/java/com/example/nto/exception/AuthorityNotFoundException.java @@ -0,0 +1,7 @@ +package com.example.nto.exception; + +public class AuthorityNotFoundException extends RuntimeException { + public AuthorityNotFoundException(String message) { + super(message); + } +} diff --git a/src/main/java/com/example/nto/exception/CenterNotFoundException.java b/src/main/java/com/example/nto/exception/CenterNotFoundException.java new file mode 100644 index 0000000..20d4a6f --- /dev/null +++ b/src/main/java/com/example/nto/exception/CenterNotFoundException.java @@ -0,0 +1,7 @@ +package com.example.nto.exception; + +public class CenterNotFoundException extends RuntimeException { + public CenterNotFoundException(String message) { + super(message); + } +} diff --git a/src/main/java/com/example/nto/exception/EmailAlreadyExistsException.java b/src/main/java/com/example/nto/exception/EmailAlreadyExistsException.java new file mode 100644 index 0000000..49cfbc0 --- /dev/null +++ b/src/main/java/com/example/nto/exception/EmailAlreadyExistsException.java @@ -0,0 +1,7 @@ +package com.example.nto.exception; + +public class EmailAlreadyExistsException extends RuntimeException { + public EmailAlreadyExistsException(String message) { + super(message); + } +} diff --git a/src/main/java/com/example/nto/exception/InvalidBirthDateException.java b/src/main/java/com/example/nto/exception/InvalidBirthDateException.java new file mode 100644 index 0000000..8bd8a3e --- /dev/null +++ b/src/main/java/com/example/nto/exception/InvalidBirthDateException.java @@ -0,0 +1,7 @@ +package com.example.nto.exception; + +public class InvalidBirthDateException extends RuntimeException { + public InvalidBirthDateException(String message) { + super(message); + } +} diff --git a/src/main/java/com/example/nto/exception/InvalidDescriptionException.java b/src/main/java/com/example/nto/exception/InvalidDescriptionException.java new file mode 100644 index 0000000..b91c6a7 --- /dev/null +++ b/src/main/java/com/example/nto/exception/InvalidDescriptionException.java @@ -0,0 +1,7 @@ +package com.example.nto.exception; + +public class InvalidDescriptionException extends RuntimeException { + public InvalidDescriptionException(String message) { + super(message); + } +} diff --git a/src/main/java/com/example/nto/exception/InvalidNameException.java b/src/main/java/com/example/nto/exception/InvalidNameException.java new file mode 100644 index 0000000..1cc2218 --- /dev/null +++ b/src/main/java/com/example/nto/exception/InvalidNameException.java @@ -0,0 +1,7 @@ +package com.example.nto.exception; + +public class InvalidNameException extends RuntimeException { + public InvalidNameException(String message) { + super(message); + } +} diff --git a/src/main/java/com/example/nto/exception/InvalidPasswordException.java b/src/main/java/com/example/nto/exception/InvalidPasswordException.java new file mode 100644 index 0000000..ac6af7c --- /dev/null +++ b/src/main/java/com/example/nto/exception/InvalidPasswordException.java @@ -0,0 +1,7 @@ +package com.example.nto.exception; + +public class InvalidPasswordException extends RuntimeException { + public InvalidPasswordException(String message) { + super(message); + } +} diff --git a/src/main/java/com/example/nto/exception/NoRequestBodyException.java b/src/main/java/com/example/nto/exception/NoRequestBodyException.java new file mode 100644 index 0000000..f419b80 --- /dev/null +++ b/src/main/java/com/example/nto/exception/NoRequestBodyException.java @@ -0,0 +1,7 @@ +package com.example.nto.exception; + +public class NoRequestBodyException extends RuntimeException { + public NoRequestBodyException(String message) { + super(message); + } +} diff --git a/src/main/java/com/example/nto/exception/NoRequestParamsException.java b/src/main/java/com/example/nto/exception/NoRequestParamsException.java new file mode 100644 index 0000000..c97af8e --- /dev/null +++ b/src/main/java/com/example/nto/exception/NoRequestParamsException.java @@ -0,0 +1,7 @@ +package com.example.nto.exception; + +public class NoRequestParamsException extends RuntimeException { + public NoRequestParamsException(String message) { + super(message); + } +} diff --git a/src/main/java/com/example/nto/exception/OtherException.java b/src/main/java/com/example/nto/exception/OtherException.java new file mode 100644 index 0000000..415d034 --- /dev/null +++ b/src/main/java/com/example/nto/exception/OtherException.java @@ -0,0 +1,7 @@ +package com.example.nto.exception; + +public class OtherException extends RuntimeException { + public OtherException(String message) { + super(message); + } +} diff --git a/src/main/java/com/example/nto/exception/UserHasNoCenterException.java b/src/main/java/com/example/nto/exception/UserHasNoCenterException.java new file mode 100644 index 0000000..c887417 --- /dev/null +++ b/src/main/java/com/example/nto/exception/UserHasNoCenterException.java @@ -0,0 +1,7 @@ +package com.example.nto.exception; + +public class UserHasNoCenterException extends RuntimeException { + public UserHasNoCenterException(String message) { + super(message); + } +} diff --git a/src/main/java/com/example/nto/exception/UserNotFoundException.java b/src/main/java/com/example/nto/exception/UserNotFoundException.java new file mode 100644 index 0000000..5db2624 --- /dev/null +++ b/src/main/java/com/example/nto/exception/UserNotFoundException.java @@ -0,0 +1,7 @@ +package com.example.nto.exception; + +public class UserNotFoundException extends RuntimeException { + public UserNotFoundException(String message) { + super(message); + } +} diff --git a/src/main/java/com/example/nto/exception/UsernameAlreadyExistsException.java b/src/main/java/com/example/nto/exception/UsernameAlreadyExistsException.java new file mode 100644 index 0000000..52191f2 --- /dev/null +++ b/src/main/java/com/example/nto/exception/UsernameAlreadyExistsException.java @@ -0,0 +1,7 @@ +package com.example.nto.exception; + +public class UsernameAlreadyExistsException extends RuntimeException { + public UsernameAlreadyExistsException(String message) { + super(message); + } +} diff --git a/src/main/java/com/example/nto/exception/handler/GlobalExceptionHandler.java b/src/main/java/com/example/nto/exception/handler/GlobalExceptionHandler.java new file mode 100644 index 0000000..3f4f607 --- /dev/null +++ b/src/main/java/com/example/nto/exception/handler/GlobalExceptionHandler.java @@ -0,0 +1,77 @@ +package com.example.nto.exception.handler; + +import com.example.bootcamp.exception.*; +import liquibase.pro.packaged.E; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; + +@ControllerAdvice +public class GlobalExceptionHandler { + + @ExceptionHandler(CenterNotFoundException.class) + public ResponseEntity handleCenterNotFoundException(CenterNotFoundException e) { + return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND); + } + + @ExceptionHandler(UserNotFoundException.class) + public ResponseEntity handleUserNotFoundException(UserNotFoundException e) { + return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND); + } + + @ExceptionHandler(UserHasNoCenterException.class) + public ResponseEntity handleUserException(UserHasNoCenterException e) { + return new ResponseEntity<>(e.getMessage(), HttpStatus.CONFLICT); + } + + @ExceptionHandler(NoRequestBodyException.class) + public ResponseEntity handleNoRequestBodyException(NoRequestBodyException e) { + return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST); + } + + @ExceptionHandler(EmailAlreadyExistsException.class) + public ResponseEntity handleEmailAlreadyExistsException(EmailAlreadyExistsException e) { + return new ResponseEntity<>(e.getMessage(), HttpStatus.CONFLICT); + } + + @ExceptionHandler(NoRequestParamsException.class) + public ResponseEntity handleNoRequestParamsException(NoRequestParamsException e) { + return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST); + } + + @ExceptionHandler(InvalidBirthDateException.class) + public ResponseEntity handleInvalidBirthDateException(InvalidBirthDateException e) { + return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST); + } + + @ExceptionHandler(InvalidPasswordException.class) + public ResponseEntity handleInvalidPasswordException(InvalidPasswordException e) { + return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST); + } + + @ExceptionHandler(InvalidNameException.class) + public ResponseEntity handleInvalidNameException(InvalidNameException e) { + return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST); + } + + @ExceptionHandler(InvalidDescriptionException.class) + public ResponseEntity handleInvalidDescriptionException(InvalidDescriptionException e) { + return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST); + } + + @ExceptionHandler(OtherException.class) + public ResponseEntity handleOtherException(OtherException e) { + return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); + } + + @ExceptionHandler(UsernameAlreadyExistsException.class) + public ResponseEntity handleUsernameAlreadyExistsException(UsernameAlreadyExistsException e) { + return new ResponseEntity<>(e.getMessage(), HttpStatus.CONFLICT); + } + + @ExceptionHandler(AuthorityNotFoundException.class) + public ResponseEntity handleAuthorityNotFoundException(AuthorityNotFoundException e) { + return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND); + } +}