문의 시, 사용하시는 개발환경과 디벨로퍼스 앱ID를 알려주세요.1104075
springboot를 이용해서 카카오로그인을 구현중인데요!!! 백엔드를 담당하고 있고 로그인관련 에러에 대해 질문드리고 싶습니다 해당 사진에서와 같이 401에러가 왜 뜨는지 모르겠어서 질문드립니다
https://kauth.kakao.com/oauth/authorize?client_id=저희id&redirect_uri=http://localhost:8080/api/user/login/kakao&response_type=code
이 링크를 입력해서 카카오 회원가입을 진행하고(회원가입은 잘됨, 디비에 회원정보 저장됨) https://actip.swygbro.com/my/doing
로그인완료시 doing페이지로 이동을 해야하는데요!!
// 카카오 로그인 엔드포인트
@GetMapping("/login/kakao")
@Operation(summary = "카카오 로그인", description = "프론트에서 받은 인가 코드로 카카오 액세스 토큰을 발급받는다.")
public ResponseEntity<?> getLogin(@RequestParam("code") String code) {
try {
// 인가 코드로 카카오 액세스 토큰을 발급받는다.
OauthToken oauthToken = userService.getAccessToken(code);
logger.info("OauthToken: " + oauthToken);
// 카카오 회원정보 디비 저장 후 JWT 생성
AuthResponse authResponse = userService.saveUserAndGetToken(oauthToken.getAccess_token());
logger.info("JWT Token: " + authResponse.getJwtToken());
logger.info("User Nickname: " + authResponse.getNickname());
session.setAttribute("jwtToken", authResponse.getJwtToken());
session.setAttribute("nickname", authResponse.getNickname());
// 리다이렉션
URI redirectUri = URI.create("https://actip.swygbro.com/my/doing");
return ResponseEntity.status(HttpStatus.FOUND).location(redirectUri).build();
//return ResponseEntity.ok(authResponse);
} catch (HttpClientErrorException e) {
// 로그 추가
e.printStackTrace();
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid authorization code");
} catch (Exception e) {
// 기타 예외 처리
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Internal server error");
}
}
이게 카카오 로그인 컨트롤러이고 return으로 페이지로 리다이렉션하게 되어 있습니다. 현재 !!
로그를 보면
2024-08-28T19:00:40.582+09:00 INFO 15256 — [SWYP_team1_back] [nio-8080-exec-1] c.e.s.d.user.controller.UserController : JWT Token: eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJjZWxpbmFfeWdAbmF2ZXIuY29tIiwiYXV0aCI6IlJPTEVfVVNFUiIsImV4cCI6MTcyN2024-08-28T19:00:40.630+09:00 DEBUG 15256 — [SWYP_team1_back] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed 302 FOUND
이렇게 Completed 302 FOUND 로그가 뜨고 doing페이지로 이동을 하고 있습니다
그런데 해당 doing페이지로 이동해서 콘솔을 보면 캡쳐화면과 같이 401에러가 뜨고 있습니다. 저 에러는 왜 뜨는건지 아무리 찾아봐도 모르겠어서 질문드립니다 ㅜㅜ
Img로 필터 하셔서 아무 내용도 안보입니다.
/api/tip/doing?pageSize=15
에서 401 오류 발생하는 케이스로
구현하신 서버 로그를 확인해 보시는게 좋을것 같습니다.
API 사용 인증에 필요한 쿠키나 헤더가 전달되지 않았을것으로 보이네요
다시 올렸습니다 쿠키는 백엔드랑 프론트 도메인이 달라서 쿠키로 전달하는방법이 안되서 다른 방법을 찾고 있습니다 ㅜㅜ
2024-08-31T19:20:50.424+09:00 DEBUG 24036 — [SWYP_team1_back] [nio-8080-exec-6] o.s.security.web.FilterChainProxy : Securing GET /api/user/login/
kakao?code=D6qPpC9dlqfXRtNputC22IhSCEXcnCt9woRJt1_7QJlPc9WfhhdOCgAAAAQKKiUOAAABkafzrCQe0jm_MNo9Pw
2024-08-31T19:20:50.424+09:00 INFO 24036 — [SWYP_team1_back] [nio-8080-exec-6] c.e.s.global.jwt.JwtFilter : Bearer Token: null
2024-08-31T19:20:50.424+09:00 INFO 24036 — [SWYP_team1_back] [nio-8080-exe
c-6] c.e.s.global.jwt.JwtFilter : Extracted JWT from request header: null
2024-08-31T19:20:50.425+09:00 DEBUG 24036 — [SWYP_team1_back] [nio-8080-exe
c-6] o.s.security.web.FilterChainProxy : Secured GET /api/user/login/k
akao?code=D6qPpC9dlqfXRtNputC22IhSCEXcnCt9woRJt1_7QJlPc9WfhhdOCgAAAAQKKiUOAAABkafzrCQe0jm_MNo9Pw
2024-08-31T19:20:50.425+09:00 DEBUG 24036 — [SWYP_team1_back] [nio-8080-exe
c-6] o.s.web.servlet.DispatcherServlet : GET “/api/user/login/kakao?co
de=D6qPpC9dlqfXRtNputC22IhSCEXcnCt9woRJt1_7QJlPc9WfhhdOCgAAAAQKKiUOAAABkafzrCQe0jm_MNo9Pw”, parameters={masked}
2024-08-31T19:20:50.425+09:00 DEBUG 24036 — [SWYP_team1_back] [nio-8080-exe
c-6] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.example.swyp_team1_back.domain.user.controller.UserController#getLogin(String)
2024-08-31T19:20:50.426+09:00 DEBUG 24036 — [SWYP_team1_back] [nio-8080-exe
c-6] o.s.web.client.RestTemplate : HTTP POST https://kauth.kakao.com/oauth/token
2024-08-31T19:20:50.426+09:00 DEBUG 24036 — [SWYP_team1_back] [nio-8080-exe
c-6] o.s.web.client.RestTemplate : Accept=[text/plain, application/json, application/cbor, application/+json, /]
2024-08-31T19:20:50.427+09:00 DEBUG 24036 — [SWYP_team1_back] [nio-8080-exe
c-6] o.s.web.client.RestTemplate : Writing [{grant_type=[authori
zation_code], client_id=[5d3ed4c53f6081a9a1503e178dfdfaeb], redirect_uri=[htt
p://localhost:8080/api/user/login/kakao], client_secret=[SazkBcme6hi3TkyhRBBp
s3Hl0G7rMfcP], code=[D6qPpC9dlqfXRtNputC22IhSCEXcnCt9woRJt1_7QJlPc9WfhhdOCgAAAAQKKiUOAAABkafzrCQe0jm_MNo9Pw]}] as “application/x-www-form-urlencoded”
2024-08-31T19:20:50.518+09:00 DEBUG 24036 — [SWYP_team1_back] [nio-8080-exec-6] o.s.web.client.RestTemplate : Response 200 OK
2024-08-31T19:20:50.518+09:00 DEBUG 24036 — [SWYP_team1_back] [nio-8080-exe
c-6] o.s.web.client.RestTemplate : Reading to [java.lang.String] as “application/json;charset=utf-8”
2024-08-31T19:20:50.519+09:00 INFO 24036 — [SWYP_team1_back] [nio-8080-exe
c-6] c.e.s.d.user.controller.UserController : OauthToken: OauthToken{access
_token=‘vfbyHaHzN7jytfR-m-sR4gYqCy2_pta_AAAAAQo8JB8AAAGRp_OsgrG7d-HwzTGR’, to
ken_type=‘bearer’, refresh_token=‘38GUfDfRz8Lv-sC9xS2HEosDhthoOETrAAAAAgo8JB8
AAAGRp_OsfrG7d-HwzTGR’, expires_in=21599, scope=‘account_email profile_nickname’, refresh_token_expires_in=5183999}
2024-08-31T19:20:50.521+09:00 DEBUG 24036 — [SWYP_team1_back] [nio-8080-exe
c-6] o.s.web.client.RestTemplate : HTTP POST https://kapi.kakao.com/v2/user/me
2024-08-31T19:20:50.521+09:00 DEBUG 24036 — [SWYP_team1_back] [nio-8080-exe
c-6] o.s.web.client.RestTemplate : Accept=[text/plain, application/json, application/cbor, application/+json, /]
2024-08-31T19:20:50.594+09:00 DEBUG 24036 — [SWYP_team1_back] [nio-8080-exec-6] o.s.web.client.RestTemplate : Response 200 OK
2024-08-31T19:20:50.595+09:00 DEBUG 24036 — [SWYP_team1_back] [nio-8080-exe
c-6] o.s.web.client.RestTemplate : Reading to [java.lang.String] as “application/json;charset=UTF-8”
Hibernate:
select
u1_0.id,
u1_0.agree_picu,
u1_0.agree_tos,
u1_0.agree_mt,
u1_0.email,
u1_0.from_social,
u1_0.imgurl,
u1_0.is_deleted,
u1_0.name,
u1_0.nickname,
u1_0.password,
u1_0.phone,
u1_0.reg_date,
u1_0.role,
u1_0.update_date
from
user u1_0
where
u1_0.email=?
Hibernate:
select
cl1_0.id,
cl1_0.cs_id,
cl1_0.contents
from
2024-08-31T19:20:50.637+09:00 DEBUG 24036 — [SWYP_team1_back] [nio-8080-exec-6] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Using ‘text/html’, given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, /;q=0.8, application/signed-exchange;v=b3;q=0.7] and supported [/]
2024-08-31T19:20:50.637+09:00 DEBUG 24036 — [SWYP_team1_back] [nio-8080-exec-6] o.s.web.servlet.DispatcherServlet : Completed 302 FOUND
2024-08-31T19:20:50.637+09:00 DEBUG 24036 — [SWYP_team1_back] [nio-8080-exec-6] o.s.s.w.a.AnonymousAuthenticationFilter : Set SecurityContextHolder to anonymous SecurityContext
현재 서버 코드입니다.카카오로그인시 로그입니다!
쿠키전달이 저희 백엔드, 프론트 배포 도메인이 달라서 안되는데 혹시 다른방법은 없나요 ㅜㅜㅜ