From 43faab805be152c8ca952cfdc9dbdcd25fa99930 Mon Sep 17 00:00:00 2001 From: Le Phuc Date: Fri, 14 Jun 2024 16:19:38 +0700 Subject: [PATCH] login-regis --- BE/.gitignore | 38 +++++++ BE/.idea/.gitignore | 8 ++ BE/.idea/encodings.xml | 7 ++ BE/.idea/misc.xml | 14 +++ BE/.idea/vcs.xml | 6 ++ BE/pom.xml | 85 ++++++++++++++++ BE/src/main/java/org/example/Main.java | 11 +++ .../org/example/config/SecurityConfig.java | 69 +++++++++++++ .../example/controllers/UserController.java | 34 +++++++ BE/src/main/java/org/example/models/User.java | 98 +++++++++++++++++++ .../java/org/example/objects/UserDTO.java | 33 +++++++ .../CourseEnrolmentQueryResult.java | 20 ++++ .../example/repositories/UserRepository.java | 20 ++++ .../example/requests/CreateUserRequest.java | 49 ++++++++++ .../services/NeoUserDetailsService.java | 24 +++++ .../org/example/services/UserService.java | 33 +++++++ BE/src/main/resources/application.properties | 3 + FE | 1 + 18 files changed, 553 insertions(+) create mode 100644 BE/.gitignore create mode 100644 BE/.idea/.gitignore create mode 100644 BE/.idea/encodings.xml create mode 100644 BE/.idea/misc.xml create mode 100644 BE/.idea/vcs.xml create mode 100644 BE/pom.xml create mode 100644 BE/src/main/java/org/example/Main.java create mode 100644 BE/src/main/java/org/example/config/SecurityConfig.java create mode 100644 BE/src/main/java/org/example/controllers/UserController.java create mode 100644 BE/src/main/java/org/example/models/User.java create mode 100644 BE/src/main/java/org/example/objects/UserDTO.java create mode 100644 BE/src/main/java/org/example/queryresults/CourseEnrolmentQueryResult.java create mode 100644 BE/src/main/java/org/example/repositories/UserRepository.java create mode 100644 BE/src/main/java/org/example/requests/CreateUserRequest.java create mode 100644 BE/src/main/java/org/example/services/NeoUserDetailsService.java create mode 100644 BE/src/main/java/org/example/services/UserService.java create mode 100644 BE/src/main/resources/application.properties create mode 160000 FE diff --git a/BE/.gitignore b/BE/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/BE/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/BE/.idea/.gitignore b/BE/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/BE/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/BE/.idea/encodings.xml b/BE/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/BE/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/BE/.idea/misc.xml b/BE/.idea/misc.xml new file mode 100644 index 0000000..82dbec8 --- /dev/null +++ b/BE/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/BE/.idea/vcs.xml b/BE/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/BE/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/BE/pom.xml b/BE/pom.xml new file mode 100644 index 0000000..09fad9b --- /dev/null +++ b/BE/pom.xml @@ -0,0 +1,85 @@ + + + 4.0.0 + + org.example + demoNeo4j + 1.0-SNAPSHOT + + + 17 + ${java.version} + ${java.version} + UTF-8 + + + + + + org.springframework.boot + spring-boot-dependencies + 3.0.0 + pom + import + + + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-data-neo4j + + + org.neo4j.driver + neo4j-java-driver + + + + org.springframework.boot + spring-boot-devtools + runtime + true + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-starter-security + + + me.paulschwarz + spring-dotenv + 3.0.0 + + + org.springframework + spring-jdbc + 6.1.8 + + + org.projectlombok + lombok + provided + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/BE/src/main/java/org/example/Main.java b/BE/src/main/java/org/example/Main.java new file mode 100644 index 0000000..b61c53e --- /dev/null +++ b/BE/src/main/java/org/example/Main.java @@ -0,0 +1,11 @@ +package org.example; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Main { + public static void main(String[] args) { + SpringApplication.run(Main.class, args); + } +} \ No newline at end of file diff --git a/BE/src/main/java/org/example/config/SecurityConfig.java b/BE/src/main/java/org/example/config/SecurityConfig.java new file mode 100644 index 0000000..9492342 --- /dev/null +++ b/BE/src/main/java/org/example/config/SecurityConfig.java @@ -0,0 +1,69 @@ +package org.example.config; + +import org.example.services.NeoUserDetailsService; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.Customizer; +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.config.http.SessionCreationPolicy; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.security.web.SecurityFilterChain; +import org.springframework.web.cors.CorsConfiguration; +import org.springframework.web.cors.CorsConfigurationSource; +import org.springframework.web.cors.UrlBasedCorsConfigurationSource; + +import java.util.Arrays; + +@Configuration +@EnableWebSecurity +public class SecurityConfig { + private final NeoUserDetailsService neoUserDetailsService; + + public SecurityConfig(NeoUserDetailsService neoUserDetailsService) { + this.neoUserDetailsService = neoUserDetailsService; + } + + @Bean + SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception { + return httpSecurity + .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) + .csrf(AbstractHttpConfigurer::disable) + .cors(Customizer.withDefaults()) + .authorizeHttpRequests(auth -> auth + .requestMatchers( + "/api/v1/auth/me" + ).authenticated() + .anyRequest().permitAll() + ) + .userDetailsService(neoUserDetailsService) + .httpBasic(Customizer.withDefaults()) + .build(); + } + + @Bean + PasswordEncoder passwordEncoder() { + return new BCryptPasswordEncoder(); + } + + @Bean + CorsConfigurationSource corsConfigurationSource() { + CorsConfiguration configuration = new CorsConfiguration(); + + // TODO: make sure that the origin list comes from an environment file. + configuration.setAllowedOrigins(Arrays.asList("http://localhost:3001", "http://127.0.0.1:3000")); + configuration.setAllowedMethods(Arrays.asList("GET","POST","PATCH", "PUT", "DELETE", "OPTIONS", "HEAD")); + configuration.setAllowCredentials(true); + configuration.setAllowedHeaders(Arrays.asList("Authorization", "Requestor-Type", "Content-Type")); + configuration.setExposedHeaders(Arrays.asList("X-Get-Header")); + configuration.setMaxAge(3600L); + + UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); + source.registerCorsConfiguration("/**", configuration); + + return source; + } + +} diff --git a/BE/src/main/java/org/example/controllers/UserController.java b/BE/src/main/java/org/example/controllers/UserController.java new file mode 100644 index 0000000..b6e201c --- /dev/null +++ b/BE/src/main/java/org/example/controllers/UserController.java @@ -0,0 +1,34 @@ +package org.example.controllers; + +import org.example.models.User; +import org.example.objects.UserDTO; +import org.example.requests.CreateUserRequest; +import org.example.services.UserService; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.security.Principal; +@RestController +@RequestMapping("/api/v1/auth") +public class UserController { + private final UserService userService; + + public UserController(UserService userService) { + this.userService = userService; + } + + @GetMapping("/me") + public String loggedInUserDetails(Principal principal) { + return principal.getName(); + } + + @PostMapping("/register") + public ResponseEntity signUp(@RequestBody CreateUserRequest request) { + User user = userService.createUser(request); + + UserDTO responseUser = new UserDTO(user.getName(), user.getUsername(), user.getRoles()); + + return new ResponseEntity<>(responseUser, HttpStatus.CREATED); + } +} diff --git a/BE/src/main/java/org/example/models/User.java b/BE/src/main/java/org/example/models/User.java new file mode 100644 index 0000000..07ab36f --- /dev/null +++ b/BE/src/main/java/org/example/models/User.java @@ -0,0 +1,98 @@ +package org.example.models; + +import org.springframework.data.neo4j.core.schema.GeneratedValue; +import org.springframework.data.neo4j.core.schema.Id; +import org.springframework.data.neo4j.core.schema.Node; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.core.userdetails.UserDetails; + +import java.util.Arrays; +import java.util.Collection; + +@Node +public class User implements UserDetails { + @Id @GeneratedValue + private Long id; + private String name; + private String username; + private String password; + private String roles; + + public User() { + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + @Override + public String getUsername() { + return username; + } + + @Override + public String getPassword() { + return password; + } + + public String getRoles() { + return roles; + } + + public void setName(String name) { + this.name = name; + } + + public void setUsername(String username) { + this.username = username; + } + + public void setPassword(String password) { + this.password = password; + } + + public void setRoles(String roles) { + this.roles = roles; + } + + @Override + public boolean isAccountNonExpired() { + return true; + } + + @Override + public boolean isAccountNonLocked() { + return true; + } + + @Override + public boolean isCredentialsNonExpired() { + return true; + } + + @Override + public boolean isEnabled() { + return true; + } + + @Override + public Collection getAuthorities() { + return Arrays.stream(roles.split(",")) + .map(SimpleGrantedAuthority::new) + .toList(); + } + + @Override + public String toString() { + return "User{" + + "name='" + name + '\'' + + ", username='" + username + '\'' + + ", roles='" + roles + '\'' + + '}'; + } +} diff --git a/BE/src/main/java/org/example/objects/UserDTO.java b/BE/src/main/java/org/example/objects/UserDTO.java new file mode 100644 index 0000000..b3cfee0 --- /dev/null +++ b/BE/src/main/java/org/example/objects/UserDTO.java @@ -0,0 +1,33 @@ +package org.example.objects; + +public class UserDTO { + private String name; + private String username; + private String roles; + + public UserDTO(String name, String username, String roles) { + this.name = name; + this.username = username; + this.roles = roles; + } + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getUsername() { + return username; + } + public void setUsername(String username) { + this.username = username; + } + public String getRoles() { + return roles; + } + public void setRoles(String roles) { + this.roles = roles; + } + +} diff --git a/BE/src/main/java/org/example/queryresults/CourseEnrolmentQueryResult.java b/BE/src/main/java/org/example/queryresults/CourseEnrolmentQueryResult.java new file mode 100644 index 0000000..45256e9 --- /dev/null +++ b/BE/src/main/java/org/example/queryresults/CourseEnrolmentQueryResult.java @@ -0,0 +1,20 @@ +package org.example.queryresults; + + +import org.example.models.User; + +public class CourseEnrolmentQueryResult { + private User user; + + public CourseEnrolmentQueryResult() { + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + +} diff --git a/BE/src/main/java/org/example/repositories/UserRepository.java b/BE/src/main/java/org/example/repositories/UserRepository.java new file mode 100644 index 0000000..6ca9695 --- /dev/null +++ b/BE/src/main/java/org/example/repositories/UserRepository.java @@ -0,0 +1,20 @@ +package org.example.repositories; + +import org.example.models.User; +import org.example.queryresults.CourseEnrolmentQueryResult; +import org.springframework.data.neo4j.repository.Neo4jRepository; +import org.springframework.data.neo4j.repository.query.Query; + +import java.util.Optional; + +public interface UserRepository extends Neo4jRepository { + Optional findUserByUsername(String username); + + @Query("MATCH (user:User), (course:Course) WHERE user.username = $username AND course.identifier = $identifier " + + "RETURN EXISTS((user)-[:ENROLLED_IN]->(course))") + Boolean findEnrolmentStatus(String username, String identifier); + + @Query("MATCH (user:User), (course:Course) WHERE user.username = $username AND course.identifier = $identifier " + + "CREATE (user)-[:ENROLLED_IN]->(course) RETURN user, course") + CourseEnrolmentQueryResult createEnrolmentRelationship(String username, String identifier); +} diff --git a/BE/src/main/java/org/example/requests/CreateUserRequest.java b/BE/src/main/java/org/example/requests/CreateUserRequest.java new file mode 100644 index 0000000..43e5bbd --- /dev/null +++ b/BE/src/main/java/org/example/requests/CreateUserRequest.java @@ -0,0 +1,49 @@ +package org.example.requests; + +public class CreateUserRequest { + private String name; + private String username; + private String password; + private String roles; + + public CreateUserRequest() { + } + public CreateUserRequest(String name, String username, String password, String roles) { + this.name = name; + this.username = username; + this.password = password; + this.roles = roles; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getRoles() { + return roles; + } + + public void setRoles(String roles) { + this.roles = roles; + } +} diff --git a/BE/src/main/java/org/example/services/NeoUserDetailsService.java b/BE/src/main/java/org/example/services/NeoUserDetailsService.java new file mode 100644 index 0000000..17552d5 --- /dev/null +++ b/BE/src/main/java/org/example/services/NeoUserDetailsService.java @@ -0,0 +1,24 @@ +package org.example.services; + +import org.example.repositories.UserRepository; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.stereotype.Service; + +@Service +public class NeoUserDetailsService implements UserDetailsService { + + private final UserRepository userRepository; + + public NeoUserDetailsService(UserRepository userRepository) { + this.userRepository = userRepository; + } + + @Override + public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { + return userRepository + .findUserByUsername(username) + .orElseThrow(() -> new UsernameNotFoundException("Username not found" + username)); + } +} diff --git a/BE/src/main/java/org/example/services/UserService.java b/BE/src/main/java/org/example/services/UserService.java new file mode 100644 index 0000000..e4362ee --- /dev/null +++ b/BE/src/main/java/org/example/services/UserService.java @@ -0,0 +1,33 @@ +package org.example.services; + +import org.example.models.User; +import org.example.repositories.UserRepository; +import org.example.requests.CreateUserRequest; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.stereotype.Service; + +@Service +public class UserService { + private final UserRepository userRepository; + + private final PasswordEncoder passwordEncoder; + + public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder) { + this.userRepository = userRepository; + this.passwordEncoder = passwordEncoder; + } + + public User createUser(CreateUserRequest request) { + User user = new User(); + + user.setName(request.getName()); + // TODO: make sure that this username doesn't exist. + user.setUsername(request.getUsername()); + user.setRoles(request.getRoles()); + user.setPassword(passwordEncoder.encode(request.getPassword())); + + userRepository.save(user); + + return user; + } +} diff --git a/BE/src/main/resources/application.properties b/BE/src/main/resources/application.properties new file mode 100644 index 0000000..f808f6d --- /dev/null +++ b/BE/src/main/resources/application.properties @@ -0,0 +1,3 @@ +spring.neo4j.uri=neo4j://localhost:7687 +spring.neo4j.authentication.username=neo4j +spring.neo4j.authentication.password=12345678 \ No newline at end of file diff --git a/FE b/FE new file mode 160000 index 0000000..81ad3d1 --- /dev/null +++ b/FE @@ -0,0 +1 @@ +Subproject commit 81ad3d154b3b36b98dc5c1179c7e7bfcd7a0b1b0