Test.html 접속시 Google OAuth 로그인 오류

ssongyi·4일 전
0

Java/Spring 트러블슈팅

목록 보기
12/12

WebSocket 을 테스트하기 위해

http://localhost:8080/test-websocket.html

--> 해당 브라우저에 접속했는데 아래와 같은 오류가 났다.

✅ 구글 로그인 시도 → OAuth client 설정이 잘못됨 → 401 발생

🚩 근데 왜 /test-websocket.html 접속했는데 이게 떴나?

✅ 이유 → 브라우저에서 기존에 "자동 리다이렉트" 또는 "자동 로그인" 시도가 발생했을 가능성
✅ 특히 스프링 시큐리티를 사용 중이라면:

1️⃣ /test-websocket.html → 시큐리티 필터 타면서 인증 필요 → OAuth2 로그인으로 리다이렉트 됨
2️⃣ Google OAuth 설정이 지금 client-id / secret 가 잘못돼 있어서 → 저 에러 발생

🚩 핵심 → 이건 WebSocket 테스트랑 별개

/test-websocket.html정적 리소스(static)
→ 원래는 시큐리티 예외 처리해서 로그인 없이 열리게 해야 정상

WebSocketConfig 코드는 WebSocket/STOMP 설정만 담당하고 있고,
HTTP 요청 (예: /test-websocket.html) 에 대한 보안 설정은 SecurityConfig 쪽에서 담당한다.
→ 그러니까 static 리소스 예외처리는 Security 쪽 설정에 추가해야할 필요성이 있다.

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    http
            .csrf(csrf -> csrf.disable()) // 운영할 땐 disable xx
            //.cors(cors -> cors.configurationSource(corsConfigurationSource)) // 배포 후 설정
            .sessionManagement(sess -> sess.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
            .authorizeHttpRequests(auth -> auth
                    .requestMatchers(
                            "/test-websocket.html",  // ✅ 정적 테스트 페이지 허용
                            "/static/**",            // ✅ static 리소스 허용
                            "/api/users/login",
                            "/api/users/signup",
                            "/oauth2/**",
                            "/login/oauth2/**"
                    ).permitAll()
                    .anyRequest().authenticated()
            )
            .oauth2Login(oauth ->
                    oauth.userInfoEndpoint(userInfo ->
                            userInfo.userService(customOAuth2UserService))
                            .successHandler(oAuth2SuccessHandler)
            )
            .addFilterBefore(securityFilter, UsernamePasswordAuthenticationFilter.class)
            .httpBasic(httpBasic -> httpBasic.disable())
            .formLogin(formLogin -> formLogin.disable());

    return http.build();
}
  • /test-websocket.html → 정적 파일 직접 허용
  • /static/** → 혹시 정적 리소스 하위에서 css, js 등 쓸 경우도 대비

✅ 요약

단계해야 할 것
SecurityConfig 수정.requestMatchers("/test-websocket.html", "/static/**").permitAll() 추가
서버 재시작 후 테스트/test-websocket.html 접속해서 확인

정상적으로 접근되는 점 확인할 수 있다!

0개의 댓글