Я пытаюсь получить имя пользователя следующим образом:
public static String getUsername() {
SecurityContext context = SecurityContextHolder.getContext();
UserDetails userDetails = (UserDetails) context.getAuthentication().getPrincipal();
return userDetails.getUsername();
}
Работает нормально, только когда залогинен, но при переходе на другую страницу контекст становится org.springframework.security.core.context.SecurityContextImpl@ffffffff: Null authentication
с authentification = null
.
Как правильно восстановить зарегистрированного пользователя с помощью Spring Security в Spring Boot?
Версия Spring Boot 2.0.3.RELEASE.
UPD:
Чтобы уточнить: после перехода на другую страницу SecurityContextHolder.getContext()
дает контекст с authentification = null
.Так что context.getAuthentication().getPrincipal()
бросает NullPointerException
, потому что context.getAuthentication()
равно нулю.