-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
141 additions
and
1 deletion.
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
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,7 @@ | ||
package com.s1350.sooljangmacha.global; | ||
|
||
public class Constants { | ||
public static final String AUTHORIZATION_HEADER = "Authorization"; | ||
public static final String BEARER_PREFIX = "bearer "; | ||
public static final String CLAIM_NAME = "userId"; | ||
} |
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
11 changes: 11 additions & 0 deletions
11
src/main/java/com/s1350/sooljangmacha/global/resolver/UserAccount.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,11 @@ | ||
package com.s1350.sooljangmacha.global.resolver; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.PARAMETER) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface UserAccount { | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/s1350/sooljangmacha/global/resolver/UserAccountResolver.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,46 @@ | ||
package com.s1350.sooljangmacha.global.resolver; | ||
|
||
import com.s1350.sooljangmacha.global.exception.BaseException; | ||
import com.s1350.sooljangmacha.global.exception.BaseResponseCode; | ||
import com.s1350.sooljangmacha.global.utils.JwtUtil; | ||
import com.s1350.sooljangmacha.user.entity.User; | ||
import com.s1350.sooljangmacha.user.repository.UserRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.StringUtils; | ||
import org.springframework.web.bind.support.WebDataBinderFactory; | ||
import org.springframework.web.context.request.NativeWebRequest; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
import org.springframework.web.method.support.ModelAndViewContainer; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.validation.constraints.NotNull; | ||
|
||
import static com.s1350.sooljangmacha.global.Constants.AUTHORIZATION_HEADER; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class UserAccountResolver implements HandlerMethodArgumentResolver { | ||
|
||
private final JwtUtil jwtUtil; | ||
private final UserRepository userRepository; | ||
|
||
@Override | ||
public boolean supportsParameter(MethodParameter parameter) { | ||
return parameter.hasParameterAnnotation(UserAccount.class) && User.class.equals(parameter.getParameterType()); | ||
} | ||
|
||
@Override | ||
public User resolveArgument(@NotNull MethodParameter parameter, ModelAndViewContainer modelAndViewContainer, @NotNull NativeWebRequest webRequest, WebDataBinderFactory binderFactory) { | ||
final HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest(); | ||
String header = request.getHeader(AUTHORIZATION_HEADER); | ||
|
||
if (!StringUtils.hasText(header)) throw new BaseException(BaseResponseCode.NULL_TOKEN); | ||
final String token = JwtUtil.replaceBearer(header); | ||
|
||
if (!jwtUtil.validateToken(token)) throw new BaseException(BaseResponseCode.INVALID_TOKEN); | ||
return userRepository.findByIdAndIsEnable(jwtUtil.getJwtContents(token), true).orElseThrow(() -> new BaseException(BaseResponseCode.USER_NOT_FOUND)); | ||
} | ||
} | ||
|
60 changes: 60 additions & 0 deletions
60
src/main/java/com/s1350/sooljangmacha/global/utils/JwtUtil.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,60 @@ | ||
package com.s1350.sooljangmacha.global.utils; | ||
|
||
import com.s1350.sooljangmacha.global.exception.BaseException; | ||
import com.s1350.sooljangmacha.global.exception.BaseResponseCode; | ||
import io.jsonwebtoken.*; | ||
import io.jsonwebtoken.security.Keys; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.security.Key; | ||
|
||
import static com.s1350.sooljangmacha.global.Constants.BEARER_PREFIX; | ||
import static com.s1350.sooljangmacha.global.Constants.CLAIM_NAME; | ||
|
||
@Component | ||
public class JwtUtil { | ||
|
||
@Value("${jwt.secret}") | ||
private String jwtSecret; | ||
|
||
public static String replaceBearer(String header) { | ||
return header.substring(BEARER_PREFIX.length()); | ||
} | ||
|
||
public boolean validateToken(String token) { | ||
try { | ||
getBody(token); | ||
return true; | ||
} catch (io.jsonwebtoken.security.SecurityException e) { | ||
throw new BaseException(BaseResponseCode.INVALID_TOKEN); | ||
} catch (MalformedJwtException e) { | ||
throw new BaseException(BaseResponseCode.MALFORMED_TOKEN); | ||
} catch (ExpiredJwtException e) { | ||
throw new BaseException(BaseResponseCode.EXPIRED_TOKEN); | ||
} catch (UnsupportedJwtException e) { | ||
throw new BaseException(BaseResponseCode.UNSUPPORTED_TOKEN); | ||
} catch (IllegalArgumentException e) { | ||
throw new BaseException(BaseResponseCode.NULL_TOKEN); | ||
} | ||
} | ||
|
||
private Key getSigningKey() { | ||
final byte[] keyBytes = jwtSecret.getBytes(StandardCharsets.UTF_8); | ||
return Keys.hmacShaKeyFor(keyBytes); | ||
} | ||
|
||
public Long getJwtContents(String token) { | ||
String userId = String.valueOf(getBody(token).get(CLAIM_NAME)); | ||
return Long.parseLong(userId); | ||
} | ||
|
||
private Claims getBody(String token) { | ||
try { | ||
return Jwts.parserBuilder().setSigningKey(getSigningKey()).build().parseClaimsJws(token).getBody(); | ||
} catch (ExpiredJwtException e) { | ||
return e.getClaims(); | ||
} | ||
} | ||
} |
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