Commit 82fe890a authored by 王明生's avatar 王明生

fixed: 解决http错误码太大问题,并后置休眠5秒逻辑

parent ab760914
......@@ -21,7 +21,7 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2;
public class AfantiOpenApiApplication {
public static void main(String[] args) {
System.setProperty("mybatis - plus.configuration.log - impl", "org.apache.ibatis.logging.stdout.StdOutImpl");
// System.setProperty("mybatis - plus.configuration.log - impl", "org.apache.ibatis.logging.stdout.StdOutImpl");
SpringApplication.run(AfantiOpenApiApplication.class, args);
}
......
......@@ -5,26 +5,21 @@ import cn.hutool.core.util.StrUtil;
import com.afanticar.afantiopenapi.constant.Constant;
import com.afanticar.afantiopenapi.controller.BaseController;
import com.afanticar.afantiopenapi.feign.AfantiCasFeign;
import com.afanticar.afantiopenapi.model.BaseResponse;
import com.afanticar.afantiopenapi.model.vo.TokenCheckVO;
import com.afanticar.afantiopenapi.utils.JWTUtils;
import com.alibaba.fastjson.JSONObject;
import feign.FeignException;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBucket;
import org.redisson.api.RLock;
import org.springframework.lang.Nullable;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* @author chin
......@@ -33,7 +28,7 @@ import java.util.concurrent.TimeUnit;
*/
@Slf4j
@Component
public class TokenInterceptor extends HandlerInterceptorAdapter {
public class CommonInterceptor extends HandlerInterceptorAdapter {
@Autowired
private AfantiCasFeign casFeign;
......@@ -81,6 +76,31 @@ public class TokenInterceptor extends HandlerInterceptorAdapter {
return true;
}
// @Override
// public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
// @Nullable Exception ex) throws Exception {
// if (response.getStatus() == 200) {
// try {
// Thread.sleep(5000);
// } catch (InterruptedException e) {
// throw new RuntimeException(e);
// }
// }
// }
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
@Nullable ModelAndView modelAndView) throws Exception {
// 在当前请求的 Controller 方法调用之后,且在 DispatcherServlet 进行视图返回渲染之前被调用。
// 注意,只有当所属拦截器的 preHandle 方法返回值为 true 时,该拦截器的 postHandle 方法才会被调用.
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
private void writeResponse(HttpServletResponse response, String msg) throws Exception {
response.setCharacterEncoding("UTF-8");
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
......
......@@ -23,8 +23,8 @@ public class WebMvcConfig implements WebMvcConfigurer {
BydOpenController bydOpenController;
@Bean
public TokenInterceptor getLoginInterceptor(){
return new TokenInterceptor();
public CommonInterceptor getLoginInterceptor(){
return new CommonInterceptor();
}
......
......@@ -123,7 +123,7 @@ public class BaseController extends HandlerInterceptorAdapter{
@SneakyThrows
public static void setResponse(HttpServletResponse response, Integer errorCode, String msg) {
response.setCharacterEncoding("UTF-8");
response.setStatus(errorCode);
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
response.setContentType("application/json");
response.getWriter().append(JSONObject.toJSONString(BaseController.error(errorCode.toString(), msg)));
}
......
......@@ -55,11 +55,6 @@ public class BaseService {
* @param limitPeriod 对应trySetRate中的rateInterval,即rate time interval
*/
public <T> BaseResponse<T> pageApiLimit(String dataType, Integer limit, Integer limitPeriod) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
String header = "token";
String fawKey = String.format("%s:%s:limit", dataType, header);
RRateLimiter rateLimiter = redissonClient.getRateLimiter(fawKey);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment