|
@@ -0,0 +1,45 @@
|
|
|
|
+package com.persagy.calendar.aop;
|
|
|
|
+
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
+
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
|
|
|
|
+
|
|
|
|
+import com.persagy.common.exception.BusinessException;
|
|
|
|
+
|
|
|
|
+import cn.hutool.http.useragent.Browser;
|
|
|
|
+import cn.hutool.http.useragent.UserAgent;
|
|
|
|
+import cn.hutool.http.useragent.UserAgentUtil;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 非浏览器请求直接拒绝
|
|
|
|
+ *
|
|
|
|
+ * @version 1.0.0
|
|
|
|
+ * @company persagy
|
|
|
|
+ * @author zhangqiankun
|
|
|
|
+ * @date 2022年3月21日 下午6:27:39
|
|
|
|
+ */
|
|
|
|
+@Slf4j
|
|
|
|
+@Component
|
|
|
|
+public class UserAgentInterceptor extends HandlerInterceptorAdapter {
|
|
|
|
+
|
|
|
|
+ @Value("${calendar.user.agent.browser:false}")
|
|
|
|
+ private boolean isUserAgent;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
|
|
|
+ if (isUserAgent) {
|
|
|
|
+ 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("非法请求");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return super.preHandle(request, response, handler);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|