Skip to content

Commit

Permalink
Fix/token parsing error (#211)
Browse files Browse the repository at this point in the history
* fix: token parsing error

* refactor: 중복제거
  • Loading branch information
sypark9646 authored Jan 1, 2021
1 parent 85ade0e commit 4676f7a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 20 deletions.
30 changes: 13 additions & 17 deletions login-service/src/main/java/com/yapp/crew/config/JwtUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.yapp.crew.config;

import com.yapp.crew.domain.errors.TokenRequiredException;
import com.yapp.crew.domain.model.User;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
Expand Down Expand Up @@ -56,28 +57,21 @@ private Date generateExpirationDate(Date now) {
return calendar.getTime();
}

public String getUserIdFromToken(String token) {
token = token.replace(prefix + " ", "");
log.info("remove prefix token: " + token);

String userId;
public long getUserIdFromToken(String token) {
try {
final Claims claims = getClaimsFromToken(token);
userId = String.valueOf(claims.get("userId"));
final Claims claims = getClaimsFromToken(token).getBody();
long userId = Long.parseLong(String.valueOf(claims.get("userId")));
log.info("userId from token: " + userId);
return userId;
} catch (Exception e) {
userId = null;
throw new TokenRequiredException("[Auto Login Service] Token is required but wasn't sent");
}
return userId;
}

public Date getExpirationFromToken(String token) {
token = token.replace(prefix + " ", "");
log.info("remove prefix token: " + token);

Date expiration;
try {
final Claims claims = getClaimsFromToken(token);
final Claims claims = getClaimsFromToken(token).getBody();
expiration = claims.getExpiration();
log.info("expiration from token: " + expiration.toString());
} catch (Exception e) {
Expand All @@ -86,14 +80,16 @@ public Date getExpirationFromToken(String token) {
return expiration;
}

private Claims getClaimsFromToken(String token) {
Claims claims;
private Jws<Claims> getClaimsFromToken(String token) {
token = token.replace(prefix + " ", "");
log.info("remove prefix token: " + token);

Jws<Claims> claims;
try {
claims = Jwts.parserBuilder()
.setSigningKey(Keys.hmacShaKeyFor(secret.getBytes()))
.build()
.parseClaimsJws(token)
.getBody();
.parseClaimsJws(token);
} catch (Exception e) {
claims = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public UserAuthResponse signIn(LoginUserInfo loginUserInfo) {
}

public UserAuthResponse autoSignIn(String token) {
long userId = Long.parseLong(jwtUtils.getUserIdFromToken(token));
long userId = jwtUtils.getUserIdFromToken(token);

User user = getUserByUserId(userId)
.orElseThrow(() -> new UserNotFoundException(userId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public HttpHeaders setToken(User user) {
public HttpHeaders refreshToken(User user, String token) {
String refreshToken = null;
if (jwtUtil.verifyToken(token)) {
refreshToken = token;
refreshToken = "Bearer " + token;
} else {
refreshToken = jwtUtil.createToken(user);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public WithdrawService(UserRepository userRepository, JwtUtils jwtUtils) {

@Transactional
public UserAuthResponse withdraw(String token) {
long userId = Long.parseLong(jwtUtils.getUserIdFromToken(token));
long userId = jwtUtils.getUserIdFromToken(token);

User user = getUserByUserId(userId)
.orElseThrow(() -> new UserNotFoundException(userId));
Expand Down

0 comments on commit 4676f7a

Please sign in to comment.