dnd-side-project / dnd-5th-10-backend

IT 면접 지식 공유 플랫폼 IT'erview Backend-repo
4 stars 0 forks source link

security 현재 유저 정보 User 매핑 #18

Open RulLu16 opened 3 years ago

RulLu16 commented 3 years ago

https://freedeveloper.tistory.com/217

처럼 로그인 시 바로 jwt 토큰 해석 후 생성된 username~ 토큰 정보가 security에 있고, 이를 @CurrentUser와 같은 어노테이션을 만들어서 바로 User or userDto 에 매핑할 수 있음. 이러면 controller단에서 유효한 email 유저인지 확인해줄 필요가 없어짐.

UserPrincipal을 따로 만들고 customUserService의 loadUser 부분에서 이를 반환하게 해서 맞춰보려 했으나 실패.

그래서 일단은 아래처럼 그냥 Principal로 User를 가져와야 함.

  @ApiOperation(value = "currentuser 테스트", notes = "어노테이션이 잘 되는지 확인하기")
  @GetMapping("/test")
  public ResponseEntity getUserProfile(Principal p) {

    User user = u.findUserByEmail(p.getName()).get(); // p.getname() == email 정보
    return ResponseEntity.ok(user.getUsername());
  }

우선순위는 나중으로 예상. 필요 시 구현하기

RulLu16 commented 3 years ago

삽질하느라 시간 다썼네요..

d36choi commented 3 years ago

삽질이라뇨~ 중요한 작업같은데요 ㅋㅋ 보안상, 토큰에 담긴 요청자 정보를 컨트롤러에서 다루는일이 아마 많을 것 같습니다 (유저정보 조회 시, 토큰에 포함된 요청자 id = 조회요청된 유저id가 같아야만 조회가능이라던지)

RulLu16 commented 3 years ago

넵넵. 이게 제가 시도해보다가 oauth2랑 개념이 좀 꼬인건지 난리나서 ㅋㅋㅋㅋ 우선은 기능개발부터 했습니당