Merge branch 'be/user' into 'backend'

[Back-End] feat: user중복로그인 체크

See merge request s11-webmobile1-sub2/S11P12A701!3
This commit is contained in:
박정민 2024-08-01 10:53:15 +09:00
commit 25bcd5203f
3 changed files with 17 additions and 33 deletions

View File

@ -33,29 +33,22 @@ public class UserController {
@PostMapping("/join") @PostMapping("/join")
public ResponseEntity<String> join(@RequestBody User user) throws Exception { public ResponseEntity<String> join(@RequestBody User user) throws Exception {
System.out.println("@@@@@");
log.info("@@@@@@@@@@@@@@@@");
userService.join(user); userService.join(user);
return ResponseEntity.ok("User registered successfully"); return ResponseEntity.ok("User registered successfully");
} }
@PostMapping("/findpassword/{user_id}") @PostMapping("/findpassword/{user_id}")
public ResponseEntity<String> findpassword(@PathVariable("user_id") Long user_id) throws Exception { public ResponseEntity<String> findpassword(@PathVariable("user_id") Long user_id) throws Exception {
userService.userCheck(user_id); userService.userCheck(user_id);
return ResponseEntity.ok("임시 비밀번호가 이메일로 전송되었습니다."); return ResponseEntity.ok("임시 비밀번호가 이메일로 전송되었습니다.");
} }
@PutMapping("/updateinfo") @PutMapping("/updateinfo")
public ResponseEntity<String> updateUserInfo( public ResponseEntity<String> updateUserInfo(
@RequestBody InfoDto infoDto, HttpServletRequest request) { @RequestBody InfoDto infoDto, HttpServletRequest request) {
try { try {
String token = request.getHeader("Authorization"); String token = request.getHeader("Authorization");
Long userId = Long.parseLong(jwtUtil.getUserId(token)); Long userId = Long.parseLong(jwtUtil.getUserId(token));
userService.changeuInfo(infoDto, userId); userService.changeUserInfo(infoDto, userId);
return ResponseEntity.ok("User info updated successfully"); return ResponseEntity.ok("User info updated successfully");
} catch (Exception e) { } catch (Exception e) {
return ResponseEntity.badRequest().body(e.getMessage()); return ResponseEntity.badRequest().body(e.getMessage());
@ -80,36 +73,35 @@ public class UserController {
@Operation(summary = "로그인", description = "아이디와 비밀번호를 이용하여 로그인 처리.") @Operation(summary = "로그인", description = "아이디와 비밀번호를 이용하여 로그인 처리.")
@PostMapping("/login") @PostMapping("/login")
public ResponseEntity<Map<String, Object>> login( public ResponseEntity<Map<String, Object>> login(
@RequestBody @Parameter(description = "로그인 시 필요한 회원정보(아이디, 비밀번호).", required = true) User user, HttpServletResponse response) { @RequestBody @Parameter(description = "로그인 시 필요한 회원정보(아이디, 비밀번호).", required = true) User user, HttpServletRequest request, HttpServletResponse response) {
String token = request.getHeader("Authorization");
if(jwtUtil.checkToken(token)){
return new ResponseEntity<>(HttpStatus.FORBIDDEN);
}
Map<String, Object> resultMap = new HashMap<>(); Map<String, Object> resultMap = new HashMap<>();
HttpStatus status = HttpStatus.ACCEPTED; HttpStatus status = HttpStatus.ACCEPTED;
try { try {
User loginUser = userService.login(user); User loginUser = userService.login(user);
if (loginUser != null) { if (loginUser != null) {
String name = loginUser.getName();
resultMap.put("name",name);
String accessToken = jwtUtil.createAccessToken(String.valueOf(loginUser.getId())); String accessToken = jwtUtil.createAccessToken(String.valueOf(loginUser.getId()));
String refreshToken = jwtUtil.createRefreshToken(String.valueOf(loginUser.getId())); String refreshToken = jwtUtil.createRefreshToken(String.valueOf(loginUser.getId()));
userService.saveRefreshToken(loginUser.getId(), refreshToken); userService.saveRefreshToken(loginUser.getId(), refreshToken);
resultMap.put("name",loginUser.getName());
resultMap.put("role",loginUser.getRole()); resultMap.put("role",loginUser.getRole());
resultMap.put("access-token", accessToken); resultMap.put("access-token", accessToken);
Cookie refreshCookie = new Cookie("refresh-token", refreshToken); Cookie refreshCookie = new Cookie("refresh-token", refreshToken);
refreshCookie.setPath("/"); refreshCookie.setPath("/");
refreshCookie.setHttpOnly(true); refreshCookie.setHttpOnly(true);
refreshCookie.setSecure(true); // HTTPS에서만 전송되도록 설정 refreshCookie.setSecure(true); // HTTPS에서만 전송되도록 설정
//r/efreshCookie.setSameSite(Cookie.SameSite.NONE); // Cross-Origin 요청에 대해 모두 전송 //refreshCookie.setSameSite(Cookie.SameSite.NONE); // Cross-Origin 요청에 대해 모두 전송
//refreshCookie.setSameSite("None"); // Cross-Origin 요청에 대해 모두 전송 //refreshCookie.setSameSite("None"); // Cross-Origin 요청에 대해 모두 전송
String cookieHeader = String.format("refresh-token=%s; Path=/; HttpOnly; Secure; SameSite=None", refreshToken); String cookieHeader = String.format("refresh-token=%s; Path=/; HttpOnly; Secure; SameSite=None", refreshToken);
@ -118,7 +110,6 @@ public class UserController {
// refreshCookie.setSameSite("None"); // Cross-Origin 요청에 대해 모두 전송 // refreshCookie.setSameSite("None"); // Cross-Origin 요청에 대해 모두 전송
response.addCookie(refreshCookie); response.addCookie(refreshCookie);
status = HttpStatus.CREATED; status = HttpStatus.CREATED;
} else { } else {
resultMap.put("message", "아이디 또는 패스워드를 확인해 주세요."); resultMap.put("message", "아이디 또는 패스워드를 확인해 주세요.");

View File

@ -14,7 +14,7 @@ public interface UserService {
void sendEamail(User user) throws Exception; void sendEamail(User user) throws Exception;
void userCheck(Long id) throws Exception; void userCheck(Long id) throws Exception;
String getUserName(Long id) throws Exception; String getUserName(Long id) throws Exception;
void changeuInfo(InfoDto infoDto,Long id) throws Exception; void changeUserInfo(InfoDto infoDto,Long id) throws Exception;
void changePassword(PasswordDto passwordDto,Long id) throws Exception; void changePassword(PasswordDto passwordDto,Long id) throws Exception;
} }

View File

@ -119,23 +119,16 @@ public class UserServiceImpl implements UserService {
@Override @Override
public void changeuInfo(InfoDto infoDto, Long id) throws Exception { public void changeUserInfo(InfoDto infoDto, Long id) throws Exception {
User user = userRepository.findById(id).orElse(null); User user = userRepository.findById(id).orElseThrow(IllegalArgumentException::new);
if (user == null) {
throw new Exception("User not found");
}
if (infoDto.getName() != null) if (infoDto.getName() != null)
{
user.setName(infoDto.getName()); user.setName(infoDto.getName());
}
if(infoDto.getEmail()!=null) if(infoDto.getEmail()!=null)
{
user.setEmail(infoDto.getEmail()); user.setEmail(infoDto.getEmail());
}
userRepository.save(user); userRepository.save(user);
} }