|
@@ -3,16 +3,20 @@ package com.persagy.calendar.aop;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
+import org.apache.http.entity.ContentType;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.persagy.common.enums.ResponseCode;
|
|
|
import com.persagy.common.exception.BusinessException;
|
|
|
+import com.persagy.common.utils.StringUtil;
|
|
|
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
import cn.hutool.http.useragent.Browser;
|
|
|
import cn.hutool.http.useragent.UserAgent;
|
|
|
import cn.hutool.http.useragent.UserAgentUtil;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
/**
|
|
|
* 非浏览器请求直接拒绝
|
|
@@ -22,10 +26,12 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
* @author zhangqiankun
|
|
|
* @date 2022年3月21日 下午6:27:39
|
|
|
*/
|
|
|
-@Slf4j
|
|
|
@Component
|
|
|
public class UserAgentInterceptor extends HandlerInterceptorAdapter {
|
|
|
|
|
|
+ @Value("${calendar.person.center.url:http://127.0.0.1:8814}")
|
|
|
+ private String personUrl;
|
|
|
+
|
|
|
@Value("${calendar.user.agent.browser:false}")
|
|
|
private boolean isUserAgent;
|
|
|
|
|
@@ -35,9 +41,31 @@ public class UserAgentInterceptor extends HandlerInterceptorAdapter {
|
|
|
UserAgent userAgent = UserAgentUtil.parse(request.getHeader("User-Agent"));
|
|
|
Browser browser = userAgent == null ? null : userAgent.getBrowser();
|
|
|
if (browser == null || "Unknown".equalsIgnoreCase(browser.getName())) {
|
|
|
- log.error("不允许非浏览器请求");
|
|
|
throw new BusinessException("非法请求");
|
|
|
}
|
|
|
+ String appToken = request.getHeader("app-token");
|
|
|
+ if (StringUtil.isBlank(appToken)) {
|
|
|
+ throw new BusinessException("非法请求");
|
|
|
+ }
|
|
|
+ String[] split = appToken.split("-");
|
|
|
+ if (split.length < 2) {
|
|
|
+ throw new BusinessException("非法请求");
|
|
|
+ }
|
|
|
+ JSONObject params = new JSONObject();
|
|
|
+ params.put("userId", split[0]);
|
|
|
+ params.put("token", split[1]);
|
|
|
+
|
|
|
+ String url = personUrl + "/user/checkToken";
|
|
|
+ String responseStr = HttpUtil.createPost(url).body(params.toJSONString(),
|
|
|
+ ContentType.APPLICATION_JSON.toString()).timeout(180*1000).execute().body();
|
|
|
+ if (StringUtil.isBlank(responseStr)) {
|
|
|
+ throw new BusinessException("token校验异常");
|
|
|
+ }
|
|
|
+ JSONObject parseObject = JSONObject.parseObject(responseStr);
|
|
|
+ String code = parseObject.getString("respCode");
|
|
|
+ if (!ResponseCode.A00000.getCode().equals(code)) {
|
|
|
+ throw new BusinessException("token校验异常");
|
|
|
+ }
|
|
|
}
|
|
|
return super.preHandle(request, response, handler);
|
|
|
}
|