-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JWT 리프레시 토큰 구현 #21
Open
eunjee
wants to merge
2
commits into
MentosTeam:develop
Choose a base branch
from
eunjee:feat/jwt
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
JWT 리프레시 토큰 구현 #21
Changes from all commits
Commits
Show all changes
2 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
43 changes: 43 additions & 0 deletions
43
src/main/java/MentosServer/mentos/controller/JwtController.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,43 @@ | ||
package MentosServer.mentos.controller; | ||
|
||
import MentosServer.mentos.config.BaseException; | ||
import MentosServer.mentos.config.BaseResponse; | ||
import MentosServer.mentos.model.dto.TokenRes; | ||
import MentosServer.mentos.utils.JwtService; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import static MentosServer.mentos.config.BaseResponseStatus.FAILED_TO_LOGOUT; | ||
import static MentosServer.mentos.config.BaseResponseStatus.SUCCESS_LOGOUT; | ||
|
||
@RestController | ||
public class JwtController { | ||
private final JwtService jwtService; | ||
|
||
public JwtController(JwtService jwtService) { | ||
this.jwtService = jwtService; | ||
} | ||
|
||
/* | ||
새로운 액세스 토큰 혹은 리프레시 토큰이 필요한 경우 | ||
*/ | ||
@PostMapping("/accessToken") | ||
public BaseResponse<TokenRes> createRefreshToken() throws BaseException { | ||
return new BaseResponse<>(jwtService.checkRefreshJwt()); | ||
} | ||
/* | ||
로그아웃 | ||
*/ | ||
@GetMapping("/log-out") | ||
public BaseResponse logOut() throws BaseException { | ||
//리프레시 토큰 버리고 | ||
try { | ||
jwtService.logOut(); | ||
//반환 | ||
return new BaseResponse<>(SUCCESS_LOGOUT); | ||
}catch(Exception e){ | ||
return new BaseResponse<>(FAILED_TO_LOGOUT); | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -18,9 +18,8 @@ public class Member { | |
private String memberEmail; | ||
private String memberPw; | ||
private int memberSchoolId; | ||
private int memberMajorId; | ||
private String memberMajor; | ||
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. 이건 저번 PR에서 수정했던것 같은데 아직 머지가 안됐나보네요.. ㅠㅠ |
||
private String memberSex; | ||
private String memberImage; | ||
private int memberMentos; | ||
private String memberStatus; | ||
private Timestamp memberCreateAt; | ||
|
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 |
---|---|---|
|
@@ -11,4 +11,5 @@ | |
public class PostLoginRes { | ||
private int memberId; | ||
private String jwt; | ||
private String refreshJwt; | ||
} |
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 |
---|---|---|
|
@@ -8,5 +8,6 @@ | |
public class SignUpRes { | ||
private int memberId; | ||
private String memberJwt; | ||
private String refreshJwt; | ||
|
||
} |
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,9 @@ | ||
package MentosServer.mentos.model.dto; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class TokenRes { | ||
private final String memberJwt; | ||
private final String refreshJwt; | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/MentosServer/mentos/repository/JwtRepository.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,38 @@ | ||
package MentosServer.mentos.repository; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import javax.sql.DataSource; | ||
|
||
@Repository | ||
public class JwtRepository { | ||
private JdbcTemplate jdbcTemplate; | ||
|
||
@Autowired | ||
public void setDataSource(DataSource dataSource) { | ||
this.jdbcTemplate = new JdbcTemplate(dataSource); | ||
} | ||
|
||
public void createRefreshToken(int memberId,String refreshToken){ | ||
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. DB에 JWT 테이블을 새로 생성한건가요?? |
||
String insertQuery = "insert into JWT (memberId, refreshToken) VALUES (?,?)"; | ||
Object[] params = new Object[]{memberId,refreshToken}; | ||
this.jdbcTemplate.update(insertQuery,params); | ||
} | ||
public void updateRefreshToken(int memberId, String refreshToken){ | ||
String updateQuery = "update JWT set refreshToken = ?,updateAt = CURRENT_TIMESTAMP() where memberId=?"; | ||
Object[] params = new Object[]{refreshToken,memberId}; | ||
this.jdbcTemplate.update(updateQuery,params); | ||
} | ||
//로그아웃 | ||
public void deleteRefreshToken(int memberId){ | ||
String deleteQuery = "update JWT set refreshToken= null,updateAt = CURRENT_TIMESTAMP()where memberId=?"; | ||
this.jdbcTemplate.update(deleteQuery,memberId); | ||
} | ||
//멤버Id 얻어오기 | ||
public int getMemberId(String refreshToken){ | ||
String getQuery = "select memberId from JWT where refreshToken=?"; | ||
return this.jdbcTemplate.queryForObject(getQuery,int.class,refreshToken); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -39,7 +39,6 @@ public SignUpRes createMember(SignUpReq signUpReq) throws BaseException { | |
try { | ||
// 암호화: signUpReq에서 제공받은 비밀번호를 보안을 위해 암호화시켜 DB에 저장합니다. | ||
// ex) password123 -> dfhsjfkjdsnj4@[email protected] | ||
logger.info("암호화 로직"); | ||
pwd = new AES128(Secret.USER_INFO_PASSWORD_KEY).encrypt(signUpReq.getMemberPw()); // 암호화코드 | ||
signUpReq.setMemberPw(pwd); | ||
} catch (Exception ignored) { // 암호화가 실패하였을 경우 에러 발생 | ||
|
@@ -48,9 +47,9 @@ public SignUpRes createMember(SignUpReq signUpReq) throws BaseException { | |
//실제 비즈니스 로직 | ||
try { | ||
int memberId = signUpRepository.createMember(signUpReq); | ||
logger.info("jwt발급"); | ||
String memberJwt = jwtService.createJwt(memberId); | ||
return new SignUpRes(memberId,memberJwt); | ||
String refreshToken = jwtService.createRefreshToken(memberId);//리프레시 토큰 생성 | ||
return new SignUpRes(memberId,memberJwt,refreshToken); | ||
} catch (Exception exception) { // DB에 이상이 있는 경우 에러 메시지를 보냅니다. | ||
throw new BaseException(DATABASE_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
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.
안드단에서 리프레시 토큰을 요청하는 거군요! 😀