Skip to content

Commit 5653760

Browse files
committed
SecurityLoginHandler add UserDetails
1 parent ebaa3a0 commit 5653760

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-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

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.alibaba.fastjson.JSONObject;
44
import com.codingapi.springboot.framework.dto.response.Response;
5+
import com.codingapi.springboot.security.dto.request.LoginRequestContext;
56
import com.codingapi.springboot.security.exception.TokenExpiredException;
67
import com.codingapi.springboot.security.gateway.Token;
78
import com.codingapi.springboot.security.gateway.TokenGateway;
@@ -80,6 +81,10 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
8081

8182
private void writeResponse(HttpServletResponse servletResponse, Response returnResponse) throws IOException {
8283
String content = JSONObject.toJSONString(returnResponse);
84+
// 设置响应的 Content-Type 为 JSON,并指定字符编码为 UTF-8
85+
servletResponse.setContentType("application/json;charset=UTF-8");
86+
servletResponse.setCharacterEncoding("UTF-8");
87+
8388
IOUtils.write(content, servletResponse.getOutputStream(), StandardCharsets.UTF_8);
8489
}
8590

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
@@ -20,6 +20,10 @@ public class MyUnAuthenticationEntryPoint implements AuthenticationEntryPoint {
2020
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
2121
log.debug("no authentication ~");
2222
String content = JSONObject.toJSONString(Response.buildFailure("not.login", "please to login."));
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
}

0 commit comments

Comments
 (0)