[Spring] 무한 redirection

2024. 8. 14. 14:43·Back-end/Spring
반응형

문제사항

 

spring google 소셜 로그인 구현 중 SuccessHandler에 리디렉션 url을 설정하여 로그인이 잘 되는지 테스트하고 있는 도중에, 갑자기 무한 리다이렉트 되는 현상을 발견했다...!

ㅠㅠ

OAuth2SuccessHandler

핸들러 코드를 살펴보자...

@Component
@Slf4j
public class OAuth2SuccessHandler implements AuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
                                        Authentication authentication) throws IOException {

        log.info("success handler 실행");

        if (authentication.getPrincipal() instanceof OAuth2User) {
            CustomOauth2UserDetails principal = (CustomOauth2UserDetails) authentication.getPrincipal();

            Member member = principal.getMember();
            log.info("member : {} {} ", member.getId(), member.getEmail()); // 수정된 부분

            if (member.getEmail().equals("ryu7844@gmail.com")) {
                String redirectionUri = UriComponentsBuilder.fromUriString("http://localhost:8080/hello")
                        .queryParam("email", member.getEmail())
                        .build()
                        .toUriString();
                response.sendRedirect(redirectionUri);

            } else {
                String redirectionUri = UriComponentsBuilder.fromUriString("http://localhost:8080/hello")
                        .queryParam("token", "hello")
                        .build()
                        .toUriString();
                response.sendRedirect(redirectionUri);
            }
        } else {
            // OAuth2User가 아닌 경우
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        }
    }
}

 

위 방식으로 백엔드 url의 /hello 엔드포인트로 요청을 보냈을 때 무한 리디렉션이 일어나게 되었다...

 

해결

구글링을 계속해도 불구하고 마땅한 답변이 나오지 않아 현재 현업에서 종사하시는 형님께 여쭤보았다.

 

결론적으로 redirection은 해당 url로 GET 요청을 보내는 것.

 

/hello 라는 엔드포인트가 구현이 되어있는지 물어보셨다. (여기서 살짝 쎄함을 느끼는데...)

 

해당 엔드포인트가 구현이 안되어있기 때문에 계속 success handler가 작동을 하게 된다는 것!

 

그래서 localhost:3000 이라는 빈 포트로 리디렉션해보니 정상적으로 작동하였다!

 

String redirectionUri = UriComponentsBuilder.fromUriString("http://localhost:3000/hello")
                        .queryParam("token", "hello")
                        .build()
                        .toUriString();
response.sendRedirect(redirectionUri);
반응형

'Back-end > Spring' 카테고리의 다른 글

[Spring] Security + JWT + Redis를 활용한 로그인 구현 (2)  (0) 2024.08.30
[Spring] @NotNull vs @Column(nullable = false)  (1) 2024.08.19
[Spring] Security + JWT + Redis를 활용한 로그인 구현 (1)  (0) 2024.07.20
[Spring] WebClient를 통한 공공데이터 Open API 호출  (1) 2024.07.17
[Spring] H2 DataBase 사용법  (0) 2024.06.27
'Back-end/Spring' 카테고리의 다른 글
  • [Spring] Security + JWT + Redis를 활용한 로그인 구현 (2)
  • [Spring] @NotNull vs @Column(nullable = false)
  • [Spring] Security + JWT + Redis를 활용한 로그인 구현 (1)
  • [Spring] WebClient를 통한 공공데이터 Open API 호출
류건
류건
개발 일지
  • 류건
    건's Dev
    류건
  • 전체
    오늘
    어제
    • 분류 전체보기 (94)
      • Back-end (55)
        • Spring (30)
        • Nest.js (3)
        • Next.js (2)
        • Node.js (3)
      • Infra & Cloud (20)
        • Cloud Computing (6)
        • Docker (3)
        • AWS (7)
      • Java (2)
      • Computer Science (12)
        • Computer Network (0)
        • Operating System (0)
        • 정보 보호와 시스템 보안 (12)
      • 회고록 (1)
        • 우아한테크코스 (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    Kafka
    public key
    Spring Boot
    CD
    정보보호
    WebClient
    Docker
    JPA
    Spring
    CI/CD
    aws
    Lock
    Nest.js
    어노테이션
    CORS
    고가용성
    EC2
    db
    https
    Github Actions
    Webflux
    nginx
    ddl-auto
    오블완
    JWT
    티스토리챌린지
    express.js
    node.js
    보안
    ssl
  • 최근 댓글

  • 최근 글

  • 반응형
  • hELLO· Designed By정상우.v4.10.0
류건
[Spring] 무한 redirection
상단으로

티스토리툴바