From c7b3f6bf70afe64c9afc231102a7038730a6e045 Mon Sep 17 00:00:00 2001 From: ThisBirchWood Date: Mon, 23 Jun 2025 23:28:29 +0200 Subject: [PATCH] REFINE OAuth2 user handling and update database schema --- frontend/src/components/Topbar.tsx | 12 ++++-- .../vodsystem/controllers/AuthController.java | 3 +- .../java/com/ddf/vodsystem/entities/User.java | 2 +- .../security/CustomOAuth2UserService.java | 42 ++++++++----------- .../vodsystem/security/SecurityConfig.java | 21 ++++++---- .../resources/application-local.properties | 1 - src/main/resources/db/data.sql | 22 +++++----- src/main/resources/db/schema.sql | 7 ++-- 8 files changed, 56 insertions(+), 54 deletions(-) diff --git a/frontend/src/components/Topbar.tsx b/frontend/src/components/Topbar.tsx index 46a5c6c..8567637 100644 --- a/frontend/src/components/Topbar.tsx +++ b/frontend/src/components/Topbar.tsx @@ -1,19 +1,23 @@ -import { Menu, X} from 'lucide-react'; +import { Menu, X } from 'lucide-react'; import MenuButton from "./buttons/MenuButton.tsx"; import clsx from "clsx"; type props = { - sidebarToggled: boolean, - setSidebarToggled: Function + sidebarToggled: boolean; + setSidebarToggled: Function; className?: string; } const Topbar = ({sidebarToggled, setSidebarToggled, className}: props) => { return ( -
+
setSidebarToggled(!sidebarToggled)}> {sidebarToggled ? : } + + + Login +
) } diff --git a/src/main/java/com/ddf/vodsystem/controllers/AuthController.java b/src/main/java/com/ddf/vodsystem/controllers/AuthController.java index 107c7d4..311d506 100644 --- a/src/main/java/com/ddf/vodsystem/controllers/AuthController.java +++ b/src/main/java/com/ddf/vodsystem/controllers/AuthController.java @@ -6,7 +6,6 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import java.util.Collections; import java.util.Map; @RestController @@ -15,7 +14,7 @@ public class AuthController { @GetMapping("/user") public Map user(@AuthenticationPrincipal OAuth2User principal) { - return Collections.singletonMap("name", principal.getAttribute("name")); + return principal.getAttributes(); } @GetMapping("/login") diff --git a/src/main/java/com/ddf/vodsystem/entities/User.java b/src/main/java/com/ddf/vodsystem/entities/User.java index 2ea785c..20e47a2 100644 --- a/src/main/java/com/ddf/vodsystem/entities/User.java +++ b/src/main/java/com/ddf/vodsystem/entities/User.java @@ -29,7 +29,7 @@ public class User { @Column(name = "role", nullable = false) private Integer role; // 0: user, 1: admin - @Column(name = "created_at", nullable = false) + @Column(name = "created_at") private LocalDateTime createdAt; } diff --git a/src/main/java/com/ddf/vodsystem/security/CustomOAuth2UserService.java b/src/main/java/com/ddf/vodsystem/security/CustomOAuth2UserService.java index 0423922..0189ac4 100644 --- a/src/main/java/com/ddf/vodsystem/security/CustomOAuth2UserService.java +++ b/src/main/java/com/ddf/vodsystem/security/CustomOAuth2UserService.java @@ -1,17 +1,15 @@ package com.ddf.vodsystem.security; - -import com.ddf.vodsystem.entities.User; import com.ddf.vodsystem.repositories.UserRepository; + import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService; import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest; +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 { +public class CustomOAuth2UserService extends DefaultOAuth2UserService { private final UserRepository userRepository; @@ -20,25 +18,21 @@ public class CustomOAuth2UserService extends DefaultOAuth2UserService { } @Override - public OAuth2User loadUser(OAuth2UserRequest userRequest) { - OAuth2User oauthUser = super.loadUser(userRequest); + public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException { + OAuth2User oAuth2User = super.loadUser(userRequest); - String googleId = oauthUser.getAttribute("sub"); // Google's unique user ID - String email = oauthUser.getAttribute("email"); - String name = oauthUser.getAttribute("name"); +// String email = oAuth2User.getAttribute("email"); +// String name = oAuth2User.getAttribute("name"); +// String googleId = oAuth2User.getAttribute("sub"); +// +// userRepository.findByGoogleId(googleId).orElseGet(() -> { +// User user = new User(); +// user.setEmail(email); +// user.setName(name); +// user.setGoogleId(googleId); +// return userRepository.save(user); +// }); - Optional userOptional = userRepository.findByGoogleId(googleId); - User user; - if (userOptional.isEmpty()) { - user = new User(); - user.setGoogleId(googleId); - user.setEmail(email); - user.setName(name); - user.setUsername(email.split("@")[0]); - user.setCreatedAt(LocalDateTime.now()); - userRepository.save(user); - } - - return oauthUser; + return oAuth2User; } -} \ No newline at end of file +} diff --git a/src/main/java/com/ddf/vodsystem/security/SecurityConfig.java b/src/main/java/com/ddf/vodsystem/security/SecurityConfig.java index 93d3d8c..584ba53 100644 --- a/src/main/java/com/ddf/vodsystem/security/SecurityConfig.java +++ b/src/main/java/com/ddf/vodsystem/security/SecurityConfig.java @@ -3,10 +3,14 @@ package com.ddf.vodsystem.security; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 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 public class SecurityConfig { private final CustomOAuth2UserService customOAuth2UserService; @@ -20,19 +24,22 @@ public class SecurityConfig { http .csrf(AbstractHttpConfigurer::disable) .authorizeHttpRequests(auth -> auth - .requestMatchers("/", "/css/**", "api/v1/**").permitAll() .anyRequest().authenticated() ) .oauth2Login(oauth2 -> oauth2 - .loginPage("/login") .userInfoEndpoint(userInfo -> userInfo - .userService(customOAuth2UserService)) - ) - .logout(logout -> logout - .logoutSuccessUrl("/") - .permitAll() + .userService(customOAuth2UserService) + + ) + .successHandler(successHandler()) ); return http.build(); } + + @Bean + public AuthenticationSuccessHandler successHandler() { + return new SimpleUrlAuthenticationSuccessHandler("/api/v1/auth/user"); + } + } diff --git a/src/main/resources/application-local.properties b/src/main/resources/application-local.properties index 7aa05ee..83b93c7 100644 --- a/src/main/resources/application-local.properties +++ b/src/main/resources/application-local.properties @@ -13,4 +13,3 @@ spring.sql.init.data-locations=classpath:db/data.sql spring.security.oauth2.client.registration.google.client-id=${GOOGLE_CLIENT_ID} spring.security.oauth2.client.registration.google.client-secret=${GOOGLE_CLIENT_SECRET} spring.security.oauth2.client.registration.google.scope=openid,profile,email -spring.security.oauth2.client.registration.google.redirect-uri=http://localhost:8080/login/oauth2/code/google \ No newline at end of file diff --git a/src/main/resources/db/data.sql b/src/main/resources/db/data.sql index 86a5171..ddb0c87 100644 --- a/src/main/resources/db/data.sql +++ b/src/main/resources/db/data.sql @@ -1,15 +1,15 @@ -INSERT INTO users (id, google_id, username, email, name) +INSERT INTO users ( google_id, username, email, name) VALUES - (1, 'google-uid-001', 'alice', 'alice@example.com', 'Alice Example'), - (2, 'google-uid-002', 'bob', 'bob@example.com', 'Bob Example'), - (3, 'google-uid-003', 'carol', 'carol@example.com', 'Carol Example'), - (4, 'google-uid-004', 'wizard42', 'gandalf@middle.earth', 'Gandalf the Grey'), - (5, 'google-uid-005', 'catnap', 'whiskers@meowmail.com', 'Sir Whiskers McFluff'), - (6, 'google-uid-006', 'robotron', 'bender@futurama.tv', 'Bender Rodriguez'), - (7, 'google-uid-007', 'unicorn', 'sparkle@rainbow.com', 'Princess Sparklehoof'), - (8, 'google-uid-008', 'pirate', 'blackbeard@seas.com', 'Edward Teach'), - (9, 'google-uid-009', 'detective', 'holmes@bakerstreet.uk', 'Sherlock Holmes'), - (10, 'google-uid-010', 'timey', 'docbrown@delorean.net', 'Dr. Emmett Brown'); + ('google-uid-001', 'alice', 'alice@example.com', 'Alice Example'), + ( 'google-uid-002', 'bob', 'bob@example.com', 'Bob Example'), + ('google-uid-003', 'carol', 'carol@example.com', 'Carol Example'), + ( 'google-uid-004', 'wizard42', 'gandalf@middle.earth', 'Gandalf the Grey'), + ( 'google-uid-005', 'catnap', 'whiskers@meowmail.com', 'Sir Whiskers McFluff'), + ( 'google-uid-006', 'robotron', 'bender@futurama.tv', 'Bender Rodriguez'), + ('google-uid-007', 'unicorn', 'sparkle@rainbow.com', 'Princess Sparklehoof'), + ( 'google-uid-008', 'pirate', 'blackbeard@seas.com', 'Edward Teach'), + ( 'google-uid-009', 'detective', 'holmes@bakerstreet.uk', 'Sherlock Holmes'), + ( 'google-uid-010', 'timey', 'docbrown@delorean.net', 'Dr. Emmett Brown'); INSERT INTO clips (id, user_id, title, description, width, height, fps, duration, file_size, video_path) VALUES diff --git a/src/main/resources/db/schema.sql b/src/main/resources/db/schema.sql index 4e8bcda..bb261e5 100644 --- a/src/main/resources/db/schema.sql +++ b/src/main/resources/db/schema.sql @@ -2,18 +2,17 @@ DROP TABLE IF EXISTS clips; DROP TABLE IF EXISTS users; CREATE TABLE IF NOT EXISTS users ( - id BIGINT PRIMARY KEY, + id BIGSERIAL PRIMARY KEY, google_id VARCHAR(64), username VARCHAR(50) NOT NULL UNIQUE, email VARCHAR(100) NOT NULL UNIQUE, name VARCHAR(100) NOT NULL, role INTEGER DEFAULT 0, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP -); - + ); CREATE TABLE IF NOT EXISTS clips ( - id BIGINT PRIMARY KEY, + id BIGSERIAL PRIMARY KEY, user_id BIGINT NOT NULL, title VARCHAR(255) NOT NULL, description TEXT,