Как обрабатывать информацию о пользователе после успешного входа в Spring Security - PullRequest
0 голосов
/ 30 января 2019

Я хочу использовать информацию о пользователе после успешного входа в систему.Я думал о сохранении его в атрибуте сессии.или используя аннотацию @scope ('session).но я не нашел лучший способ сделать это.поэтому я просто сохранил его в атрибут модели.

@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserProfileService userProfileService;

    @ModelAttribute("user") 
    public User getUserModel () {

        return userService.findByEmail(SecurityContextHolder.getContext().getAuthentication().getName());
    }

    @ModelAttribute("userProfile")
    public UserProfile getUserProfile() {

        return userProfileService.findByUser(userService.findByEmail(SecurityContextHolder.getContext().getAuthentication().getName()));
    }

    @GetMapping("/")
    public String userIndex() { 

        logger.info("UserIndex");
        return "userPage";
    }

Как видите, SecurityContextHolder.getContext (). GetAuthentication (). GetName () -> этот метод повторяется.каждый раз, когда пользователь делает HTTP-запрос, это хорошая практика?или любой другой способ хранить пользовательскую информацию в приложении?

1 Ответ

0 голосов
/ 30 января 2019

Я бы пошел с этим.

@RequestMapping(value = "/username", method = RequestMethod.GET)
@ResponseBody
public String currentUserName(Authentication authentication) {
    return authentication.getName();
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...