From 1900cc8249937577df3118232c7219008d938622 Mon Sep 17 00:00:00 2001 From: dawoon08 Date: Sun, 20 Nov 2022 01:52:27 +0900 Subject: [PATCH] #31 Feat : JWT Token --- .../demo/src/board/BoardController.java | 1 + .../example/demo/src/user/UserController.java | 4 --- .../example/demo/src/user/UserProvider.java | 7 ++--- .../example/demo/src/user/UserService.java | 6 ++--- .../demo/src/user/model/PostLoginRes.java | 5 ++-- .../demo/src/user/model/PostUserReq.java | 1 + .../demo/src/user/model/PostUserRes.java | 5 +++- .../com/example/demo/utils/JwtService.java | 26 +++++++++---------- 8 files changed, 26 insertions(+), 29 deletions(-) diff --git a/SpringBootTemplate/src/main/java/com/example/demo/src/board/BoardController.java b/SpringBootTemplate/src/main/java/com/example/demo/src/board/BoardController.java index 0488e4e..bf562bc 100644 --- a/SpringBootTemplate/src/main/java/com/example/demo/src/board/BoardController.java +++ b/SpringBootTemplate/src/main/java/com/example/demo/src/board/BoardController.java @@ -88,4 +88,5 @@ public BaseResponse modifyPostContent(@PathVariable("postIdx") int postI return new BaseResponse<>(exception.getStatus()); } } + } diff --git a/SpringBootTemplate/src/main/java/com/example/demo/src/user/UserController.java b/SpringBootTemplate/src/main/java/com/example/demo/src/user/UserController.java index 2f6f21c..eae00df 100644 --- a/SpringBootTemplate/src/main/java/com/example/demo/src/user/UserController.java +++ b/SpringBootTemplate/src/main/java/com/example/demo/src/user/UserController.java @@ -160,8 +160,6 @@ public BaseResponse getUser(@PathVariable("userIdx") int userIdx) { @PatchMapping("/{userIdx}") public BaseResponse modifyUserName(@PathVariable("userIdx") int userIdx, @RequestBody User user) { try { -/** - *********** 해당 부분은 7주차 - JWT 수업 후 주석해체 해주세요! **************** //jwt에서 idx 추출. int userIdxByJwt = jwtService.getUserIdx(); //userIdx와 접근한 유저가 같은지 확인 @@ -169,8 +167,6 @@ public BaseResponse modifyUserName(@PathVariable("userIdx") int userIdx, return new BaseResponse<>(INVALID_USER_JWT); } //같다면 유저네임 변경 - ************************************************************************** - */ PatchUserReq patchUserReq = new PatchUserReq(userIdx, user.getNickname()); userService.modifyUserName(patchUserReq); diff --git a/SpringBootTemplate/src/main/java/com/example/demo/src/user/UserProvider.java b/SpringBootTemplate/src/main/java/com/example/demo/src/user/UserProvider.java index ef980cf..3e5b380 100644 --- a/SpringBootTemplate/src/main/java/com/example/demo/src/user/UserProvider.java +++ b/SpringBootTemplate/src/main/java/com/example/demo/src/user/UserProvider.java @@ -52,11 +52,8 @@ public PostLoginRes logIn(PostLoginReq postLoginReq) throws BaseException { if (postLoginReq.getPassword().equals(password)) { //비말번호가 일치한다면 userIdx를 가져온다. int userIdx = userDao.getPwd(postLoginReq).getUserIdx(); - return new PostLoginRes(userIdx); -// *********** 해당 부분은 7주차 - JWT 수업 후 주석해제 및 대체해주세요! **************** // -// String jwt = jwtService.createJwt(userIdx); -// return new PostLoginRes(userIdx,jwt); -// ************************************************************************** + String jwt = jwtService.createJwt(userIdx); + return new PostLoginRes(userIdx, jwt); } else { // 비밀번호가 다르다면 에러메세지를 출력한다. throw new BaseException(FAILED_TO_LOGIN); diff --git a/SpringBootTemplate/src/main/java/com/example/demo/src/user/UserService.java b/SpringBootTemplate/src/main/java/com/example/demo/src/user/UserService.java index c9b971d..0f594a4 100644 --- a/SpringBootTemplate/src/main/java/com/example/demo/src/user/UserService.java +++ b/SpringBootTemplate/src/main/java/com/example/demo/src/user/UserService.java @@ -56,12 +56,12 @@ public PostUserRes createUser(PostUserReq postUserReq) throws BaseException { } try { int userIdx = userDao.createUser(postUserReq); - return new PostUserRes(userIdx); + //return new PostUserRes(userIdx); // *********** 해당 부분은 7주차 수업 후 주석해제하서 대체해서 사용해주세요! *********** // //jwt 발급. -// String jwt = jwtService.createJwt(userIdx); -// return new PostUserRes(jwt,userIdx); + String jwt = jwtService.createJwt(userIdx); + return new PostUserRes(jwt,userIdx); // ********************************************************************* } catch (Exception exception) { // DB에 이상이 있는 경우 에러 메시지를 보냅니다. throw new BaseException(DATABASE_ERROR); diff --git a/SpringBootTemplate/src/main/java/com/example/demo/src/user/model/PostLoginRes.java b/SpringBootTemplate/src/main/java/com/example/demo/src/user/model/PostLoginRes.java index 2d8198f..5e23a78 100644 --- a/SpringBootTemplate/src/main/java/com/example/demo/src/user/model/PostLoginRes.java +++ b/SpringBootTemplate/src/main/java/com/example/demo/src/user/model/PostLoginRes.java @@ -13,6 +13,7 @@ */ public class PostLoginRes { private int userIdx; -// 해당 부분은 7주차 - JWT 수업 후 주석해제 및 대체해주세요! -// private String jwt; + + // 해당 부분은 7주차 - JWT 수업 후 주석해제 및 대체해주세요! + private String jwt; } diff --git a/SpringBootTemplate/src/main/java/com/example/demo/src/user/model/PostUserReq.java b/SpringBootTemplate/src/main/java/com/example/demo/src/user/model/PostUserReq.java index fe30cf3..69b0ab7 100644 --- a/SpringBootTemplate/src/main/java/com/example/demo/src/user/model/PostUserReq.java +++ b/SpringBootTemplate/src/main/java/com/example/demo/src/user/model/PostUserReq.java @@ -10,4 +10,5 @@ public class PostUserReq { private String email; private String password; private String nickname; + private String point; } diff --git a/SpringBootTemplate/src/main/java/com/example/demo/src/user/model/PostUserRes.java b/SpringBootTemplate/src/main/java/com/example/demo/src/user/model/PostUserRes.java index 75af52d..98b7dee 100644 --- a/SpringBootTemplate/src/main/java/com/example/demo/src/user/model/PostUserRes.java +++ b/SpringBootTemplate/src/main/java/com/example/demo/src/user/model/PostUserRes.java @@ -13,6 +13,9 @@ */ public class PostUserRes { private int userIdx; + + public PostUserRes(String jwt, int userIdx) { + } // 해당 부분은 7주차 - JWT 수업 후 주석해제 및 대체해주세요! -// private String jwt; + private String jwt; } diff --git a/SpringBootTemplate/src/main/java/com/example/demo/utils/JwtService.java b/SpringBootTemplate/src/main/java/com/example/demo/utils/JwtService.java index c4701e1..6b298c5 100644 --- a/SpringBootTemplate/src/main/java/com/example/demo/utils/JwtService.java +++ b/SpringBootTemplate/src/main/java/com/example/demo/utils/JwtService.java @@ -18,12 +18,10 @@ @Service public class JwtService { + //JWT 생성 + /* @param userIdx + @return String*/ - /* - JWT 생성 - @param userIdx - @return String - */ public String createJwt(int userIdx){ Date now = new Date(); return Jwts.builder() @@ -35,20 +33,20 @@ public String createJwt(int userIdx){ .compact(); } - /* - Header에서 X-ACCESS-TOKEN 으로 JWT 추출 - @return String - */ + + //Header에서 X-ACCESS-TOKEN 으로 JWT 추출 + //@return String + public String getJwt(){ HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest(); return request.getHeader("X-ACCESS-TOKEN"); } - /* - JWT에서 userIdx 추출 - @return int - @throws BaseException - */ + + //JWT에서 userIdx 추출 + /* @return int + @throws BaseException*/ + public int getUserIdx() throws BaseException{ //1. JWT 추출 String accessToken = getJwt();