feign client는 annotation으로 사용할 수 있도록 구현되어 있습니다. 카카오 로그인에서 토큰을 가져오고 토큰을 이용해서 정보를 가져올 때 feign client를 이용했습니다.
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
implementation platform("org.springframework.cloud:spring-cloud-dependencies:2021.0.8")
@EnableFeignClients
@SpringBootApplication
public class ProjectApplication {
public static void main(String[] args) {
SpringApplication.run(ProjectApplication.class, args);
}
}
@Component
@FeignClient(name = "KakaoTokenFeignClient", url = "${oauth2.kakao.base-url}", configuration = KakaoFeignConfiguration.class)
public interface KakaoTokenFeignClient {
@PostMapping(value = "/oauth/token")
KakaoTokenResponse generateToken(@RequestParam(value = "grant_type") String grantType,
@RequestParam(value = "client_id") String clientId,
@RequestParam(value = "redirect_uri") String redirectUri,
@RequestParam(value = "code") String code);
}
public class KakaoFeignConfiguration {
@Bean
public RequestInterceptor requestInterceptor() {
return requestTemplate -> {
requestTemplate.header("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
};
}
@Bean
public ErrorDecoder errorDecoder() {
return new FeignClientException();
}
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
//memberController
@GetMapping("/callback/kakao")
public ResponseDTO<?> getKakaoAccount(@RequestParam("code") String code) {
return memberService.getKakaoInfo(code);
}
@Service
@RequiredArgsConstructor
public class KakaoOauthService {
private final KakaoInfoFeignClient kakaoInfoFeignClient;
public KakaoProfile getKakaoUserInfo(String token) {
KakaoProfile kakaoProfile = kakaoInfoFeignClient.getInfo(token);
return kakaoProfile;
}
}
@Component
@FeignClient(name = "KakaoFeignClient", url = "${oauth2.kakao.info-url}", configuration = KakaoFeignConfiguration.class)
public interface KakaoInfoFeignClient {
@GetMapping("/v2/user/me")
KakaoProfile getInfo(@RequestHeader(name = "Authorization") String Authorization);
}