URL-адрес домашней страницы: https://www.codebyte.work/
Желаемый URL-адрес перенаправления после входа в систему:
Код: Выделить всё
https://www.codebyte.work/courses?accessToken=&refreshToken=
Авторизованные URI перенаправления в консоли Google:
Код: Выделить всё
https://www.codebyte.work/login/oauth2/code/googleМой класс OAuth2SuccessHandler:
Код: Выделить всё
@Component
@RequiredArgsConstructor
public class OAuth2SuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
private final UserEntityService userService;
private final JwtService jwtService;
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
OAuth2AuthenticationToken token = (OAuth2AuthenticationToken) authentication;
DefaultOAuth2User OAuthUser = (DefaultOAuth2User) token.getPrincipal();
String email = OAuthUser.getAttribute("email");
UserEntity user = userService.getUserByEmail(email);
if (user == null) {
user = userService.save(
UserEntity.builder()
.name(OAuthUser.getAttribute("name"))
.email(email)
.roles(Set.of(Roles.USER))
.build()
);
}
String accessToken = jwtService.generateAccessJwtToken(user);
String refreshToken = jwtService.generateRefreshJwtToken(user);
Cookie cookie = new Cookie("refreshToken", refreshToken);
cookie.setHttpOnly(true);
cookie.setPath("/");
response.addCookie(cookie);
// Redirecting to the frontend with tokens
String frontEndUrl = "https://www.codebyte.work/courses?accessToken=" + accessToken
+ "&refreshToken=" + refreshToken;
getRedirectStrategy().sendRedirect(request, response, frontEndUrl);
}
}
Код: Выделить всё
@Configuration
@EnableWebSecurity
@EnableMethodSecurity
@RequiredArgsConstructor
public class WebSecurityConfigs {
private final JwtAuthFilter jwtAuthFilter;
private final OAuth2SuccessHandler successHandler;
@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.authorizeRequests(auth -> auth
.requestMatchers("/auth/**", "/swagger-ui/**", "/v3/api-docs/**", "/home.html").permitAll()
.anyRequest().authenticated())
.csrf(AbstractHttpConfigurer::disable)
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class)
.oauth2Login(oauth2 -> oauth2
.successHandler(successHandler)
);
return http.build();
}
}
Код: Выделить всё
spring.security.oauth2.client.registration.google.client-id=your-client-id
spring.security.oauth2.client.registration.google.client-secret=your-client-secret
spring.security.oauth2.client.registration.google.redirect-uri=https://www.codebyte.work/login/oauth2/code/google
Когда я вхожу в систему с помощью Google, вместо перенаправления на нужный URL-адрес (https://www.codebyte.work/courses?acces ... reshToken=) приложение перенаправляется на:
Код: Выделить всё
https://www.codebyte.work/login/oauth2/code/google?state=...&code=...&scope=...
Я проверил, что:
Идентификатор клиента и секрет клиента: Правильно настроены в application.properties.
URI авторизованного перенаправления в консоли Google: Соответствует приложению конфигурация.
OAuth2SuccessHandler: зарегистрирован в WebSecurityConfigs.
Что мне нужно:
Как обеспечить перенаправление приложения на мой собственный URL-адрес (
Код: Выделить всё
[https://www.codebyte.work/courses][1]Вы можете обратиться к изображениям ниже для получения более подробной информации о проблеме:
Будем признательны за любые рекомендации и предложения.

Подробнее здесь: https://stackoverflow.com/questions/793 ... om-success
Мобильная версия