-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #172 from woowacourse-teams/refactor/164-login-user
#164 컨트롤러에서 인증된 유저를 구하는 코드 리팩토링하기
- Loading branch information
Showing
10 changed files
with
190 additions
and
108 deletions.
There are no files selected for viewing
19 changes: 15 additions & 4 deletions
19
src/main/java/com/saebyeok/saebyeok/config/WebMvcConfig.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 |
---|---|---|
@@ -1,23 +1,34 @@ | ||
package com.saebyeok.saebyeok.config; | ||
|
||
import com.saebyeok.saebyeok.controller.resolver.LoginMemberArgumentResolver; | ||
import com.saebyeok.saebyeok.interceptor.LogInterceptor; | ||
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 java.util.List; | ||
|
||
@Configuration | ||
public class WebMvcConfig implements WebMvcConfigurer { | ||
private final LogInterceptor logInterceptor; | ||
private final LoginMemberArgumentResolver loginMemberArgumentResolver; | ||
|
||
public WebMvcConfig(LogInterceptor logInterceptor) { | ||
public WebMvcConfig(LogInterceptor logInterceptor, LoginMemberArgumentResolver loginMemberArgumentResolver) { | ||
this.logInterceptor = logInterceptor; | ||
this.loginMemberArgumentResolver = loginMemberArgumentResolver; | ||
} | ||
|
||
@Override | ||
public void addInterceptors(InterceptorRegistry registry) { | ||
registry.addInterceptor(logInterceptor).addPathPatterns("/**") | ||
.excludePathPatterns("/css/**") | ||
.excludePathPatterns("/js/**") | ||
.excludePathPatterns("/index.html"); | ||
.excludePathPatterns("/css/**") | ||
.excludePathPatterns("/js/**") | ||
.excludePathPatterns("/index.html"); | ||
} | ||
|
||
@Override | ||
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) { | ||
resolvers.add(loginMemberArgumentResolver); | ||
} | ||
} |
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
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
11 changes: 11 additions & 0 deletions
11
src/main/java/com/saebyeok/saebyeok/controller/resolver/LoginMember.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.saebyeok.saebyeok.controller.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 LoginMember { | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/saebyeok/saebyeok/controller/resolver/LoginMemberArgumentResolver.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,34 @@ | ||
package com.saebyeok.saebyeok.controller.resolver; | ||
|
||
import com.saebyeok.saebyeok.security.User; | ||
import com.saebyeok.saebyeok.service.MemberService; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
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; | ||
|
||
@Component | ||
public class LoginMemberArgumentResolver implements HandlerMethodArgumentResolver { | ||
private final MemberService memberService; | ||
|
||
public LoginMemberArgumentResolver(MemberService memberService) { | ||
this.memberService = memberService; | ||
} | ||
|
||
@Override | ||
public boolean supportsParameter(MethodParameter parameter) { | ||
return parameter.hasParameterAnnotation(LoginMember.class); | ||
} | ||
|
||
@Override | ||
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, | ||
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) { | ||
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); | ||
User user = (User) principal; | ||
|
||
return memberService.findById(user.getId()); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/saebyeok/saebyeok/service/MemberService.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,18 @@ | ||
package com.saebyeok.saebyeok.service; | ||
|
||
import com.saebyeok.saebyeok.domain.Member; | ||
import com.saebyeok.saebyeok.domain.MemberRepository; | ||
import com.saebyeok.saebyeok.exception.MemberNotFoundException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class MemberService { | ||
private final MemberRepository memberRepository; | ||
|
||
public Member findById(Long id) { | ||
return memberRepository.findById(id) | ||
.orElseThrow(() -> new MemberNotFoundException(id)); | ||
} | ||
} |
Oops, something went wrong.