package com.unionx.api.LoginInterceptor;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object obj, Exception err) throws Exception {
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response,
Object obj, ModelAndView mav) throws Exception {
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object obj) throws Exception {
Boolean flag = false;
JSONObject outputMSg = new JSONObject();
String token = request.getHeader(Constant.Token);
if(StringUtils.isNotBlank(token)){
flag = true;
}else{
outputMSg.put("success", false);
outputMSg.put("statusCode",101);
output(outputMSg, request, response);
}
return flag;
}
public void output(Object json, HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("application/json;charset=UTF-8;");
PrintWriter out = response.getWriter();
out.println(json);
out.flush();
out.close();
}
}
Java 接口拦截器的实现以及返回
最新推荐文章于 2024-05-23 23:58:18 发布