Spring Boot 2 WebFlux中获取当前登录用户

在Spring Boot 2中,假如使用webflux,在Controller中直接使用以下代码是获取不到当前登录用户的:

UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

SecurityContextHolder.getContext().getAuthentication()为null,可以参考以下示例获取:

    @GetMapping("")
    public String index(Model model, Authentication authentication) {
        if (null != authentication && authentication.isAuthenticated()) {
            UserDetails userDetails = (UserDetails) authentication.getPrincipal();

            if (null != userDetails) {

            }
        }

        return "home/index";
    }

你可能感兴趣的:(Spring Boot 2 WebFlux中获取当前登录用户)