diff --git a/src/main/java/stackpot/stackpot/config/security/SecurityConfig.java b/src/main/java/stackpot/stackpot/config/security/SecurityConfig.java index 55f86071..2248aeca 100644 --- a/src/main/java/stackpot/stackpot/config/security/SecurityConfig.java +++ b/src/main/java/stackpot/stackpot/config/security/SecurityConfig.java @@ -49,7 +49,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http, JwtTokenProvid .requestMatchers("/swagger-ui/**", "/v3/api-docs/**", "/swagger-ui.html").permitAll() // 스웨거 관련 접근 허용 .requestMatchers("/users/oauth/**", "/reissue").permitAll() // 인증 관련 스웨거 접근 허용 .requestMatchers("/home", "/sign-up", "/pots", "/feeds").permitAll() - .requestMatchers("/ws-connect/**").permitAll() + .requestMatchers("/ws-connect/**","/oauth/**").permitAll() // .requestMatchers("").hasAnyRole("TEMP","ADMIN") // Test를 위해 모든 접근 // .requestMatchers("").hasAnyRole("USER","ADMIN") // .requestMatchers("").hasRole("ADMIN")// 관리자 권한은 아직 생성하지 않았습니다. diff --git a/src/main/java/stackpot/stackpot/user/controller/UserController.java b/src/main/java/stackpot/stackpot/user/controller/UserController.java index a7b343b0..720c2f74 100644 --- a/src/main/java/stackpot/stackpot/user/controller/UserController.java +++ b/src/main/java/stackpot/stackpot/user/controller/UserController.java @@ -6,6 +6,7 @@ import java.nio.charset.StandardCharsets; import java.util.List; +import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; @@ -79,6 +80,12 @@ public class UserController { private final UserQueryService userQueryService; private final FeedQueryService feedQueryService; + @Value("${spring.google.client-id}") + private String clientId; + + @Value("${spring.google.redirect-uri}") + private String redirectUri; + @GetMapping("/login/token") @Operation( summary = "토큰 테스트 API", @@ -171,6 +178,28 @@ public ResponseEntity> naverCallback(@Requ return ResponseEntity.ok(ApiResponse.onSuccess(userResponse)); } + @GetMapping("/start") + public void googleStart(@RequestParam String returnUrl, HttpServletResponse response) throws IOException { + + if (!returnUrl.startsWith("http://localhost:5173")) { + throw new IllegalArgumentException("Invalid returnUrl"); + } + + // state에 returnUrl 넣어서 콜백 때 다시 받기 + String state = URLEncoder.encode(returnUrl, StandardCharsets.UTF_8); + + String googleAuthUrl = + "https://accounts.google.com/o/oauth2/v2/auth" + + "?client_id=" + URLEncoder.encode(clientId, StandardCharsets.UTF_8) + + "&redirect_uri=" + URLEncoder.encode(redirectUri, StandardCharsets.UTF_8) + + "&response_type=code" + + "&scope=" + URLEncoder.encode("openid email profile", StandardCharsets.UTF_8) + + "&access_type=offline" + + "&prompt=consent" + + "&state=" + state; + + response.sendRedirect(googleAuthUrl); + } @GetMapping("/oauth/google") @Operation( summary = "구글 로그인 및 토큰발급 API",