Skip to content

Commit

Permalink
merge : 1-branch, 3-branch
Browse files Browse the repository at this point in the history
  • Loading branch information
sjmjys954646 committed Oct 8, 2023
2 parents f30b5c0 + e9235e9 commit fc6e946
Show file tree
Hide file tree
Showing 32 changed files with 461 additions and 183 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/example/demo/GardenBeApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

@EnableJpaAuditing
@SpringBootApplication
@EnableJpaAuditing
public class GardenBeApplication {

public static void main(String[] args) {
Expand Down
57 changes: 0 additions & 57 deletions src/main/java/com/example/demo/account/AccountRequest.java

This file was deleted.

4 changes: 0 additions & 4 deletions src/main/java/com/example/demo/account/AccountResponse.java

This file was deleted.

21 changes: 0 additions & 21 deletions src/main/java/com/example/demo/account/AccountService.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.demo.config.auth;

import com.example.demo.account.Account;
import com.example.demo.account.AccountJPARepository;
import com.example.demo.user.User;
import com.example.demo.user.UserJPARepository;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
Expand All @@ -13,17 +13,17 @@
@RequiredArgsConstructor
@Service
public class CustomUserDetailService implements UserDetailsService {
private final AccountJPARepository accountJPARepository;
private final UserJPARepository accountJPARepository;

@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
Optional<Account> optionalAccount = accountJPARepository.findByEmail(email);
Optional<User> optionalAccount = accountJPARepository.findByEmail(email);

if (optionalAccount.isEmpty()) {
return null;
}
else {
Account account = optionalAccount.get();
User account = optionalAccount.get();
return new CustomUserDetails(account);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.demo.config.auth;

import com.example.demo.account.Account;
import com.example.demo.user.User;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
Expand All @@ -14,7 +14,7 @@
@RequiredArgsConstructor
@Getter
public class CustomUserDetails implements UserDetails {
private final Account account;
private final User account;

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,40 @@
import com.example.demo.config.utils.ApiUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception400.class)
public ResponseEntity<?> badRequest(Exception400 e){
return new ResponseEntity<>(e.body(), e.status());
public ResponseEntity<?> badRequest(Exception400 exception) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(exception.body());
}

@ExceptionHandler(Exception401.class)
public ResponseEntity<?> unAuthorized(Exception401 e){
return new ResponseEntity<>(e.body(), e.status());
public ResponseEntity<?> unAuthorized(Exception401 exception) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(exception.body());
}

@ExceptionHandler(Exception403.class)
public ResponseEntity<?> forbidden(Exception403 e){
return new ResponseEntity<>(e.body(), e.status());
public ResponseEntity<?> forbidden(Exception403 exception) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(exception.body());
}

@ExceptionHandler(Exception404.class)
public ResponseEntity<?> notFound(Exception404 e){
return new ResponseEntity<>(e.body(), e.status());
public ResponseEntity<?> notFound(Exception404 exception) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(exception.body());
}

@ExceptionHandler(Exception500.class)
public ResponseEntity<?> serverError(Exception500 e){
return new ResponseEntity<>(e.body(), e.status());
public ResponseEntity<?> serverError(Exception500 exception) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(exception.body());
}

@ExceptionHandler(Exception.class)
public ResponseEntity<?> unknownError(Exception e){
ApiUtils.ApiResult<?> apiResult = ApiUtils.error(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
return new ResponseEntity<>(apiResult, HttpStatus.INTERNAL_SERVER_ERROR);
public ResponseEntity<?> unknownError(Exception exception) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ApiUtils.error(exception.getMessage()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,42 @@
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Aspect
@Component
public class GlobalValidationHandler {
@Pointcut("@annotation(org.springframework.web.bind.annotation.PostMapping)")
public void postMapping() {
}
public void postMapping() {}

@Before("postMapping()")
public void validationAdvice(JoinPoint jp) {
Object[] args = jp.getArgs();
public void validationAdvice(JoinPoint joinPoint) {
Map<String, String> errorList = new HashMap<>();

Object[] args = joinPoint.getArgs(); // join point parameter

for (Object arg : args) {
if (arg instanceof Errors) {
Errors errors = (Errors) arg;

if (errors.hasErrors()) {
throw new Exception400(
errors.getFieldErrors().get(0).getDefaultMessage()+":"+errors.getFieldErrors().get(0).getField()
);
List<FieldError> fieldErrors = errors.getFieldErrors();
for (FieldError fieldError : fieldErrors) {
errorList.put(fieldError.getField(), fieldError.getDefaultMessage());
}
}
}
}

if (!errorList.isEmpty()) {
throw new Exception400(errorList, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,27 @@
import com.example.demo.config.utils.ApiUtils;
import lombok.Getter;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;

import java.util.HashMap;
import java.util.Map;

@Getter
public class Exception400 extends RuntimeException {
public Exception400(String message) {
Map<String, String> errors;

public Exception400(Map<String, String> errors, String message) {
super(message);
this.errors = errors;
}

public ApiUtils.ApiResult<?> body(){
return ApiUtils.error(getMessage(), HttpStatus.BAD_REQUEST);
public Exception400(String message) {
super(message);
errors = null;
}

public HttpStatus status(){
return HttpStatus.BAD_REQUEST;
public ApiUtils.ApiResponse<?> body(){
return ApiUtils.fail(errors, getMessage());
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ public Exception401(String message) {
super(message);
}

public ApiUtils.ApiResult<?> body(){
return ApiUtils.error(getMessage(), HttpStatus.UNAUTHORIZED);
}

public HttpStatus status(){
return HttpStatus.UNAUTHORIZED;
public ApiUtils.ApiResponse<?> body(){
return ApiUtils.error(getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ public Exception403(String message) {
super(message);
}

public ApiUtils.ApiResult<?> body(){
return ApiUtils.error(getMessage(), HttpStatus.FORBIDDEN);
}

public HttpStatus status(){
return HttpStatus.FORBIDDEN;
public ApiUtils.ApiResponse<?> body(){
return ApiUtils.error(getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ public Exception404(String message) {
super(message);
}

public ApiUtils.ApiResult<?> body(){
return ApiUtils.error(getMessage(), HttpStatus.NOT_FOUND);
}

public HttpStatus status(){
return HttpStatus.NOT_FOUND;
public ApiUtils.ApiResponse<?> body(){
return ApiUtils.error(getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ public Exception500(String message) {
super(message);
}

public ApiUtils.ApiResult<?> body() {
return ApiUtils.error(getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}

public HttpStatus status() {
return HttpStatus.INTERNAL_SERVER_ERROR;
public ApiUtils.ApiResponse<?> body() {
return ApiUtils.error(getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.example.demo.account.Account;
import com.example.demo.user.User;
import com.example.demo.config.auth.CustomUserDetails;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AuthenticationManager;
Expand Down Expand Up @@ -37,7 +37,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
DecodedJWT decodedJWT = JWTTokenProvider.verify(jwt);
int id = decodedJWT.getClaim("user_id").asInt();
String email = decodedJWT.getClaim("user_email").asString();
Account user = Account.builder().id(id).email(email).build();
User user = User.builder().id(uid).email(email).build();
CustomUserDetails customUserDetails = new CustomUserDetails(user);
Authentication authentication =
new UsernamePasswordAuthenticationToken(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.example.demo.account.Account;
import com.example.demo.user.User;
import org.springframework.stereotype.Component;

import java.util.Date;
Expand All @@ -17,7 +17,7 @@ public class JWTTokenProvider {
public static final String HEADER = "Authorization";
public static final String TOKEN_PREFIX = "Bearer "; // 스페이스 필요함

public static String create(Account account) {
public static String create(User account) {
String jwt = JWT.create()
.withSubject(account.getEmail())
.withExpiresAt(new Date(System.currentTimeMillis() + EXP))
Expand Down
Loading

0 comments on commit fc6e946

Please sign in to comment.