회원 서비스 추가 || 회원 컨트롤러 수정

This commit is contained in:
byung eun park 2019-08-20 22:55:47 +09:00
parent c96d693d2d
commit 45efc8ab8c
8 changed files with 163 additions and 17 deletions

View File

@ -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.RoleRepository;
import com.totopia.server.modules.user.repository.UserRepository; import com.totopia.server.modules.user.repository.UserRepository;
import com.totopia.server.modules.user.type.RoleName; import com.totopia.server.modules.user.type.RoleName;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -32,6 +33,7 @@ import java.util.Collections;
/** /**
* Created by rajeevkumarsingh on 02/08/17. * Created by rajeevkumarsingh on 02/08/17.
*/ */
@Slf4j
@RestController @RestController
@RequestMapping("/auth") @RequestMapping("/auth")
public class AuthController { public class AuthController {
@ -57,8 +59,8 @@ public class AuthController {
@PostMapping("/signin") @PostMapping("/signin")
public ResponseEntity<?> authenticateUser(@Valid @RequestBody SigninRequest signinRequest) { public ResponseEntity<?> authenticateUser(@Valid @RequestBody SigninRequest signinRequest) {
String ipAddr = request.getRemoteAddr(); String ipAddr = this.getIp();
System.out.println("ipAddr = " + ipAddr); log.debug("ipAddr = " + ipAddr);
Authentication authentication = authenticationManager.authenticate( Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(signinRequest.getUsername(), signinRequest.getPassword())); new UsernamePasswordAuthenticationToken(signinRequest.getUsername(), signinRequest.getPassword()));
@ -100,4 +102,34 @@ public class AuthController {
return ResponseEntity.created(location).body(new ApiResponse(true, "User registered successfully")); 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;
}
} }

View File

@ -3,6 +3,7 @@ package com.totopia.server.modules.user.controller;
import com.totopia.server.commons.exception.ResourceNotFoundException; import com.totopia.server.commons.exception.ResourceNotFoundException;
import com.totopia.server.modules.user.entity.UserEntity; import com.totopia.server.modules.user.entity.UserEntity;
import com.totopia.server.modules.user.repository.UserRepository; 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.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*; import org.springframework.data.domain.*;
import org.springframework.data.web.SortDefault; import org.springframework.data.web.SortDefault;
@ -20,8 +21,8 @@ public class UserController {
@Autowired @Autowired
private UserRepository userRepository; private UserRepository userRepository;
// @Autowired @Autowired
// private SessionRegistry sessionRegistry; private UserService userService;
@PostMapping(value = "/users") @PostMapping(value = "/users")
@ResponseStatus(code = HttpStatus.CREATED) @ResponseStatus(code = HttpStatus.CREATED)
@ -31,13 +32,13 @@ public class UserController {
@GetMapping(value = "/users") @GetMapping(value = "/users")
public Page<UserEntity> all(@RequestParam(value = "username", required = false) String username, public Page<UserEntity> 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 this.userService.getUsersByPageable(username, pageable);
return userRepository.findAll(pageable);
} else {
return userRepository.findByUsernameContainingIgnoreCase(username, pageable);
}
} }
@GetMapping(value = "/connect-users") @GetMapping(value = "/connect-users")
@ -64,17 +65,14 @@ public class UserController {
} }
@GetMapping(value = "/users/{userId}") @GetMapping(value = "/users/{userId}")
public UserEntity findByUserId(@PathVariable Long 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}") @DeleteMapping(value = "/users/{userId}")
public ResponseEntity<?> deleteUser(@PathVariable Long userId) { public ResponseEntity<?> deleteUser(@PathVariable Long userId) {
return userRepository.findById(userId).map(user -> { this.userService.removeUserById(userId);
userRepository.delete(user); return ResponseEntity.ok().build();
return ResponseEntity.ok().build();
}).orElseThrow(() -> new ResourceNotFoundException("User", "userId", userId));
} }
@PutMapping(value = "/users/{userId}") @PutMapping(value = "/users/{userId}")

View File

@ -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;
}

View File

@ -85,6 +85,10 @@ public class UserEntity extends DateAuditEntity {
@JoinTable(name = "user_roles", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id")) @JoinTable(name = "user_roles", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<RoleEntity> roles; private Set<RoleEntity> roles;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "user_connect_history", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "connect_history_id"))
private Set<ConnectHistoryEntity> connectHistoryEntities;
@Basic @Basic
@Column(name = "grade", nullable = true) @Column(name = "grade", nullable = true)
private Short grade; private Short grade;

View File

@ -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<ConnectHistoryEntity, Long> {
}

View File

@ -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<UserEntity> getUsersByPageable(String username, Pageable pageable) {
Page<UserEntity> 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));
}
}

View File

@ -45,7 +45,7 @@ logging:
pattern: pattern:
console: "%d %-5level %logger : %msg%n" console: "%d %-5level %logger : %msg%n"
level: level:
root: ERROR root: DEBUG
org.springframework: INFO org.springframework: INFO
org.hibernate: DEBUG org.hibernate: DEBUG

View File

@ -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());
}
}