local에서 카카오 로그인시 정상적으로 로그인이 되는데 aws에서 배포 후 카카오 로그인을 시도하면 500에러가 뜨며 서버에는 2024-04-16T06:28:11.594Z ERROR 2775 — [nio-8070-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception
java.io.IOException: Server returned HTTP response code: 401 for URL: https://kauth.kakao.com/oauth/token 라는 에러가 발생합니다. redirect_uri를 바꿔도 허용 ip를 추가해도 로그인이 되질 않네요… 무엇이 문제인지 여쭤보고싶습니다.
-
앱 id는 1058275 / 앱 이름은 Nintenddo입니다
-
redirect_uri : http://15.164.200.111:8070/api/members/kakaoLogin
-
cilent_secret 비활성화
-
카카오 로그인을 위한 코드 전문입니다
@RequestMapping(“/kakaoLogin”)
public void loginKakao(
HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException {
String code = request.getParameter("code");
String endpoint = "https://kauth.kakao.com/oauth/token";
URL url = new URL(endpoint); // import java.net.URL;
String bodyData = "grant_type=authorization_code&";
bodyData += "redirect_uri=http://15.164.200.111:8070/api/members/kakaoLogin&";
/* bodyData += "redirect_uri=http://localhost:8070/api/members/kakaoLogin&"; */
bodyData += "code=" + code;
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // import java.net.HttpURLConnection;
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
conn.setDoOutput(true);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));
bw.write(bodyData);
bw.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String input = "";
StringBuilder sb = new StringBuilder(); // 조각난 String 을 조립하기위한 객체
while ((input = br.readLine()) != null) {
sb.append(input);
System.out.println(input); // 수신된 토큰을 콘솔에 출력합니다
}
Gson gson = new Gson();
OAuthToken oAuthToken = gson.fromJson(sb.toString(), OAuthToken.class);
String endpoint2 = “https://kapi.kakao.com/v2/user/me”;
URL url2 = new URL(endpoint2);
HttpsURLConnection conn2 = (HttpsURLConnection) url2.openConnection();
conn2.setRequestProperty("Authorization", "Bearer " + oAuthToken.getAccess_token());
conn2.setDoOutput(true);
BufferedReader br2 = new BufferedReader(new InputStreamReader(conn2.getInputStream(), "UTF-8"));
String input2 = "";
StringBuilder sb2 = new StringBuilder();
while ((input2 = br2.readLine()) != null) {
sb2.append(input2);
System.out.println(input2);
}
Gson gson2 = new Gson();
KakaoProfile kakaoProfile = gson2.fromJson(sb2.toString(), KakaoProfile.class);
KakaoAccount ac = kakaoProfile.getAccount();
Profile pf = ac.getProfile();
// System.out.println("id : " + kakaoProfile.getId());
// System.out.println("email : " + ac.getEmail());
// System.out.println("nickname : " + pf.getNickname());
Member member = ms.getMember(kakaoProfile.getId());
if(member == null) {
// 해당 카카오 계정이 member 테이블에 없다면 회원가입 진행
member = new Member();
member.setUserid(kakaoProfile.getId());
member.setEmail(ac.getEmail());
member.setProvider(“kakao”);
ms.insertMember(member);
}else {
}
HttpSession session = request.getSession();
session.setAttribute("loginUser", member);
/* response.sendRedirect("http://localhost:3000/kakaosaveinfo"); */
response.sendRedirect("http://15.164.200.111:3000/kakaosaveinfo");
}