From f8f9f12dcca228a86ac8a0785f15479e0363c2e5 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Fri, 15 May 2020 16:02:19 +0200 Subject: [PATCH] Bugfix data-initialization Bugfix login Bugfix sign-up Docker-compose restart backend always --- .../geotime/controllers/UserController.java | 28 +++++++++++++++---- .../security/JWTAuthenticationFilter.java | 7 +++-- .../main/resources/application-dev.properties | 1 + .../resources/application-prod.properties | 1 - backend/src/main/resources/data.sql | 4 ++- docker-compose.yml | 8 ++++-- 6 files changed, 37 insertions(+), 12 deletions(-) diff --git a/backend/src/main/java/de/hft/geotime/controllers/UserController.java b/backend/src/main/java/de/hft/geotime/controllers/UserController.java index e1cd793..e5acae4 100644 --- a/backend/src/main/java/de/hft/geotime/controllers/UserController.java +++ b/backend/src/main/java/de/hft/geotime/controllers/UserController.java @@ -13,6 +13,8 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; +import java.util.HashMap; + @RestController public class UserController { @@ -29,15 +31,31 @@ public class UserController { @GetMapping("/whoami") public UserAllEmbeddedProjection getUsername(Authentication authentication) { TimetrackUser user = userRepository.findFirstByUsername(authentication.getName()); - return projectionFactory.createProjection(UserAllEmbeddedProjection.class, user); + if (user != null) { + return projectionFactory.createProjection(UserAllEmbeddedProjection.class, user); + } else { + return null; + } } @PostMapping("/sign-up") - public ResponseEntity signUp(@RequestBody TimetrackUser user) { - user.setPassword(bCryptPasswordEncoder.encode(user.getPassword())); - TimetrackUser byUsername = userRepository.findFirstByUsername(user.getUsername()); + public ResponseEntity signUp(@RequestBody HashMap signUpData) { + if (signUpData.get("username") == null + || signUpData.get("password") == null + || signUpData.get("firstname") == null + || signUpData.get("lastname") == null) { + return new ResponseEntity<>("Missing information", HttpStatus.BAD_REQUEST); + } + + TimetrackUser newUser = new TimetrackUser(); + newUser.setFirstname(signUpData.get("firstname")); + newUser.setLastname(signUpData.get("lastname")); + newUser.setPassword(bCryptPasswordEncoder.encode(signUpData.get("password"))); + newUser.setUsername(signUpData.get("username")); + + TimetrackUser byUsername = userRepository.findFirstByUsername(newUser.getUsername()); if (byUsername == null) { - userRepository.save(user); + userRepository.save(newUser); return new ResponseEntity<>("Created", HttpStatus.CREATED); } else { return new ResponseEntity<>("Username already exists!", HttpStatus.CONFLICT); diff --git a/backend/src/main/java/de/hft/geotime/security/JWTAuthenticationFilter.java b/backend/src/main/java/de/hft/geotime/security/JWTAuthenticationFilter.java index 7ca7442..e63aa7d 100644 --- a/backend/src/main/java/de/hft/geotime/security/JWTAuthenticationFilter.java +++ b/backend/src/main/java/de/hft/geotime/security/JWTAuthenticationFilter.java @@ -15,6 +15,7 @@ import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.ArrayList; import java.util.Date; +import java.util.HashMap; import static com.auth0.jwt.algorithms.Algorithm.HMAC512; import static de.hft.geotime.security.SecurityConstants.*; @@ -31,11 +32,11 @@ public class JWTAuthenticationFilter extends UsernamePasswordAuthenticationFilte HttpServletRequest req, HttpServletResponse res) throws AuthenticationException { try { - LoginUser creds = new ObjectMapper().readValue(req.getInputStream(), LoginUser.class); + HashMap creds = new ObjectMapper().readValue(req.getInputStream(), HashMap.class); return authenticationManager.authenticate( new UsernamePasswordAuthenticationToken( - creds.getUsername(), - creds.getPassword(), + creds.get("username"), + creds.get("password"), new ArrayList<>() ) ); diff --git a/backend/src/main/resources/application-dev.properties b/backend/src/main/resources/application-dev.properties index 4497ed1..d1af305 100644 --- a/backend/src/main/resources/application-dev.properties +++ b/backend/src/main/resources/application-dev.properties @@ -4,4 +4,5 @@ spring.datasource.username=sa spring.datasource.password= spring.jpa.database-platform=org.hibernate.dialect.H2Dialect spring.h2.console.enabled=true +spring.datasource.initialization-mode=always spring.h2.console.path=/h2-console \ No newline at end of file diff --git a/backend/src/main/resources/application-prod.properties b/backend/src/main/resources/application-prod.properties index 2ed9234..63f56f2 100644 --- a/backend/src/main/resources/application-prod.properties +++ b/backend/src/main/resources/application-prod.properties @@ -2,5 +2,4 @@ spring.jpa.hibernate.ddl-auto=update spring.datasource.url=jdbc:mariadb://db:3306/geotime spring.datasource.username=root spring.datasource.password=supersecure -spring.datasource.initialization-mode=always spring.datasource.driver-class-name=org.mariadb.jdbc.Driver \ No newline at end of file diff --git a/backend/src/main/resources/data.sql b/backend/src/main/resources/data.sql index 49f0053..3f9bdf3 100644 --- a/backend/src/main/resources/data.sql +++ b/backend/src/main/resources/data.sql @@ -1,7 +1,9 @@ SET FOREIGN_KEY_CHECKS=0; -DELETE FROM timetrack_user; DELETE FROM role; +DELETE FROM location; +DELETE FROM timetrack_user; +DELETE FROM timetrack_account; INSERT INTO role (id, `name`) VALUES (1, 'Admin'); diff --git a/docker-compose.yml b/docker-compose.yml index ccd86b9..e5daad0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,6 +12,7 @@ services: backend: container_name: qbc_backend + restart: always build: context: ./backend ports: @@ -25,7 +26,10 @@ services: build: context: ./sql volumes: - - "./sql/db-data:/var/lib/mysql" + - "qbc-db-data:/var/lib/mysql" environment: MYSQL_DATABASE: geotime - MYSQL_ROOT_PASSWORD: supersecure \ No newline at end of file + MYSQL_ROOT_PASSWORD: supersecure + +volumes: + qbc-db-data: \ No newline at end of file