From 45efc8ab8c310d2488059174e94476d5eb7b0fc4 Mon Sep 17 00:00:00 2001 From: byung eun park Date: Tue, 20 Aug 2019 22:55:47 +0900 Subject: [PATCH] =?UTF-8?q?=ED=9A=8C=EC=9B=90=20=EC=84=9C=EB=B9=84?= =?UTF-8?q?=EC=8A=A4=20=EC=B6=94=EA=B0=80=20||=20=ED=9A=8C=EC=9B=90=20?= =?UTF-8?q?=EC=BB=A8=ED=8A=B8=EB=A1=A4=EB=9F=AC=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/controller/AuthController.java | 36 +++++++++++++++- .../user/controller/UserController.java | 26 ++++++------ .../user/entity/ConnectHistoryEntity.java | 28 +++++++++++++ .../modules/user/entity/UserEntity.java | 4 ++ .../repository/ConnectHistoryRepository.java | 7 ++++ .../modules/user/service/UserService.java | 42 +++++++++++++++++++ src/main/resources/application.yml | 2 +- .../modules/user/service/UserServiceTest.java | 35 ++++++++++++++++ 8 files changed, 163 insertions(+), 17 deletions(-) create mode 100644 src/main/java/com/totopia/server/modules/user/entity/ConnectHistoryEntity.java create mode 100644 src/main/java/com/totopia/server/modules/user/repository/ConnectHistoryRepository.java create mode 100644 src/main/java/com/totopia/server/modules/user/service/UserService.java create mode 100644 src/test/java/com/totopia/server/modules/user/service/UserServiceTest.java diff --git a/src/main/java/com/totopia/server/auth/controller/AuthController.java b/src/main/java/com/totopia/server/auth/controller/AuthController.java index 220d9f8..fc3f3f0 100644 --- a/src/main/java/com/totopia/server/auth/controller/AuthController.java +++ b/src/main/java/com/totopia/server/auth/controller/AuthController.java @@ -10,6 +10,7 @@ import com.totopia.server.modules.user.entity.UserEntity; import com.totopia.server.modules.user.repository.RoleRepository; import com.totopia.server.modules.user.repository.UserRepository; import com.totopia.server.modules.user.type.RoleName; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -32,6 +33,7 @@ import java.util.Collections; /** * Created by rajeevkumarsingh on 02/08/17. */ +@Slf4j @RestController @RequestMapping("/auth") public class AuthController { @@ -57,8 +59,8 @@ public class AuthController { @PostMapping("/signin") public ResponseEntity authenticateUser(@Valid @RequestBody SigninRequest signinRequest) { - String ipAddr = request.getRemoteAddr(); - System.out.println("ipAddr = " + ipAddr); + String ipAddr = this.getIp(); + log.debug("ipAddr = " + ipAddr); Authentication authentication = authenticationManager.authenticate( new UsernamePasswordAuthenticationToken(signinRequest.getUsername(), signinRequest.getPassword())); @@ -100,4 +102,34 @@ public class AuthController { return ResponseEntity.created(location).body(new ApiResponse(true, "User registered successfully")); } + + private String getIp() { + String ip = request.getHeader("X-Forwarded-For"); + + log.info(">>>> X-FORWARDED-FOR : " + ip); + + if (ip == null) { + ip = request.getHeader("Proxy-Client-IP"); + log.info(">>>> Proxy-Client-IP : " + ip); + } + if (ip == null) { + ip = request.getHeader("WL-Proxy-Client-IP"); // 웹로직 + log.info(">>>> WL-Proxy-Client-IP : " + ip); + } + if (ip == null) { + ip = request.getHeader("HTTP_CLIENT_IP"); + log.info(">>>> HTTP_CLIENT_IP : " + ip); + } + if (ip == null) { + ip = request.getHeader("HTTP_X_FORWARDED_FOR"); + log.info(">>>> HTTP_X_FORWARDED_FOR : " + ip); + } + if (ip == null) { + ip = request.getRemoteAddr(); + } + + log.info(">>>> Result : IP Address : "+ip); + + return ip; + } } diff --git a/src/main/java/com/totopia/server/modules/user/controller/UserController.java b/src/main/java/com/totopia/server/modules/user/controller/UserController.java index 8ca1b94..79960e1 100644 --- a/src/main/java/com/totopia/server/modules/user/controller/UserController.java +++ b/src/main/java/com/totopia/server/modules/user/controller/UserController.java @@ -3,6 +3,7 @@ package com.totopia.server.modules.user.controller; import com.totopia.server.commons.exception.ResourceNotFoundException; import com.totopia.server.modules.user.entity.UserEntity; import com.totopia.server.modules.user.repository.UserRepository; +import com.totopia.server.modules.user.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.*; import org.springframework.data.web.SortDefault; @@ -20,8 +21,8 @@ public class UserController { @Autowired private UserRepository userRepository; -// @Autowired -// private SessionRegistry sessionRegistry; + @Autowired + private UserService userService; @PostMapping(value = "/users") @ResponseStatus(code = HttpStatus.CREATED) @@ -31,13 +32,13 @@ public class UserController { @GetMapping(value = "/users") public Page all(@RequestParam(value = "username", required = false) String username, - @SortDefault.SortDefaults({ @SortDefault(sort = "createdAt", direction = Sort.Direction.DESC) }) Pageable pageable) { + @SortDefault.SortDefaults({ + @SortDefault( + sort = "createdAt", + direction = Sort.Direction.DESC) }) + Pageable pageable) { - if (null == username) { - return userRepository.findAll(pageable); - } else { - return userRepository.findByUsernameContainingIgnoreCase(username, pageable); - } + return this.userService.getUsersByPageable(username, pageable); } @GetMapping(value = "/connect-users") @@ -64,17 +65,14 @@ public class UserController { } @GetMapping(value = "/users/{userId}") public UserEntity findByUserId(@PathVariable Long userId) { - return userRepository.findById(userId).orElseThrow(() -> new ResourceNotFoundException("User", "userId", userId)); + return this.userService.getUserById(userId); } @DeleteMapping(value = "/users/{userId}") public ResponseEntity deleteUser(@PathVariable Long userId) { - return userRepository.findById(userId).map(user -> { - userRepository.delete(user); - return ResponseEntity.ok().build(); - }).orElseThrow(() -> new ResourceNotFoundException("User", "userId", userId)); - + this.userService.removeUserById(userId); + return ResponseEntity.ok().build(); } @PutMapping(value = "/users/{userId}") diff --git a/src/main/java/com/totopia/server/modules/user/entity/ConnectHistoryEntity.java b/src/main/java/com/totopia/server/modules/user/entity/ConnectHistoryEntity.java new file mode 100644 index 0000000..213a222 --- /dev/null +++ b/src/main/java/com/totopia/server/modules/user/entity/ConnectHistoryEntity.java @@ -0,0 +1,28 @@ +package com.totopia.server.modules.user.entity; + +import com.totopia.server.commons.data.entity.DateAuditEntity; +import lombok.*; +import lombok.experimental.SuperBuilder; + +import javax.persistence.*; +import java.io.Serializable; + +@Entity(name = "deposit") +@SuperBuilder +@NoArgsConstructor +@AllArgsConstructor +@Getter +@Setter +@ToString +@Data +public class ConnectHistoryEntity extends DateAuditEntity implements Serializable { + + @Id + @GeneratedValue(generator = "connect_history_generator") + @SequenceGenerator(name = "connect_history_generator", sequenceName = "connect_history_sequence", initialValue = 1) + private Long id; + + @Basic + @Column(name = "activation", nullable = true, length = 100) + private String ip; +} diff --git a/src/main/java/com/totopia/server/modules/user/entity/UserEntity.java b/src/main/java/com/totopia/server/modules/user/entity/UserEntity.java index 1285c2b..6d4a623 100644 --- a/src/main/java/com/totopia/server/modules/user/entity/UserEntity.java +++ b/src/main/java/com/totopia/server/modules/user/entity/UserEntity.java @@ -85,6 +85,10 @@ public class UserEntity extends DateAuditEntity { @JoinTable(name = "user_roles", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id")) private Set roles; + @ManyToMany(fetch = FetchType.EAGER) + @JoinTable(name = "user_connect_history", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "connect_history_id")) + private Set connectHistoryEntities; + @Basic @Column(name = "grade", nullable = true) private Short grade; diff --git a/src/main/java/com/totopia/server/modules/user/repository/ConnectHistoryRepository.java b/src/main/java/com/totopia/server/modules/user/repository/ConnectHistoryRepository.java new file mode 100644 index 0000000..37fa352 --- /dev/null +++ b/src/main/java/com/totopia/server/modules/user/repository/ConnectHistoryRepository.java @@ -0,0 +1,7 @@ +package com.totopia.server.modules.user.repository; + +import com.totopia.server.modules.user.entity.ConnectHistoryEntity; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface ConnectHistoryRepository extends JpaRepository { +} diff --git a/src/main/java/com/totopia/server/modules/user/service/UserService.java b/src/main/java/com/totopia/server/modules/user/service/UserService.java new file mode 100644 index 0000000..3477dc5 --- /dev/null +++ b/src/main/java/com/totopia/server/modules/user/service/UserService.java @@ -0,0 +1,42 @@ +package com.totopia.server.modules.user.service; + +import com.totopia.server.commons.exception.ResourceNotFoundException; +import com.totopia.server.modules.user.entity.UserEntity; +import com.totopia.server.modules.user.repository.UserRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; + +@Service +public class UserService { + + @Autowired + private UserRepository userRepository; + + public Page getUsersByPageable(String username, Pageable pageable) { + Page entities = null; + + if (null == username) { + entities = userRepository.findAll(pageable); + } else { + entities = userRepository.findByUsernameContainingIgnoreCase(username, pageable); + } + + return entities; + } + + public UserEntity getUserById(Long id) { + UserEntity userEntity = this.userRepository.findById(id) + .orElseThrow( () -> new ResourceNotFoundException("User", "userId", id)); + + return userEntity; + } + + public Long removeUserById(Long userId) { + return userRepository.findById(userId).map(user -> { + userRepository.delete(user); + return userId; + }).orElseThrow(() -> new ResourceNotFoundException("User", "userId", userId)); + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 6da1e7e..e9404e9 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -45,7 +45,7 @@ logging: pattern: console: "%d %-5level %logger : %msg%n" level: - root: ERROR + root: DEBUG org.springframework: INFO org.hibernate: DEBUG diff --git a/src/test/java/com/totopia/server/modules/user/service/UserServiceTest.java b/src/test/java/com/totopia/server/modules/user/service/UserServiceTest.java new file mode 100644 index 0000000..d91fe31 --- /dev/null +++ b/src/test/java/com/totopia/server/modules/user/service/UserServiceTest.java @@ -0,0 +1,35 @@ +package com.totopia.server.modules.user.service; + +import lombok.extern.slf4j.Slf4j; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.junit.Assert.*; + +@Ignore +@RunWith(SpringRunner.class) +@SpringBootTest +@Slf4j +public class UserServiceTest { + + @Autowired + private UserService userService; + + @Test + public void getUsersByPageable() { + } + + @Test + public void getUserById() { + } + + @Test + public void removeUserById() { + Object userObj = this.userService.removeUserById(2L); + log.debug(userObj.toString()); + } +} \ No newline at end of file