Skip to content

[feat-1059]: задокументировано поведение getCurrentUser()#1075

Open
Textile86 wants to merge 4 commits intohexlet-volunteers:mainfrom
Textile86:isuue-1059
Open

[feat-1059]: задокументировано поведение getCurrentUser()#1075
Textile86 wants to merge 4 commits intohexlet-volunteers:mainfrom
Textile86:isuue-1059

Conversation

@Textile86
Copy link
Copy Markdown

@Textile86 Textile86 commented Apr 6, 2026

Closes #1059

  • Добавлен Javadoc к методу getCurrentUser() с описанием поведения и исключений
  • Вместо возврата null для неавторизованного пользователя метод теперь бросает InsufficientAuthenticationException

var authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null || !authentication.isAuthenticated()) {
return null;
throw new InsufficientAuthenticationException("User is not authenticated");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Какие есть основания в пользу того, чтобы использовать именно InsufficientAuthenticationException? Что именно мы скажем клиенты этим exception'ом?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Выбрасывая InsufficientAuthenticationException мы говорим клиенту что его учетные данные отсутствуют и что ему нужно пройти аутентификацию

Это исключение лучше всего подходит потому что чутко указывает на отсутствие аутентификации, правильно обрабатывается Spring Security фильтрами, возвращает понятный статус 401 для клиента.

// на потом чтобы работать с залогиненым юзером
/**
* Метод возвращает текущего аутентифицированного пользователя.
* Метод извлекает email из Spring Security и загружает соответствующего пользователя
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"извлекает из Spring Security" предлагаю сформулировать точнее, типа такого:

@return текущий {@link User} из репозитория по email из контекста аутентификации

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Добавлено

var email = authentication.getName();
return userRepository.findByEmail(email).get();
return userRepository.findByEmail(email)
.orElseThrow(() -> new UserNotFoundException("User not found: " + email));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это должно быть исправлено в рамках другой ишью

@ann-p-1320
Copy link
Copy Markdown

аппрув

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

Successfully merging this pull request may close these issues.

Backend: задокументировать поведение getCurrentUser()

2 participants