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 2
회원가입 및 로그인 기능 추가 #3
Open
Daniel-Taeyoun
wants to merge
4
commits into
develop
Choose a base branch
from
feature1
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
39 changes: 39 additions & 0 deletions
39
src/main/java/me/naming/onlineshoppingmall/config/RestResponseEntityExceptionHandler.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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package me.naming.onlineshoppingmall.config; | ||
|
||
import java.time.LocalDateTime; | ||
import me.naming.onlineshoppingmall.domain.ResponseData; | ||
import me.naming.onlineshoppingmall.exception.MessageException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.context.request.ServletWebRequest; | ||
import org.springframework.web.context.request.WebRequest; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
||
// @Controller or @RestController에서 발생하는 예외를 관리 할 수 있게 만들어주는 어노테이션 | ||
// @ExceptionHandler를 통해 각각의 예외처리 메세지를 원하는 형태로 작성 할 수 있다. | ||
@ControllerAdvice | ||
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler { | ||
|
||
@ExceptionHandler(value = { MessageException.class }) | ||
protected ResponseEntity handleConflict(MessageException messageException, WebRequest request) { | ||
String path = ((ServletWebRequest)request).getRequest().getRequestURI(); | ||
int code; | ||
|
||
if(messageException.getStatusCode() != 0) { | ||
code = messageException.getStatusCode(); | ||
} else { | ||
code = HttpStatus.INTERNAL_SERVER_ERROR.value(); | ||
} | ||
|
||
ResponseData responseData = ResponseData.builder() | ||
.localDateTime(LocalDateTime.now()) | ||
.statusCode(code) | ||
.path(path) | ||
.message(messageException.getMessage()) | ||
.build(); | ||
|
||
return new ResponseEntity<>(responseData, HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
} |
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
48 changes: 43 additions & 5 deletions
48
src/main/java/me/naming/onlineshoppingmall/controller/MemberController.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,17 +1,55 @@ | ||
package me.naming.onlineshoppingmall.controller; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.validation.Valid; | ||
import lombok.Getter; | ||
import lombok.NonNull; | ||
import lombok.extern.slf4j.Slf4j; | ||
import me.naming.onlineshoppingmall.domain.Member; | ||
import me.naming.onlineshoppingmall.service.MemberService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Slf4j | ||
@RestController | ||
public class MemberController { | ||
|
||
private static final Logger logger = LogManager.getLogger(MemberController.class); | ||
private final MemberService memberService; | ||
|
||
@GetMapping | ||
public void hello() { | ||
@Autowired | ||
public MemberController(MemberService memberService) { | ||
this.memberService = memberService; | ||
} | ||
|
||
// 회원가입 | ||
@PostMapping(value = "/members/signup") | ||
@ResponseStatus(HttpStatus.CREATED) | ||
public void memberJoin(@RequestBody @Valid Member member) { | ||
memberService.signUp(member); | ||
} | ||
|
||
// 회원정보 갖고오기 | ||
@GetMapping(value = "/members/{memNo}") | ||
@ResponseStatus(HttpStatus.OK) | ||
public Member getMemberInfo(@PathVariable(name = "memNo") Long memNo) { | ||
return memberService.getMemberInfo(memNo); | ||
} | ||
|
||
@GetMapping(value = "/login") | ||
@ResponseStatus(HttpStatus.OK) | ||
public void login(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) { | ||
memberService.doLogin(userLoginRequest.getEmail(), userLoginRequest.getPassword(), request); | ||
} | ||
|
||
@Getter | ||
private static class UserLoginRequest { | ||
@NonNull String email; | ||
@NonNull String password; | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
src/main/java/me/naming/onlineshoppingmall/domain/ResponseData.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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package me.naming.onlineshoppingmall.domain; | ||
|
||
import java.time.LocalDateTime; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.ToString; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Builder | ||
@ToString | ||
@Getter | ||
public class ResponseData { | ||
private LocalDateTime localDateTime; | ||
private int statusCode; | ||
private String path; | ||
private String message; | ||
|
||
public ResponseData(LocalDateTime localDateTime, int statusCode, String path, String message) { | ||
this.localDateTime = localDateTime; | ||
this.statusCode = statusCode; | ||
this.path = path; | ||
this.message = message; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/me/naming/onlineshoppingmall/exception/MessageException.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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package me.naming.onlineshoppingmall.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
public class MessageException extends RuntimeException{ | ||
|
||
private int statusCode; | ||
|
||
public MessageException(String message) { | ||
super(message); | ||
this.statusCode = HttpStatus.INTERNAL_SERVER_ERROR.value(); | ||
} | ||
|
||
public MessageException(String message, int statusCode) { | ||
super(message); | ||
this.statusCode = statusCode; | ||
} | ||
|
||
public int getStatusCode() { | ||
return statusCode; | ||
} | ||
|
||
} |
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
76 changes: 76 additions & 0 deletions
76
src/main/java/me/naming/onlineshoppingmall/service/AESServiceImpl.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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package me.naming.onlineshoppingmall.service; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.security.GeneralSecurityException; | ||
import javax.crypto.Cipher; | ||
import javax.crypto.spec.IvParameterSpec; | ||
import javax.crypto.spec.SecretKeySpec; | ||
import org.apache.commons.codec.binary.Base64; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class AESServiceImpl implements CryptoService{ | ||
|
||
@Value("${encrypt.aesKey}") | ||
private String aesKey; | ||
|
||
private static final Logger logger = LogManager.getLogger(AESServiceImpl.class); | ||
|
||
@Override | ||
public String encrypt(String plainText) { | ||
|
||
byte[] encrypted = new byte[0]; | ||
|
||
try { | ||
SecretKeySpec keySpec = getKeySpec(); | ||
|
||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | ||
cipher.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(aesKey.substring(0, 16).getBytes())); | ||
encrypted = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8.name())); | ||
|
||
} catch (GeneralSecurityException e) { | ||
logger.error("** GeneralSecurityException : {}", e); | ||
} catch (UnsupportedEncodingException e) { | ||
logger.error("** UnsupportedEncodingException : {}", e); | ||
} | ||
return new String(Base64.encodeBase64(encrypted)); | ||
} | ||
|
||
@Override | ||
public String decrypt(String encryptText) { | ||
String aesDecode = null; | ||
|
||
try { | ||
SecretKeySpec keySpec = getKeySpec(); | ||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | ||
cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(aesKey.substring(0, 16).getBytes(StandardCharsets.UTF_8.name()))); | ||
|
||
byte[] byteStr = Base64.decodeBase64(encryptText.getBytes()); | ||
aesDecode = new String(cipher.doFinal(byteStr), StandardCharsets.UTF_8.name()); | ||
|
||
} catch (Exception e) { | ||
logger.error("** Decode UnsupportedEncodingException : {}", e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 그냥 아래의 내용들과 메세지만 약간씩 다른것같은데 한번에 처리할 수 있지 않을까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
피드백 해주신 내용은 현재 |
||
} | ||
|
||
if(StringUtils.isEmpty(aesDecode)) throw new RuntimeException("복호화 에러"); | ||
return aesDecode; | ||
} | ||
|
||
private SecretKeySpec getKeySpec() throws UnsupportedEncodingException { | ||
byte[] keyBytes = new byte[16]; | ||
byte[] defaultByte = aesKey.substring(0, 16).getBytes(StandardCharsets.UTF_8.name()); | ||
|
||
int len = defaultByte.length; | ||
if (len > keyBytes.length) { | ||
len = keyBytes.length; | ||
} | ||
|
||
System.arraycopy(defaultByte, 0, keyBytes, 0, len); | ||
return new SecretKeySpec(keyBytes, "AES"); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이렇게하면 status code가 body에 들어가지 않을까요?