Spring boot - return user object after log in

前端 未结 2 1362
栀梦
栀梦 2021-01-31 11:35

I have a spring boot application with WebSecurityConfigurerAdapter configured like this -

http.csrf().disable()
                    .exceptionHandling()
                


        
2条回答
  •  不要未来只要你来
    2021-01-31 12:14

    In the accepted answer you need two calls to get the data you want. Simply return the data after the login in a custom AjaxAuthenticationSuccessHandler like this.

    @Bean
    public AjaxAuthenticationSuccessHandler ajaxAuthenticationSuccessHandler() {
        return new AjaxAuthenticationSuccessHandler() {
    
            @Override
            public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
                response.getWriter().write(new ObjectMapper().writeValueAsString(new UserAuthenticationResponse(authentication.getName(), 123l)));
                response.setStatus(200);
            }
    
        };
    }
    

    and register the successhandler:

    http.successHandler(ajaxAuthenticationSuccessHandler())
    

提交回复
热议问题