@GetMapping("/user/me") public UserDto me(@AuthenticationPrincipal JwtAuthentication authentication) { return userService.findByUsername(authentication.username) .map(user -> new UserDto(authentication.token, authentication.username, user.getGroup().getName())) .orElseThrow(() -> new IllegalArgumentException("Could not found user for " + authentication.username)); }
문제점
수업에 나온 User정보를 받아오는 코드다
하지만 실제로 OAuth2 셋팅을 하고 실제로 유저아이디를 만든 뒤 정보를 받아오려고 하면 authentication 파라미터가 null이 뜬다.
해결책
antMatchers에 등록되지 않은 사이트여서 발생하였던 오류였다. antMatchers에 등록되지 않은 페이지나 권한을 갖지 않는다면 Authentication을 받아오는 과정에서 null이 되는듯하다.