mirror of
https://github.com/zongzibinbin/MallChat.git
synced 2026-03-14 14:13:42 +08:00
去除无效日志
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
package com.abin.mallchat.common;
|
||||||
|
|
||||||
|
public interface MDCKey {
|
||||||
|
String TID = "tid";
|
||||||
|
String UID = "uid";
|
||||||
|
}
|
||||||
@@ -33,7 +33,6 @@ import java.util.stream.Collectors;
|
|||||||
public class FrequencyControlAspect {
|
public class FrequencyControlAspect {
|
||||||
@Around("@annotation(com.abin.frequencycontrol.annotation.FrequencyControl)||@annotation(com.abin.frequencycontrol.annotation.FrequencyControlContainer)")
|
@Around("@annotation(com.abin.frequencycontrol.annotation.FrequencyControl)||@annotation(com.abin.frequencycontrol.annotation.FrequencyControlContainer)")
|
||||||
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
|
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||||
System.out.println("FrequencyControlAspect start");
|
|
||||||
Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
|
Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
|
||||||
FrequencyControl[] annotationsByType = method.getAnnotationsByType(FrequencyControl.class);
|
FrequencyControl[] annotationsByType = method.getAnnotationsByType(FrequencyControl.class);
|
||||||
Map<String, FrequencyControl> keyMap = new HashMap<>();
|
Map<String, FrequencyControl> keyMap = new HashMap<>();
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package com.abin.frequencycontrol.config;
|
||||||
|
|
||||||
|
import com.abin.frequencycontrol.intecepter.CollectorInterceptor;
|
||||||
|
import com.abin.frequencycontrol.intecepter.TokenInterceptor;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置所有拦截器
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class InterceptorConfig implements WebMvcConfigurer {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TokenInterceptor tokenInterceptor;
|
||||||
|
@Autowired
|
||||||
|
private CollectorInterceptor collectorInterceptor;
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addInterceptors(InterceptorRegistry registry) {
|
||||||
|
registry.addInterceptor(tokenInterceptor)
|
||||||
|
.addPathPatterns("/capi/**");
|
||||||
|
registry.addInterceptor(collectorInterceptor)
|
||||||
|
.addPathPatterns("/capi/**");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package com.abin.frequencycontrol.intecepter;
|
||||||
|
|
||||||
|
import cn.hutool.extra.servlet.ServletUtil;
|
||||||
|
import com.abin.frequencycontrol.domain.dto.RequestInfo;
|
||||||
|
import com.abin.frequencycontrol.util.RequestHolder;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.core.annotation.Order;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 信息收集的拦截器
|
||||||
|
*/
|
||||||
|
@Order(1)
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class CollectorInterceptor implements HandlerInterceptor {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||||
|
RequestInfo info = new RequestInfo();
|
||||||
|
info.setUid(Optional.ofNullable(request.getAttribute(TokenInterceptor.ATTRIBUTE_UID)).map(Object::toString).map(Long::parseLong).orElse(null));
|
||||||
|
info.setIp(ServletUtil.getClientIP(request));
|
||||||
|
RequestHolder.set(info);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
||||||
|
RequestHolder.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
package com.abin.frequencycontrol.intecepter;
|
||||||
|
|
||||||
|
import com.abin.frequencycontrol.exception.HttpErrorEnum;
|
||||||
|
import com.abin.mallchat.common.MDCKey;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.slf4j.MDC;
|
||||||
|
import org.springframework.core.annotation.Order;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Order(-2)
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class TokenInterceptor implements HandlerInterceptor {
|
||||||
|
|
||||||
|
public static final String AUTHORIZATION_HEADER = "Authorization";
|
||||||
|
public static final String AUTHORIZATION_SCHEMA = "Bearer ";
|
||||||
|
public static final String ATTRIBUTE_UID = "uid";
|
||||||
|
//
|
||||||
|
// @Autowired
|
||||||
|
// private LoginService loginService;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||||
|
//获取用户登录token
|
||||||
|
String token = getToken(request);
|
||||||
|
// Long validUid = loginService.getValidUid(token);
|
||||||
|
Long validUid = 1L;
|
||||||
|
if (Objects.nonNull(validUid)) {//有登录态
|
||||||
|
request.setAttribute(ATTRIBUTE_UID, validUid);
|
||||||
|
} else {
|
||||||
|
boolean isPublicURI = isPublicURI(request.getRequestURI());
|
||||||
|
if (!isPublicURI) {//又没有登录态,又不是公开路径,直接401
|
||||||
|
HttpErrorEnum.ACCESS_DENIED.sendHttpError(response);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
MDC.put(MDCKey.UID, String.valueOf(validUid));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
||||||
|
MDC.remove(MDCKey.UID);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断是不是公共方法,可以未登录访问的
|
||||||
|
*
|
||||||
|
* @param requestURI
|
||||||
|
*/
|
||||||
|
private boolean isPublicURI(String requestURI) {
|
||||||
|
String[] split = requestURI.split("/");
|
||||||
|
return split.length > 2 && "public".equals(split[3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getToken(HttpServletRequest request) {
|
||||||
|
String header = request.getHeader(AUTHORIZATION_HEADER);
|
||||||
|
return Optional.ofNullable(header)
|
||||||
|
.filter(h -> h.startsWith(AUTHORIZATION_SCHEMA))
|
||||||
|
.map(h -> h.substring(AUTHORIZATION_SCHEMA.length()))
|
||||||
|
.orElse(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user