Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UserDebugService 사용법 #49

Open
dusdjhyeon opened this issue Jun 8, 2023 · 0 comments
Open

UserDebugService 사용법 #49

dusdjhyeon opened this issue Jun 8, 2023 · 0 comments

Comments

@dusdjhyeon
Copy link
Contributor

dusdjhyeon commented Jun 8, 2023

package com.fs.filemarket.api.domain.user.service;

import com.fs.filemarket.api.domain.user.User;
import com.fs.filemarket.api.domain.user.repository.UserRepository;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.HttpStatus;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;

@Service
public class UserDebugService extends UserService{
    private final HttpServletRequest request;

    public UserDebugService(UserRepository userRepository, PasswordEncoder passwordEncoder, HttpServletRequest request) {
        super(userRepository, passwordEncoder);
        this.request = request;
    }

    private Integer getUserIdInHeader() {
        String userIdString = request.getHeader("userId");

        if (userIdString == null)
            throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "Unauthorized");
        try {
            return Integer.parseInt(userIdString);
        } catch (NumberFormatException e) {
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "userID를 파싱할 수 없습니다.");
        }
    }

    @Override
    public User getCurrentUser() {
        return userRepository.getById(getUserIdInHeader());
    }
}

해당 코드 파일(UserDebugService.java를 commit, push하지 마세요)

repository에서의 코드는 UserService.java만 존재해야 하며, @service 어노테이션 역시 UserService에 붙어 있어야 합니다.

  1. user의 service 폴더 안에 UserDebugService 자바 파일을 만듭니다.
  2. 해당 코드를 복붙합니다.
  3. UserSerivce.java 파일의 @service 어노테이션을 제거 합니다.

이렇게 하면, PostMan에서 Hedars에 userId 값을 통해 현재 유저 설정이 가능합니다.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant