Skip to content

Commit

Permalink
[#109] Refactor: HttpSessionUtils 클래스 정의 및 기타
Browse files Browse the repository at this point in the history
- 로그인 세션과 관련된 기능을 모아둔 HttpSessionUtils 클래스 정의
- User 클래스에서만 사용하는 Gender enum을 중첩 enum 으로 선언
- ApiUserController에서 InvalidParameterException에 대한 에러 응답 객체를 반환하도록 변경
  • Loading branch information
beginin15 authored and ksundong committed Mar 27, 2020
1 parent 604d1ec commit 79dfbe9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.codesquad.team1.signup.common.utils;

import com.codesquad.team1.signup.domain.user.User;

import javax.servlet.http.HttpSession;
import java.util.Optional;

public class HttpSessionUtils {

public static final String SESSION_LOGINED_USER = "loginedUser";

private HttpSessionUtils() {}

public static Optional<User> getLoginedUserFromSession(HttpSession session) {
return Optional.ofNullable((User)session.getAttribute(SESSION_LOGINED_USER));
}

public static void logout(HttpSession session) {
session.removeAttribute(SESSION_LOGINED_USER);
session.invalidate();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.codesquad.team1.signup.controller;

import com.codesquad.team1.signup.Exception.ErrorResponse;
import com.codesquad.team1.signup.Exception.ForbiddenException;
import com.codesquad.team1.signup.Exception.UnauthorizedException;
import com.codesquad.team1.signup.repository.User;
import com.codesquad.team1.signup.repository.UserRepository;
import com.codesquad.team1.signup.response.ValidationResponse;
package com.codesquad.team1.signup.web.api;

import com.codesquad.team1.signup.common.constants.ErrorMessages;
import com.codesquad.team1.signup.common.response.ErrorResponse;
import com.codesquad.team1.signup.common.exception.ForbiddenException;
import com.codesquad.team1.signup.common.exception.UnauthorizedException;
import com.codesquad.team1.signup.common.utils.HttpSessionUtils;
import com.codesquad.team1.signup.domain.user.User;
import com.codesquad.team1.signup.domain.user.UserRepository;
import com.codesquad.team1.signup.common.response.ValidationResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
Expand All @@ -14,14 +16,12 @@

import javax.servlet.http.HttpSession;
import java.security.InvalidParameterException;
import java.util.Optional;

@RestController
@RequestMapping("/api/users")
public class ApiUserController {

private static final Logger log = LoggerFactory.getLogger(ApiUserController.class);
private static final String SESSION_USER_KEY = "sessionedUser";

private UserRepository userRepository;

Expand Down Expand Up @@ -59,7 +59,7 @@ public User createUser(@RequestBody User user) {
if (user.validate()) {
return userRepository.save(user);
}
throw new InvalidParameterException("Validation을 통과하지 못한 값 입니다.");
throw new InvalidParameterException(ErrorMessages.INVALID_PARAMETER);
}

@PostMapping("/login")
Expand All @@ -68,23 +68,22 @@ public ValidationResponse login(@RequestBody User loginUser, HttpSession session
if (!user.matchPassword(loginUser)) {
return ValidationResponse.isFail();
}
session.setAttribute(SESSION_USER_KEY, user);
session.setAttribute(HttpSessionUtils.SESSION_LOGINED_USER, user);
return ValidationResponse.isSuccess();
}

@PostMapping("/logout")
public ValidationResponse logout(HttpSession session) {
session.removeAttribute(SESSION_USER_KEY);
session.invalidate();
HttpSessionUtils.logout(session);
return ValidationResponse.isSuccess();
}

@GetMapping("/{id}")
public User showPersonalInformation(@PathVariable String id, HttpSession session) {
User sessionedUser = Optional.ofNullable((User)session.getAttribute(SESSION_USER_KEY)).orElseThrow(() -> new UnauthorizedException("로그인이 필요합니다."));
User sessionUser = HttpSessionUtils.getLoginedUserFromSession(session).orElseThrow(() -> new UnauthorizedException(ErrorMessages.UNAUTHORIZED));
User requestedUser = userRepository.findById(id).orElseGet(User::new);
if (!sessionedUser.equals(requestedUser)) {
throw new ForbiddenException("접근 권한이 없습니다.");
if (!sessionUser.equals(requestedUser)) {
throw new ForbiddenException(ErrorMessages.FORBIDDEN);
}
return requestedUser;
}
Expand All @@ -98,4 +97,9 @@ public ErrorResponse handleUnauthorizedException(UnauthorizedException e) {
public ErrorResponse handleForbiddenException(ForbiddenException e) {
return new ErrorResponse(e.getMessage(), HttpStatus.FORBIDDEN.value());
}

@ExceptionHandler(InvalidParameterException.class)
public ErrorResponse handleInvalidParameterException(InvalidParameterException e) {
return new ErrorResponse(e.getMessage(), HttpStatus.UNPROCESSABLE_ENTITY.value());
}
}

0 comments on commit 79dfbe9

Please sign in to comment.