|
|
|
@ -2,22 +2,21 @@ package cn.soul2.jyjc.admin.interceptor; |
|
|
|
|
|
|
|
|
|
import cn.soul2.jyjc.admin.annotation.SkinEncrypt; |
|
|
|
|
import cn.soul2.jyjc.admin.annotation.SkinLogin; |
|
|
|
|
import cn.soul2.jyjc.admin.config.UserLoginStatusBean; |
|
|
|
|
import cn.soul2.jyjc.admin.bean.UserLoginStatusBean; |
|
|
|
|
import cn.soul2.jyjc.admin.filter.ShaoduoRequestWrapper; |
|
|
|
|
import cn.soul2.jyjc.admin.utils.EncryptUtils; |
|
|
|
|
import cn.soul2.jyjc.admin.vo.base.Back; |
|
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
import org.springframework.context.ApplicationContext; |
|
|
|
|
import org.springframework.context.annotation.Lazy; |
|
|
|
|
import org.springframework.http.MediaType; |
|
|
|
|
import org.springframework.web.method.HandlerMethod; |
|
|
|
|
import org.springframework.web.servlet.HandlerInterceptor; |
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource; |
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
|
import java.io.PrintWriter; |
|
|
|
|
import java.lang.reflect.Method; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -29,18 +28,14 @@ import java.lang.reflect.Method; |
|
|
|
|
@Slf4j |
|
|
|
|
public class FinallyInterceptor implements HandlerInterceptor { |
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
@Lazy |
|
|
|
|
private ApplicationContext context; |
|
|
|
|
@Resource |
|
|
|
|
@Lazy |
|
|
|
|
private UserLoginStatusBean userLoginStatusBean; |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { |
|
|
|
|
boolean pass = false; |
|
|
|
|
// 允许OPTIONS请求通过
|
|
|
|
|
if ("OPTIONS".equals(request.getMethod()) && request.getHeader("Origin") != null) { |
|
|
|
|
if ("OPTIONS".equalsIgnoreCase(request.getMethod())) { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
// 如果不是映射到方法直接通过
|
|
|
|
@ -68,60 +63,63 @@ public class FinallyInterceptor implements HandlerInterceptor { |
|
|
|
|
if (request.getContentType() == null) { |
|
|
|
|
pass = true; |
|
|
|
|
} |
|
|
|
|
// 如果类型不是json 就放行
|
|
|
|
|
if (!(request.getContentType().equals(MediaType.APPLICATION_JSON_VALUE))) { |
|
|
|
|
pass = true; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 跳过使用SkinEncrypt注解的情况
|
|
|
|
|
// 跳过使用 @SkinEncrypt 的情况
|
|
|
|
|
if (skinEncrypt != null) { |
|
|
|
|
pass = true; |
|
|
|
|
} |
|
|
|
|
ShaoduoRequestWrapper shaoduoRequestWrapper; |
|
|
|
|
if (request instanceof ShaoduoRequestWrapper) { |
|
|
|
|
shaoduoRequestWrapper = (ShaoduoRequestWrapper) request; |
|
|
|
|
} else { |
|
|
|
|
shaoduoRequestWrapper = new ShaoduoRequestWrapper(request); |
|
|
|
|
request = shaoduoRequestWrapper; |
|
|
|
|
} |
|
|
|
|
String jsonParamBody = shaoduoRequestWrapper.getBodyString(); |
|
|
|
|
JSONObject obj = JSON.parseObject(EncryptUtils.decrypt(jsonParamBody)); |
|
|
|
|
String afterBody = JSONObject.toJSONString(obj); |
|
|
|
|
|
|
|
|
|
System.out.println("加密前 " + jsonParamBody); |
|
|
|
|
System.out.println("解密后 " + afterBody); |
|
|
|
|
shaoduoRequestWrapper.setBody(afterBody); |
|
|
|
|
String temp = new ShaoduoRequestWrapper(shaoduoRequestWrapper).getBodyString(); |
|
|
|
|
System.out.println("过滤器中缓存 " + temp); |
|
|
|
|
if (!pass) { |
|
|
|
|
ShaoduoRequestWrapper shaoduoRequestWrapper; |
|
|
|
|
try { |
|
|
|
|
shaoduoRequestWrapper = (ShaoduoRequestWrapper) request; |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
System.out.printf("request.ClassTypeError: %s%n", request.getClass().getName()); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
String sourceParamBody = shaoduoRequestWrapper.getBodyString(); |
|
|
|
|
JSONObject obj = JSON.parseObject(EncryptUtils.decrypt(sourceParamBody)); |
|
|
|
|
String afterBody = JSONObject.toJSONString(obj); |
|
|
|
|
shaoduoRequestWrapper.setBody(afterBody); |
|
|
|
|
log.info(String.format("解密: %s -> %s", sourceParamBody, afterBody)); |
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
token验证 |
|
|
|
|
*/ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* token验证 */ |
|
|
|
|
if (hasSkinLogin) { |
|
|
|
|
// 如果存在,绕过拦截器
|
|
|
|
|
// 跳过使用 @SkinLogin 的情况
|
|
|
|
|
pass = true; |
|
|
|
|
} else { |
|
|
|
|
// 验证token
|
|
|
|
|
if (userLoginStatusBean == null) { |
|
|
|
|
userLoginStatusBean = context.getBean(UserLoginStatusBean.class); |
|
|
|
|
} |
|
|
|
|
// 检查 token 是否存在并且有效
|
|
|
|
|
if (token == null) { |
|
|
|
|
// 没有Token,拒绝请求
|
|
|
|
|
response.setStatus(40401); |
|
|
|
|
pass = false; |
|
|
|
|
} else if (userLoginStatusBean != null && !userLoginStatusBean.containsToken(token)) { |
|
|
|
|
// Token 无效,拒绝请求,可以返回特定的响应状态码,例如 401 Unauthorized
|
|
|
|
|
response.setStatus(40401); |
|
|
|
|
pass = false; |
|
|
|
|
} else if (userLoginStatusBean != null) { |
|
|
|
|
if (!userLoginStatusBean.containsToken(token)) { |
|
|
|
|
// Token 无效,拒绝请求
|
|
|
|
|
response.setStatus(0); |
|
|
|
|
Back<String> back = new Back<String>().setCode(40401).setMessage("Token invalid!"); |
|
|
|
|
|
|
|
|
|
// 转换为 JSON 字符串
|
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper(); |
|
|
|
|
String responseBody = objectMapper.writeValueAsString(back); |
|
|
|
|
|
|
|
|
|
// 输出错误信息到响应中
|
|
|
|
|
PrintWriter writer = response.getWriter(); |
|
|
|
|
writer.print(responseBody); |
|
|
|
|
writer.flush(); |
|
|
|
|
pass = false; |
|
|
|
|
} else { |
|
|
|
|
// token存在, 通过拦截器
|
|
|
|
|
pass = true; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return pass; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|