|
|
@@ -8,6 +8,7 @@ import com.dji.sample.common.error.CommonErrorEnum;
|
|
|
import com.dji.sample.common.model.CustomClaim;
|
|
|
import com.dji.sample.common.util.JwtUtil;
|
|
|
import com.dji.sample.component.mqtt.config.MqttPropertyConfiguration;
|
|
|
+import com.dji.sample.component.redis.RedisConst;
|
|
|
import com.dji.sample.component.redis.RedisOpsUtils;
|
|
|
import com.dji.sample.configuration.CustomConfiguration;
|
|
|
import com.dji.sample.manage.log.util.ReqUtils;
|
|
|
@@ -54,46 +55,54 @@ public class AuthInterceptor implements HandlerInterceptor {
|
|
|
|
|
|
@Override
|
|
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
|
|
- String uri = request.getRequestURI();
|
|
|
-
|
|
|
// The options method is passed directly.
|
|
|
if (HttpMethod.OPTIONS.matches(request.getMethod())) {
|
|
|
response.setStatus(HttpStatus.OK.value());
|
|
|
return false;
|
|
|
}
|
|
|
- String ip = ReqUtils.getClientIp();
|
|
|
|
|
|
- log.info("====================客户端请求信息:request uri: {}, IP: {}", uri, ip);
|
|
|
+ boolean result = checkToken(request,response);
|
|
|
+ if(!result) {
|
|
|
+ return checkIP(request);
|
|
|
+ }
|
|
|
+ return true;
|
|
|
|
|
|
- Boolean check = RedisOpsUtils.checkExist(ip);
|
|
|
- if(!check) {
|
|
|
- Boolean flg = whiteListService.selIp(ip);
|
|
|
- if(flg) {
|
|
|
- RedisOpsUtils.set(ip,ip);
|
|
|
- return true;
|
|
|
- } else {
|
|
|
- String token = request.getHeader(PARAM_TOKEN);
|
|
|
- if (!StringUtils.hasText(token)) {
|
|
|
- response.setStatus(HttpStatus.UNAUTHORIZED.value());
|
|
|
- log.error(CommonErrorEnum.NO_TOKEN.getMessage());
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- // Check if the current token is valid.
|
|
|
- Optional<CustomClaim> customClaimOpt = JwtUtil.parseToken(token);
|
|
|
- if (customClaimOpt.isEmpty()) {
|
|
|
- response.setStatus(HttpStatus.UNAUTHORIZED.value());
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- // Put the custom data from the token into the request.
|
|
|
- request.setAttribute(TOKEN_CLAIM, customClaimOpt.get());
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean checkIP(HttpServletRequest request) {
|
|
|
+ String uri = request.getRequestURI();
|
|
|
+ String ip = ReqUtils.getClientIp();
|
|
|
+ log.info("====================客户端请求信息:request uri: {}, IP: {}", uri, ip);
|
|
|
+ boolean check = RedisOpsUtils.checkExist(ip);
|
|
|
+ if (!check) {
|
|
|
+ boolean flg = whiteListService.selIp(ip);
|
|
|
+ if (flg) {
|
|
|
+ RedisOpsUtils.set(ip, ip);
|
|
|
+ RedisOpsUtils.setWithExpire(ip, ip, RedisConst.WHITELIST_ALIVE_SECOND);
|
|
|
return true;
|
|
|
}
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean checkToken(HttpServletRequest request, HttpServletResponse response) {
|
|
|
+ String token = request.getHeader(PARAM_TOKEN);
|
|
|
+ if (!StringUtils.hasText(token)) {
|
|
|
+ response.setStatus(HttpStatus.UNAUTHORIZED.value());
|
|
|
+ log.error(CommonErrorEnum.NO_TOKEN.getMessage());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
+ // Check if the current token is valid.
|
|
|
+ Optional<CustomClaim> customClaimOpt = JwtUtil.parseToken(token);
|
|
|
+ if (customClaimOpt.isEmpty()) {
|
|
|
+ response.setStatus(HttpStatus.UNAUTHORIZED.value());
|
|
|
+ return false;
|
|
|
}
|
|
|
- return true;
|
|
|
|
|
|
+ // Put the custom data from the token into the request.
|
|
|
+ request.setAttribute(TOKEN_CLAIM, customClaimOpt.get());
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
@Override
|