Вот мой текущий код:
Код: Выделить всё
@Component
public class CustomOAuth2SuccessHandler implements AuthenticationSuccessHandler {
private final UserService userService;
private final JwtService jwtService;
private final RefreshTokenService refreshTokenService;
public CustomOAuth2SuccessHandler(UserService userService, JwtService jwtService, RefreshTokenService refreshTokenService) {
this.userService = userService;
this.jwtService = jwtService;
this.refreshTokenService = refreshTokenService;
}
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
OAuth2User oAuth2User = (OAuth2User) authentication.getPrincipal();
String email = oAuth2User.getAttribute("email");
String fullName = oAuth2User.getAttribute("name");
if (!userService.existsMail(email) && !userService.checkUser(email)) {
response.sendRedirect("http://localhost:3000/register?email=" + email + "&fullName=" + fullName);
} else {
response.sendRedirect("http://localhost:3000/home");
}
}
}
// Security configuration
http
.oauth2Login(oauth2 -> oauth2
.successHandler(customOAuth2SuccessHandler)
.failureUrl("http://localhost:3000/login")
);
@PostMapping("/login")
public ResponseEntity login(@RequestBody AuthRequest authRequest) {
AuthResponse response = authService.login(authRequest);
return new ResponseEntity(RestResponse.of(response), HttpStatus.OK);
}
public record AuthResponse(
Long userId,
String username,
String profileImageUrl,
String accessToken,
String refreshToken,
String message
) {}
Когда пользователь успешно входит в систему с помощью Google, я хочу получить объект AuthResponse. Как я могу добиться этого, не перенаправляя пользователя на определенный URL-адрес? Как я могу использовать его во время перенаправления вместо возврата ответа AuthResponse в формате JSON?
Спасибо за помощь!
Подробнее здесь: https://stackoverflow.com/questions/791 ... -boot-appl
Мобильная версия