1818import org .springframework .security .core .Authentication ;
1919import org .springframework .security .core .AuthenticationException ;
2020import org .springframework .security .core .GrantedAuthority ;
21- import org .springframework .security .core .userdetails .User ;
21+ import org .springframework .security .core .userdetails .UserDetails ;
2222import org .springframework .security .web .authentication .UsernamePasswordAuthenticationFilter ;
2323import org .springframework .security .web .util .matcher .AntPathRequestMatcher ;
2424
@@ -69,16 +69,21 @@ public Authentication attemptAuthentication(HttpServletRequest request, HttpServ
6969 @ Override
7070 protected void successfulAuthentication (HttpServletRequest request , HttpServletResponse response , FilterChain chain , Authentication authResult ) throws IOException , ServletException {
7171 log .debug ("login success authentication ~" );
72- User user = (User ) authResult .getPrincipal ();
72+ UserDetails user = (UserDetails ) authResult .getPrincipal ();
7373 LoginRequest loginRequest = LoginRequestContext .getInstance ().get ();
7474
7575 Token token = tokenGateway .create (user .getUsername (), loginRequest .getPassword (),
7676 user .getAuthorities ().stream ().map (GrantedAuthority ::getAuthority ).collect (Collectors .toList ()),
7777 TokenContext .getExtra ());
7878
79- LoginResponse loginResponse = loginHandler .postHandle (request , response , loginRequest , token );
79+ LoginResponse loginResponse = loginHandler .postHandle (request , response , loginRequest , user , token );
8080
8181 String content = JSONObject .toJSONString (SingleResponse .of (loginResponse ));
82+
83+ // 设置响应的 Content-Type 为 JSON,并指定字符编码为 UTF-8
84+ response .setContentType ("application/json;charset=UTF-8" );
85+ response .setCharacterEncoding ("UTF-8" );
86+
8287 IOUtils .write (content , response .getOutputStream (), StandardCharsets .UTF_8 );
8388
8489 LoginRequestContext .getInstance ().clean ();
@@ -89,7 +94,13 @@ protected void successfulAuthentication(HttpServletRequest request, HttpServletR
8994 protected void unsuccessfulAuthentication (HttpServletRequest request , HttpServletResponse response , AuthenticationException failed ) throws IOException , ServletException {
9095 log .debug ("login fail authentication ~" );
9196 String content = JSONObject .toJSONString (Response .buildFailure ("login.error" , failed .getMessage ()));
97+
98+ // 设置响应的 Content-Type 为 JSON,并指定字符编码为 UTF-8
99+ response .setContentType ("application/json;charset=UTF-8" );
100+ response .setCharacterEncoding ("UTF-8" );
101+
92102 IOUtils .write (content , response .getOutputStream (), StandardCharsets .UTF_8 );
103+
93104 LoginRequestContext .getInstance ().clean ();
94105 }
95106}
0 commit comments