feat: guava缓存/登录拦截器

This commit is contained in:
2024-03-04 17:39:22 +08:00
parent c0ec4e34f8
commit 9f36b9a126
30 changed files with 503 additions and 98 deletions

View File

@@ -3,10 +3,12 @@ package com.landaiqing.subject.application.config;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.landaiqing.subject.application.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import java.util.List;
@@ -34,4 +36,10 @@ public class GlobalConfig extends WebMvcConfigurationSupport {
MappingJackson2HttpMessageConverter converter=new MappingJackson2HttpMessageConverter(objectMapper);
return converter;
}
@Override
protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor())
.addPathPatterns("/**");
}
}

View File

@@ -0,0 +1,44 @@
package com.landaiqing.subject.application.context;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* @Classname LoginContextHolder
* @BelongsProject: qing-yu-club
* @BelongsPackage: com.landaiqing.subject.application.context
* @Author: landaiqing
* @CreateTime: 2024-03-03 18:11
* @Description: 登录上下文对象
* @Version: 1.0
*/
public class LoginContextHolder {
private static final InheritableThreadLocal <Map<String,Object>> THREAD_LOCAL=new InheritableThreadLocal<>();
public static void set(String key,Object val){
Map<String,Object> map=getThreadLoacalMap();
map.put(key, val);
}
public static Object get(String key){
Map<String,Object> threadLoacalMap=getThreadLoacalMap();
return threadLoacalMap.get(key);
}
public static void remove(){
THREAD_LOCAL.remove();
}
public static String getLoginId(){
return (String) getThreadLoacalMap().get("loginId");
}
public static Map<String,Object> getThreadLoacalMap(){
Map<String,Object> map =THREAD_LOCAL.get();
if (Objects.isNull(map)){
map=new ConcurrentHashMap<>();
THREAD_LOCAL.set(map);
}
return map;
}
}

View File

@@ -0,0 +1,32 @@
package com.landaiqing.subject.application.controller;
import com.landaiqing.subject.infra.entity.UserInfo;
import com.landaiqing.subject.infra.rpc.UserRpc;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* 刷题分类controller
*
* @author: landaiqing
* @date: 2024/3/3
*/
@RestController
@RequestMapping("/subject/category")
@Slf4j
public class TestFeignController {
@Resource
private UserRpc userRpc;
@GetMapping("testFeign")
public void testFeign() {
UserInfo userInfo = userRpc.getUserInfo("jichi");
log.info("testFeign.userInfo:{}", userInfo);
}
}

View File

@@ -0,0 +1,22 @@
package com.landaiqing.subject.application.interceptor;
import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Classname FeignConfiguration
* @BelongsProject: qing-yu-club
* @BelongsPackage: com.landaiqing.subject.application.interceptor
* @Author: landaiqing
* @CreateTime: 2024-03-03 21:11
* @Description: TODO
* @Version: 1.0
*/
@Configuration
public class FeignConfiguration {
@Bean
public RequestInterceptor requestInterceptor(){
return new FeignRequestInterceptor();
}
}

View File

@@ -0,0 +1,37 @@
package com.landaiqing.subject.application.interceptor;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;
/**
* @Classname FeignRequestInterceptor
* @BelongsProject: qing-yu-club
* @BelongsPackage: com.landaiqing.subject.application.interceptor
* @Author: landaiqing
* @CreateTime: 2024-03-03 21:04
* @Description: Feign请求拦截器
* @Version: 1.0
*/
@Component
public class FeignRequestInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
if (Objects.nonNull(request)) {
String loginId = request.getHeader("loginId");
if (StringUtils.isNotBlank(loginId)) {
requestTemplate.header("loginId", loginId);
}
}
}
}

View File

@@ -0,0 +1,33 @@
package com.landaiqing.subject.application.interceptor;
import com.landaiqing.subject.application.context.LoginContextHolder;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
/**
* @Classname LoginInterceptor
* @BelongsProject: qing-yu-club
* @BelongsPackage: com.landaiqing.subject.application.interceptor
* @Author: landaiqing
* @CreateTime: 2024-03-03 18:05
* @Description: 登录拦截器
* @Version: 1.0
*/
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String loginId = request.getHeader("loginId");
LoginContextHolder.set("loginId",loginId);
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
LoginContextHolder.remove();
}
}

View File

@@ -0,0 +1,19 @@
package com.landaiqing.subject.application.util;
import com.landaiqing.subject.application.context.LoginContextHolder;
/**
* @Classname LoginUtil
* @BelongsProject: qing-yu-club
* @BelongsPackage: com.landaiqing.subject.application.util
* @Author: landaiqing
* @CreateTime: 2024-03-03 18:33
* @Description: 用户登录util
* @Version: 1.0
*/
public class LoginUtil {
public static String getLoginId() {
return LoginContextHolder.getLoginId();
}
}