You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.5 KiB
37 lines
1.5 KiB
package com.intelligentHealthCare.exception;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.intelligentHealthCare.enums.ResultCodeEnum;
|
|
import com.intelligentHealthCare.pojo.Result;
|
|
import org.springframework.security.core.AuthenticationException;
|
|
import org.springframework.security.web.AuthenticationEntryPoint;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import javax.servlet.ServletException;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import java.io.IOException;
|
|
import java.io.PrintWriter;
|
|
|
|
/**
|
|
* @author yang
|
|
* @version 1.0
|
|
* @date Created in 2023/9/28 21:16
|
|
*/
|
|
@Component
|
|
public class AuthenticationEntryPointException implements AuthenticationEntryPoint {
|
|
@Override
|
|
public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
|
|
Result<Object> result = Result.builder().data("账号过期,请重新登录").status(ResultCodeEnum.Unauth).build();
|
|
// 设置响应的内容类型为JSON
|
|
httpServletResponse.setContentType("application/json");
|
|
httpServletResponse.setCharacterEncoding("UTF-8");
|
|
// 将result对象转换为JSON字符串
|
|
String jsonResult = new ObjectMapper().writeValueAsString(result);
|
|
// 将JSON字符串写入响应输出流
|
|
PrintWriter writer = httpServletResponse.getWriter();
|
|
writer.write(jsonResult);
|
|
writer.flush();
|
|
}
|
|
}
|