Skip to content

Commit

Permalink
Merge pull request #124 from twenty-three-23/feature/TT-162-userid-to…
Browse files Browse the repository at this point in the history
…-token

TT-162 userid to token
  • Loading branch information
snacktime81 authored Jul 3, 2024
2 parents cb04793 + aeca63a commit 8010a9b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package com.twentythree.peech.auth.config;

import com.twentythree.peech.auth.resolver.AuthArgumentResolver;
import com.twentythree.peech.common.utils.JWTUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@RequiredArgsConstructor
@Configuration
public class ArgumentResolverConfig implements WebMvcConfigurer {

private final JWTUtils jwtUtils;

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(new AuthArgumentResolver());
resolvers.add(new AuthArgumentResolver(jwtUtils));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import com.twentythree.peech.auth.dto.LoginUserId;
import com.twentythree.peech.auth.dto.UserIdDTO;
import com.twentythree.peech.common.utils.JWTUtils;
import io.jsonwebtoken.JwtException;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
Expand All @@ -12,8 +15,11 @@
import org.springframework.web.method.support.ModelAndViewContainer;

@Slf4j
@RequiredArgsConstructor
public class AuthArgumentResolver implements HandlerMethodArgumentResolver {

private final JWTUtils jwtUtils;

@Override
public boolean supportsParameter(MethodParameter parameter) {
log.info("supportsParameter 시작");
Expand All @@ -22,22 +28,32 @@ public boolean supportsParameter(MethodParameter parameter) {

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();
try {

Cookie[] cookies = request.getCookies();
if (cookies == null) {
throw new IllegalArgumentException("로그인을 다시 해주세요");
}

Long userId = null;
for (Cookie cookie : cookies) {
if (cookie.getName().equals("userId")) {
userId = Long.parseLong(cookie.getValue());
HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();

Cookie[] cookies = request.getCookies();
if (cookies == null) {
throw new IllegalArgumentException("로그인을 다시 해주세요");
}
}

if (userId == null) {throw new IllegalArgumentException("cookie의 userId가 잘 못 되었습니다");}
Long userId = null;
for (Cookie cookie : cookies) {
if (cookie.getName().equals("LoginToken")) {
String token = cookie.getValue();

return new UserIdDTO(userId);
userId = Long.parseLong(jwtUtils.parseJWT(token).getPayload().get("userId").toString());
}
}

if (userId == null) {
throw new IllegalArgumentException("cookie의 userId가 잘 못 되었습니다");
}

return new UserIdDTO(userId);
} catch (JwtException e) {
throw e;
}
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/twentythree/peech/common/utils/JWTUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.twentythree.peech.common.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;
Expand Down Expand Up @@ -35,4 +37,14 @@ public String createJWT(Long userId) {
compact();
}

public Jws<Claims> parseJWT(String token) {

Jws<Claims> claimsJws = Jwts.parser()
.verifyWith(secretKey)
.build()
.parseSignedClaims(token);

return claimsJws;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.twentythree.peech.script.service;

import com.twentythree.peech.script.domain.ThemeEntity;
import com.twentythree.peech.script.dto.response.ThemesResponseDTO;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -23,10 +24,10 @@ class ThemeServiceTest {
Long userId = 1L;

//When
List<ThemeEntity> themesByUserId = themeService.getThemesByUserId(userId);
ThemesResponseDTO themesByUserId = themeService.getThemesByUserId(userId);

//Then
assertThat(themesByUserId.size()).isEqualTo(1);
assertThat(themesByUserId.getThemes().size()).isEqualTo(1);

}
}

0 comments on commit 8010a9b

Please sign in to comment.