-
Notifications
You must be signed in to change notification settings - Fork 63
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
[Spring MVC (인증)] 주민기 미션 제출합니다. #123
base: mingking2
Are you sure you want to change the base?
Changes from all commits
3b13df3
2aae3ea
151917d
8d7e981
48ab6ed
95e8cf9
569fd5b
74012f3
2211751
6d1f017
3817306
1f6c0ac
f564284
67b7081
c263b83
b4535a3
c4faf31
6ecdcf5
f2c62f4
294288a
9a7457e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package roomescape.auth; | ||
package roomescape.auth.dto.request; | ||
|
||
public record LoginRequest ( | ||
String email, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package roomescape.auth.dto.response; | ||
|
||
|
||
public record LoginMember( | ||
Long id, | ||
String name, | ||
String email, | ||
String role | ||
) { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package roomescape.auth.jwt; | ||
package roomescape.auth.dto.response; | ||
|
||
public record MemberTokenDto ( | ||
Long id, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package roomescape.auth; | ||
package roomescape.auth.dto.response; | ||
|
||
public record TokenResponse ( | ||
String token | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package roomescape.common.config; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
import roomescape.common.interceptor.AdminInterceptor; | ||
import roomescape.common.resolver.LoginMemberArgumentResolver; | ||
|
||
@Configuration | ||
public class WebConfig implements WebMvcConfigurer { | ||
|
||
private final LoginMemberArgumentResolver loginMemberArgumentResolver; | ||
private final AdminInterceptor adminInterceptor; | ||
|
||
public WebConfig(LoginMemberArgumentResolver loginMemberArgumentResolver, AdminInterceptor adminInterceptor) { | ||
this.loginMemberArgumentResolver = loginMemberArgumentResolver; | ||
this.adminInterceptor = adminInterceptor; | ||
} | ||
|
||
@Override | ||
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) { | ||
argumentResolvers.add(loginMemberArgumentResolver); | ||
} | ||
|
||
@Override | ||
public void addInterceptors(InterceptorRegistry registry) { | ||
registry.addInterceptor(adminInterceptor) | ||
.addPathPatterns("/admin/**") | ||
.order(1); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package roomescape.common.interceptor; | ||
|
||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
import jakarta.servlet.http.Cookie; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import roomescape.auth.dto.response.MemberTokenDto; | ||
import roomescape.auth.service.TokenService; | ||
import roomescape.auth.util.CookieProvider; | ||
import roomescape.member.domain.Member; | ||
import roomescape.member.service.MemberService; | ||
|
||
@Component | ||
public class AdminInterceptor implements HandlerInterceptor { | ||
private CookieProvider cookieProvider; | ||
private TokenService tokenService; | ||
private MemberService memberService; | ||
|
||
public AdminInterceptor(CookieProvider cookieProvider, TokenService tokenService, MemberService memberService) { | ||
this.cookieProvider = cookieProvider; | ||
this.tokenService = tokenService; | ||
this.memberService = memberService; | ||
} | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws | ||
Exception { | ||
|
||
Cookie[] cookies = request.getCookies(); | ||
String token = cookieProvider.extractTokenFromCookie(cookies) | ||
.orElse(null); | ||
|
||
if (token == null) { | ||
response.sendError(HttpServletResponse.SC_BAD_REQUEST); | ||
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. sendError 를 사용했네요. setStatus 랑 차이점이 뭔가요? |
||
return false; | ||
} | ||
|
||
MemberTokenDto memberTokenDto = tokenService.extractMemberResponseFromToken(token); | ||
Member member = memberService.findMemberByName(memberTokenDto.name()); | ||
if (member == null || !member.getRole().equals("ADMIN")) { | ||
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. ADMIN 인지 Member 가 스스로 알려줘도 괜찮을거 같아요. |
||
response.sendError(HttpServletResponse.SC_UNAUTHORIZED); | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package roomescape.common.resolver; | ||
|
||
import org.springframework.core.MethodParameter; | ||
import org.springframework.stereotype.Component; | ||
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 jakarta.servlet.http.Cookie; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import roomescape.auth.dto.response.MemberTokenDto; | ||
import roomescape.auth.service.TokenService; | ||
import roomescape.auth.util.CookieProvider; | ||
import roomescape.auth.dto.response.LoginMember; | ||
import roomescape.member.domain.Member; | ||
import roomescape.member.service.MemberService; | ||
|
||
@Component | ||
public class LoginMemberArgumentResolver implements HandlerMethodArgumentResolver { | ||
|
||
private MemberService memberService; | ||
private TokenService tokenService; | ||
private CookieProvider cookieProvider; | ||
|
||
public LoginMemberArgumentResolver(MemberService memberService, TokenService tokenService, CookieProvider cookieProvider) { | ||
this.memberService = memberService; | ||
this.tokenService = tokenService; | ||
this.cookieProvider = cookieProvider; | ||
} | ||
|
||
@Override | ||
public boolean supportsParameter(MethodParameter parameter) { | ||
return parameter.getParameterType().equals(LoginMember.class); | ||
} | ||
|
||
@Override | ||
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, | ||
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) { | ||
HttpServletRequest httpServletRequest = (HttpServletRequest) webRequest.getNativeRequest(); | ||
Cookie[] cookies = httpServletRequest.getCookies(); | ||
String token = cookieProvider.extractTokenFromCookie(cookies) | ||
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. 위 Interceptor 에서는 Null 을 받아 처리했고, 여기선 에외를 던졌네요. |
||
.orElseThrow(() -> new IllegalArgumentException("쿠키에 토큰이 존재하지 않습니다.")); | ||
|
||
MemberTokenDto memberTokenDto = tokenService.extractMemberResponseFromToken(token); | ||
Member member = memberService.findMemberByName(memberTokenDto.name()); | ||
return new LoginMember(member.getId(), member.getName(), member.getEmail(), member.getRole()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package roomescape.common.util; | ||
|
||
import java.util.Date; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class DefaultTimeProvider implements TimeProvider { | ||
@Override | ||
public Date now() { | ||
return new Date(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package roomescape.common.util; | ||
|
||
import java.util.Date; | ||
|
||
public interface TimeProvider { | ||
Date now(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package roomescape.member; | ||
package roomescape.member.domain; | ||
|
||
public class Member { | ||
private Long id; | ||
|
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.
Interceptor 는 왜 boolean 을 return 하게 하는거 같나요?