UPDATE OAuth2 user service to handle user creation and redirect after login

This commit is contained in:
2025-06-24 14:21:59 +02:00
parent df7c2a15df
commit 0237ec57db
8 changed files with 84 additions and 13 deletions

View File

@@ -1,4 +1,4 @@
package com.ddf.vodsystem.security;
package com.ddf.vodsystem.configuration;
import com.ddf.vodsystem.entities.User;
import com.ddf.vodsystem.repositories.UserRepository;
@@ -8,6 +8,9 @@ import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.Optional;
@Service
public class CustomOAuth2UserService extends DefaultOAuth2UserService {
@@ -26,15 +29,18 @@ public class CustomOAuth2UserService extends DefaultOAuth2UserService {
String name = oAuth2User.getAttribute("name");
String googleId = oAuth2User.getAttribute("sub");
userRepository.findByGoogleId(googleId).orElseGet(() -> {
Optional<User> existingUser = userRepository.findByGoogleId(googleId);
if (existingUser.isEmpty()) {
User user = new User();
user.setEmail(email);
user.setName(name);
user.setGoogleId(googleId);
user.setUsername(email);
user.setRole(0);
return userRepository.save(user);
});
user.setCreatedAt(LocalDateTime.now());
userRepository.save(user);
}
return oAuth2User;
}

View File

@@ -1,5 +1,6 @@
package com.ddf.vodsystem.security;
package com.ddf.vodsystem.configuration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
@@ -7,7 +8,6 @@ import org.springframework.security.config.annotation.web.configuration.EnableWe
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
@Configuration
@EnableWebSecurity
@@ -15,6 +15,9 @@ public class SecurityConfig {
private final CustomOAuth2UserService customOAuth2UserService;
@Value("${frontend.url}")
private String frontendUrl;
public SecurityConfig(CustomOAuth2UserService customOAuth2UserService) {
this.customOAuth2UserService = customOAuth2UserService;
}
@@ -24,6 +27,7 @@ public class SecurityConfig {
http
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(auth -> auth
.requestMatchers("/api/v1/auth/login", "/api/v1/auth/user").permitAll()
.anyRequest().authenticated()
)
.oauth2Login(oauth2 -> oauth2
@@ -39,7 +43,7 @@ public class SecurityConfig {
@Bean
public AuthenticationSuccessHandler successHandler() {
return new SimpleUrlAuthenticationSuccessHandler("/api/v1/auth/user");
return (request, response, authentication) -> response.sendRedirect(frontendUrl);
}
}

View File

@@ -16,9 +16,4 @@ public class AuthController {
public Map<String, Object> user(@AuthenticationPrincipal OAuth2User principal) {
return principal.getAttributes();
}
@GetMapping("/login")
public String login() {
return "login";
}
}