说明:threadLocalUtil 工具类为从网上收集而来,来源已经记不清了,抱歉,只记得是微信收集的文章里面的
一,拦截器获取请求头相关信息
package *.component.interceptor;
import *.utils.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import static com.intellif.vesionbook.admin.constant.MemberApiConstant.*;
@Slf4j
public class RequestHeaderInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler) throws Exception {
if (!(handler instanceof HandlerMethod)) {
return true;
}
String openAndAppId = httpServletRequest.getHeader("secretCode");
String deviceId = httpServletRequest.getHeader(AI_SCREEN_ID);
String uri = httpServletRequest.getRequestURI();
// log.info(uri+" openAndAppId:{}",openAndAppId);
if(StringUtils.isNotBlank(openAndAppId)){
String[] split = openAndAppId.split("\\|");
if(null != split && split.length>1){
ThreadLocalUtil.set(WECHAT_OPENID,split[0]);
ThreadLocalUtil.set(WECHAT_APPID,split[1]);
}
}
if(StringUtils.isNotBlank(deviceId)){
ThreadLocalUtil.set(AI_SCREEN_ID,deviceId);
}
if(uri.contains("/information/update/")){
String headerToken = getHeaderToken(httpServletRequest);
if(StringUtils.isNotBlank(headerToken)){
ThreadLocalUtil.set(LOGIN_TOKEN,headerToken);
}
}
return true;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
ThreadLocalUtil.remove(WECHAT_OPENID);
ThreadLocalUtil.remove(WECHAT_APPID);
ThreadLocalUtil.remove(AI_SCREEN_ID);
ThreadLocalUtil.remove(LOGIN_TOKEN);
}
private String getHeaderToken(HttpServletRequest httpServletRequest){
{
String reqToken = httpServletRequest.getHeader("Authorization");
if (org.apache.commons.lang3.StringUtils.isBlank(reqToken)) {
reqToken = httpServletRequest.getHeader("token");
}
if (org.apache.commons.lang3.StringUtils.isNotBlank(reqToken)) {
if (reqToken.contains("Bearer ")) {
reqToken = reqToken.replace("Bearer ", "").trim();
}
}
return reqToken;
}
}
}
二,配置拦截器
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new RequestHeaderInterceptor()).addPathPatterns("/**");
super.addInterceptors(registry);
}
}
三,threadLocal工具类
public class ThreadLocalUtil<T> {
private static final ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal() {
@Override
protected Map<String, Object> initialValue() {
return new HashMap<>(4);
}
};
public static Map<String, Object> getThreadLocal() {
return threadLocal.get();
}
public static <T> T get(String key) {
Map map = (Map) threadLocal.get();
return (T) map.get(key);
}
public static <T> T get(String key, T defaultValue) {
Map map = (Map) threadLocal.get();
return (T) map.get(key) == null ? defaultValue : (T) map.get(key);
}
public static void set(String key, Object value) {
Map map = (Map) threadLocal.get();
map.put(key, value);
}
public static void set(Map<String, Object> keyValueMap) {
Map map = (Map) threadLocal.get();
map.putAll(keyValueMap);
}
public static void remove() {
threadLocal.remove();
}
public static <T> Map<String, T> fetchVarsByPrefix(String prefix) {
Map<String, T> vars = new HashMap<>();
if (prefix == null) {
return vars;
}
Map map = (Map) threadLocal.get();
Set<Map.Entry> set = map.entrySet();
for (Map.Entry entry : set) {
Object key = entry.getKey();
if (key instanceof String) {
if (((String) key).startsWith(prefix)) {
vars.put((String) key, (T) entry.getValue());
}
}
}
return vars;
}
public static <T> T remove(String key) {
Map map = (Map) threadLocal.get();
return (T) map.remove(key);
}
public static void clear(String prefix) {
if (prefix == null) {
return;
}
Map map = (Map) threadLocal.get();
Set<Map.Entry> set = map.entrySet();
List<String> removeKeys = new ArrayList<>();
for (Map.Entry entry : set) {
Object key = entry.getKey();
if (key instanceof String) {
if (((String) key).startsWith(prefix)) {
removeKeys.add((String) key);
}
}
}
for (String key : removeKeys) {
map.remove(key);
}
}
}
使用时候,可以直接在代码中直接用工具类 get 想要的值了!