Skip to content

Commit 5fcb38e

Browse files
committed
SecurityLoginHandler add UserDetails
1 parent 70241e9 commit 5fcb38e

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/MyAccessDeniedHandler.java

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public class MyAccessDeniedHandler implements AccessDeniedHandler {
2020
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
2121
log.debug("access denied");
2222
String content = JSONObject.toJSONString(Response.buildFailure("not.access", "please check user authentication."));
23+
// 设置响应的 Content-Type 为 JSON,并指定字符编码为 UTF-8
24+
response.setContentType("application/json;charset=UTF-8");
25+
response.setCharacterEncoding("UTF-8");
26+
2327
IOUtils.write(content, response.getOutputStream(), StandardCharsets.UTF_8);
2428
}
2529
}

springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/MyAuthenticationFilter.java

+4
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
7575

7676
private void writeResponse(HttpServletResponse servletResponse, Response returnResponse) throws IOException {
7777
String content = JSONObject.toJSONString(returnResponse);
78+
// 设置响应的 Content-Type 为 JSON,并指定字符编码为 UTF-8
79+
servletResponse.setContentType("application/json;charset=UTF-8");
80+
servletResponse.setCharacterEncoding("UTF-8");
81+
7882
IOUtils.write(content, servletResponse.getOutputStream(), StandardCharsets.UTF_8);
7983
}
8084

springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/MyLogoutSuccessHandler.java

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public class MyLogoutSuccessHandler implements LogoutSuccessHandler {
2020
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
2121
log.debug("logout success ~");
2222
String content = JSONObject.toJSONString(Response.buildSuccess());
23+
// 设置响应的 Content-Type 为 JSON,并指定字符编码为 UTF-8
24+
response.setContentType("application/json;charset=UTF-8");
25+
response.setCharacterEncoding("UTF-8");
26+
2327
IOUtils.write(content, response.getOutputStream(), StandardCharsets.UTF_8);
2428
}
2529
}

springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/MyUnAuthenticationEntryPoint.java

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public class MyUnAuthenticationEntryPoint implements AuthenticationEntryPoint {
2121
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
2222
log.debug("no authentication ~");
2323
String content = JSONObject.toJSONString(Response.buildFailure("not.login", "please to login."));
24+
// 设置响应的 Content-Type 为 JSON,并指定字符编码为 UTF-8
25+
response.setContentType("application/json;charset=UTF-8");
26+
response.setCharacterEncoding("UTF-8");
27+
2428
IOUtils.write(content, response.getOutputStream(), StandardCharsets.UTF_8);
2529
}
2630
}

0 commit comments

Comments
 (0)