|
|
|
@ -8,9 +8,6 @@ import cn.soul2.jyjc.admin.utils.EncryptUtils; |
|
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
|
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; |
|
|
|
@ -29,16 +26,13 @@ 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; |
|
|
|
|
System.out.println("拦截器触发"); |
|
|
|
|
// 允许OPTIONS请求通过
|
|
|
|
|
if ("OPTIONS".equals(request.getMethod()) && request.getHeader("Origin") != null) { |
|
|
|
|
return true; |
|
|
|
@ -104,16 +98,13 @@ public class FinallyInterceptor implements HandlerInterceptor { |
|
|
|
|
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
|
|
|
|
|
// Token 无效,拒绝请求
|
|
|
|
|
response.setStatus(40401); |
|
|
|
|
pass = false; |
|
|
|
|
} |
|
|
|
|