48 lines
1.7 KiB
Java
48 lines
1.7 KiB
Java
package com.zhgd.xmgl.security;
|
||
|
||
import com.alibaba.fastjson.JSONObject;
|
||
import lombok.extern.slf4j.Slf4j;
|
||
import org.springframework.security.core.AuthenticationException;
|
||
import org.springframework.security.web.AuthenticationEntryPoint;
|
||
import org.springframework.stereotype.Component;
|
||
|
||
import javax.servlet.http.HttpServletRequest;
|
||
import javax.servlet.http.HttpServletResponse;
|
||
import java.io.IOException;
|
||
import java.io.PrintWriter;
|
||
import java.io.Serializable;
|
||
|
||
/**
|
||
* @program: wisdomSite
|
||
* @description: 认证失败处理类,返回401
|
||
* @author: Mr.Peng
|
||
* @create: 2021-03-05 16:21
|
||
**/
|
||
@Slf4j
|
||
@Component
|
||
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint, Serializable {
|
||
private static final long serialVersionUID = -8970718410437077606L;
|
||
|
||
@Override
|
||
public void commence(HttpServletRequest request,
|
||
HttpServletResponse response,
|
||
AuthenticationException authException) throws IOException {
|
||
//验证为未登陆状态会进入此方法,认证错误
|
||
log.info("认证失败:" + authException.getMessage() + "--请求路径:" + request.getRequestURL());
|
||
response.setStatus(401);
|
||
response.setCharacterEncoding("UTF-8");
|
||
response.setContentType("application/json;charset=UTF-8");
|
||
PrintWriter printWriter = response.getWriter();
|
||
JSONObject resParam = new JSONObject();
|
||
resParam.put("message", authException.getMessage());
|
||
resParam.put("success", false);
|
||
resParam.put("code", "401");
|
||
//printWriter.write(resParam.toJSONString());
|
||
printWriter.println(resParam.toJSONString());
|
||
printWriter.flush();
|
||
printWriter.close();
|
||
|
||
|
||
}
|
||
}
|