18
18
import org .springframework .security .core .Authentication ;
19
19
import org .springframework .security .core .AuthenticationException ;
20
20
import org .springframework .security .core .GrantedAuthority ;
21
- import org .springframework .security .core .userdetails .User ;
21
+ import org .springframework .security .core .userdetails .UserDetails ;
22
22
import org .springframework .security .web .authentication .UsernamePasswordAuthenticationFilter ;
23
23
import org .springframework .security .web .util .matcher .AntPathRequestMatcher ;
24
24
@@ -69,16 +69,21 @@ public Authentication attemptAuthentication(HttpServletRequest request, HttpServ
69
69
@ Override
70
70
protected void successfulAuthentication (HttpServletRequest request , HttpServletResponse response , FilterChain chain , Authentication authResult ) throws IOException , ServletException {
71
71
log .debug ("login success authentication ~" );
72
- User user = (User ) authResult .getPrincipal ();
72
+ UserDetails user = (UserDetails ) authResult .getPrincipal ();
73
73
LoginRequest loginRequest = LoginRequestContext .getInstance ().get ();
74
74
75
75
Token token = tokenGateway .create (user .getUsername (), loginRequest .getPassword (),
76
76
user .getAuthorities ().stream ().map (GrantedAuthority ::getAuthority ).collect (Collectors .toList ()),
77
77
TokenContext .getExtra ());
78
78
79
- LoginResponse loginResponse = loginHandler .postHandle (request , response , loginRequest , token );
79
+ LoginResponse loginResponse = loginHandler .postHandle (request , response , loginRequest , user , token );
80
80
81
81
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
+
82
87
IOUtils .write (content , response .getOutputStream (), StandardCharsets .UTF_8 );
83
88
84
89
LoginRequestContext .getInstance ().clean ();
@@ -89,7 +94,13 @@ protected void successfulAuthentication(HttpServletRequest request, HttpServletR
89
94
protected void unsuccessfulAuthentication (HttpServletRequest request , HttpServletResponse response , AuthenticationException failed ) throws IOException , ServletException {
90
95
log .debug ("login fail authentication ~" );
91
96
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
+
92
102
IOUtils .write (content , response .getOutputStream (), StandardCharsets .UTF_8 );
103
+
93
104
LoginRequestContext .getInstance ().clean ();
94
105
}
95
106
}
0 commit comments