Spring Security——获取当前已登录用户UserDetails对象

源代码 

SecurityContextHolder.getContext().getAuthentication().getPrincipal()

对于WebSecurityConfigurer中关于认证成功的 AuthenticationSuccessHandler 配置DEMO

.successHandler(new AuthenticationSuccessHandler() {
                    @Override
                    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
                        httpServletResponse.setContentType("application/json;charset=UTF-8");
                        ApiResponse retTemp = ApiResponseUtil.getRetTemp();
                        retTemp.setCode("200");
                        retTemp.setMsg("登录成功");
                        retTemp.setData(authentication.getPrincipal());
                        PrintWriter out = httpServletResponse.getWriter();
                        out.write(new ObjectMapper().writeValueAsString(retTemp));
                        out.flush();
                        out.close();
                    }
                })

学习资源

《SpringBoot+Vue全栈开发实战》示例源代码:https://pan.baidu.com/s/1SYxcHPFe2HfhlVoHklcB1w  

参考文章

 https://github.com/lenve/vhr

发布了1430 篇原创文章 · 获赞 260 · 访问量 42万+

猜你喜欢

转载自blog.csdn.net/weixin_43272781/article/details/104742117