Skip to content

Commit

Permalink
UMC-EWHA#31 Feat : JWT Token
Browse files Browse the repository at this point in the history
  • Loading branch information
dawoon08 committed Nov 19, 2022
1 parent 90d353e commit 1900cc8
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,5 @@ public BaseResponse<String> modifyPostContent(@PathVariable("postIdx") int postI
return new BaseResponse<>(exception.getStatus());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,13 @@ public BaseResponse<GetUserRes> getUser(@PathVariable("userIdx") int userIdx) {
@PatchMapping("/{userIdx}")
public BaseResponse<String> modifyUserName(@PathVariable("userIdx") int userIdx, @RequestBody User user) {
try {
/**
*********** 해당 부분은 7주차 - JWT 수업 후 주석해체 해주세요! ****************
//jwt에서 idx 추출.
int userIdxByJwt = jwtService.getUserIdx();
//userIdx와 접근한 유저가 같은지 확인
if(userIdx != userIdxByJwt){
return new BaseResponse<>(INVALID_USER_JWT);
}
//같다면 유저네임 변경
**************************************************************************
*/
PatchUserReq patchUserReq = new PatchUserReq(userIdx, user.getNickname());
userService.modifyUserName(patchUserReq);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
public class PostLoginRes {
private int userIdx;
// 해당 부분은 7주차 - JWT 수업 후 주석해제 및 대체해주세요!
// private String jwt;

// 해당 부분은 7주차 - JWT 수업 후 주석해제 및 대체해주세요!
private String jwt;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public class PostUserReq {
private String email;
private String password;
private String nickname;
private String point;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
*/
public class PostUserRes {
private int userIdx;

public PostUserRes(String jwt, int userIdx) {
}
// 해당 부분은 7주차 - JWT 수업 후 주석해제 및 대체해주세요!
// private String jwt;
private String jwt;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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();
Expand Down

0 comments on commit 1900cc8

Please sign in to comment.