This repository has been archived by the owner on Aug 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* style: 컨벤션에 맞게 코드 정리 * chore: indents를 tab -> space로 설정 변경 * docs: readme.md
- Loading branch information
Showing
28 changed files
with
616 additions
and
547 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,27 @@ | ||
package com.delivery.aop; | ||
|
||
import com.delivery.exception.InvalidAuthenticationException; | ||
import com.delivery.user.AuthenticationHolder; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Before; | ||
import org.aspectj.lang.annotation.Pointcut; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.delivery.exception.InvalidAuthenticationException; | ||
import com.delivery.user.AuthenticationHolder; | ||
|
||
|
||
@Aspect | ||
@Component | ||
public class AuthenticationAop { | ||
|
||
@Pointcut("@annotation(com.delivery.aop.AuthenticationRequired)") | ||
public void pointcut() { | ||
} | ||
|
||
@Before("pointcut()") | ||
public void authenticate() { | ||
if (!AuthenticationHolder.hasAuthentication()) { | ||
throw new InvalidAuthenticationException(); | ||
} | ||
} | ||
|
||
@Pointcut("@annotation(com.delivery.aop.AuthenticationRequired)") | ||
public void pointcut() { | ||
} | ||
@Before("pointcut()") | ||
public void authenticate() { | ||
if (!AuthenticationHolder.hasAuthentication()) { | ||
throw new InvalidAuthenticationException(); | ||
} | ||
} | ||
} |
51 changes: 30 additions & 21 deletions
51
src/main/java/com/delivery/config/AuthenticationHolderFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,40 @@ | ||
package com.delivery.config; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.servlet.Filter; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpSession; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import com.delivery.exception.InvalidAuthenticationException; | ||
import com.delivery.user.Authentication; | ||
import com.delivery.user.AuthenticationHolder; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.servlet.*; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpSession; | ||
import java.io.IOException; | ||
|
||
|
||
@Component | ||
public class AuthenticationHolderFilter implements Filter { | ||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { | ||
HttpSession session = ((HttpServletRequest) request).getSession(); | ||
try { | ||
Authentication auth = (Authentication) session.getAttribute("auth"); | ||
AuthenticationHolder.setAuthentication(auth); | ||
} catch (ClassCastException ex) { | ||
throw new InvalidAuthenticationException(); | ||
} | ||
chain.doFilter(request, response); | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
AuthenticationHolder.reset(); | ||
} | ||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) | ||
throws IOException, ServletException { | ||
HttpSession session = ((HttpServletRequest) request).getSession(); | ||
try { | ||
Authentication auth = (Authentication) session.getAttribute("auth"); | ||
AuthenticationHolder.setAuthentication(auth); | ||
} catch (ClassCastException ex) { | ||
throw new InvalidAuthenticationException(); | ||
} | ||
chain.doFilter(request, response); | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
AuthenticationHolder.reset(); | ||
} | ||
} |
69 changes: 35 additions & 34 deletions
69
src/main/java/com/delivery/config/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,48 @@ | ||
package com.delivery.config; | ||
|
||
import com.delivery.exception.InvalidAuthenticationException; | ||
import com.delivery.exception.PasswordAuthenticationException; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
import org.springframework.dao.DuplicateKeyException; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.FieldError; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import com.delivery.exception.InvalidAuthenticationException; | ||
import com.delivery.exception.PasswordAuthenticationException; | ||
|
||
@RestControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
public ResponseEntity<String> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException ex) { | ||
Map<String, String> errors = new LinkedHashMap<>(); | ||
errors.put("errorLength", String.valueOf(ex.getFieldErrors().size())); | ||
for (FieldError error : ex.getFieldErrors()) { | ||
errors.put(error.getField(), error.getDefaultMessage()); | ||
} | ||
return ResponseEntity.badRequest().body(errors.toString()); | ||
} | ||
|
||
@ExceptionHandler(DuplicateKeyException.class) | ||
public ResponseEntity<String> duplicateKeyExceptionHandler(DuplicateKeyException ex) { | ||
return ResponseEntity.badRequest().body("email already exists - " + ex.getMessage()); | ||
} | ||
|
||
@ExceptionHandler(IllegalArgumentException.class) | ||
public ResponseEntity<String> illegalArgumentExceptionHandler(IllegalArgumentException ex) { | ||
return ResponseEntity.badRequest().body(ex.getMessage()); | ||
} | ||
|
||
@ExceptionHandler(InvalidAuthenticationException.class) | ||
public ResponseEntity<String> invalidAuthenticationExceptionHandler(InvalidAuthenticationException ex) { | ||
return ResponseEntity.badRequest().build(); | ||
} | ||
|
||
@ExceptionHandler(PasswordAuthenticationException.class) | ||
public ResponseEntity<String> PasswordAuthenticationExceptionHandler(PasswordAuthenticationException ex) { | ||
return ResponseEntity.badRequest().body("password not match - " + ex.getMessage()); | ||
} | ||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
public ResponseEntity<String> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException ex) { | ||
Map<String, String> errors = new LinkedHashMap<>(); | ||
errors.put("errorLength", String.valueOf(ex.getFieldErrors().size())); | ||
for (FieldError error : ex.getFieldErrors()) { | ||
errors.put(error.getField(), error.getDefaultMessage()); | ||
} | ||
return ResponseEntity.badRequest().body(errors.toString()); | ||
} | ||
@ExceptionHandler(DuplicateKeyException.class) | ||
public ResponseEntity<String> duplicateKeyExceptionHandler(DuplicateKeyException ex) { | ||
return ResponseEntity.badRequest().body("email already exists - " + ex.getMessage()); | ||
} | ||
@ExceptionHandler(IllegalArgumentException.class) | ||
public ResponseEntity<String> illegalArgumentExceptionHandler(IllegalArgumentException ex) { | ||
return ResponseEntity.badRequest().body(ex.getMessage()); | ||
} | ||
@ExceptionHandler(InvalidAuthenticationException.class) | ||
public ResponseEntity<String> invalidAuthenticationExceptionHandler(InvalidAuthenticationException ex) { | ||
return ResponseEntity.badRequest().build(); | ||
} | ||
@ExceptionHandler(PasswordAuthenticationException.class) | ||
public ResponseEntity<String> passwordAuthenticationExceptionHandler(PasswordAuthenticationException ex) { | ||
return ResponseEntity.badRequest().body("password not match - " + ex.getMessage()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,18 @@ | ||
package com.delivery.config; | ||
|
||
import com.delivery.user.UserRepository; | ||
import com.delivery.user.UserRepositoryHashMap; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Profile; | ||
|
||
import com.delivery.user.UserRepository; | ||
import com.delivery.user.UserRepositoryHashMap; | ||
|
||
@Configuration | ||
@Profile("dev") | ||
public class RepositoryConfigDev { | ||
|
||
@Bean | ||
public UserRepository userRepository() { | ||
return new UserRepositoryHashMap(); | ||
} | ||
} | ||
@Bean | ||
public UserRepository userRepository() { | ||
return new UserRepositoryHashMap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
package com.delivery.user; | ||
|
||
public class Authentication { | ||
private String email; | ||
private String nickname; | ||
private String phone; | ||
|
||
public Authentication(String email, String nickname, String phone) { | ||
this.email = email; | ||
this.nickname = nickname; | ||
this.phone = phone; | ||
} | ||
private String email; | ||
private String nickname; | ||
private String phone; | ||
public Authentication(String email, String nickname, String phone) { | ||
this.email = email; | ||
this.nickname = nickname; | ||
this.phone = phone; | ||
} | ||
} |
34 changes: 17 additions & 17 deletions
34
src/main/java/com/delivery/user/AuthenticationHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
package com.delivery.user; | ||
|
||
public class AuthenticationHolder { | ||
private static ThreadLocal<Authentication> local = new ThreadLocal<>(); | ||
|
||
public static boolean hasAuthentication() { | ||
return local.get() != null; | ||
} | ||
|
||
public static Authentication getAuthentication() { | ||
return local.get(); | ||
} | ||
|
||
public static void setAuthentication(Authentication auth) { | ||
local.set(auth); | ||
} | ||
|
||
public static void reset() { | ||
local.remove(); | ||
} | ||
private static ThreadLocal<Authentication> local = new ThreadLocal<>(); | ||
public static boolean hasAuthentication() { | ||
return local.get() != null; | ||
} | ||
public static Authentication getAuthentication() { | ||
return local.get(); | ||
} | ||
public static void setAuthentication(Authentication auth) { | ||
local.set(auth); | ||
} | ||
public static void reset() { | ||
local.remove(); | ||
} | ||
} |
Oops, something went wrong.