給所有請(qǐng)求添加用戶身份
微服務(wù)獲取用戶身份
網(wǎng)關(guān)已經(jīng)給所有請(qǐng)求添加了用戶身份,也就是authorization頭信息。
?
創(chuàng)建ThreadLocal工具類 :
package com.hmall.order.utils;
public class UserHolder {
private static final ThreadLocal<Long> tl = new ThreadLocal<>();
public static void setUser(Long userId) {
tl.set(userId);
}
public static Long getUser() {
return tl.get();
}
public static void removeUser() {
tl.remove();
}
}
創(chuàng)建攔截器:?
package com.hmall.order.interceptor;
import com.hmall.order.utils.UserHolder;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@slf4j
public class UserInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//1.獲取請(qǐng)求頭
String authorization = request.getHeader("authorization");
if (StringUtils.isBlank(authorization)) {
log.warn("非法用戶訪問!請(qǐng)求路徑:{}",request.getRequestURI());
//沒有用戶信息,未登錄
throw new RuntimeException("用戶未登錄");
//或者 return false; response.setStatus(403);
}
//2.轉(zhuǎn)換用戶id
Long userId = Long.valueOf(authorization);
//3.存入ThreadLocal
UserHolder.setUser(userId);
//4.放行
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//用完清理掉
UserHolder.removeUser();
}
}
將攔截器注冊(cè)到SpringMvc,讓它生效:
package com.hmall.order.config;
import com.hmall.order.interceptor.UserInterceptor;
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 MvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
//默認(rèn)的攔截路徑就是/** ,excludePathPatterns()是不用攔截的路徑
// registry.addInterceptor(new UserInterceptor()).addPathPatterns("/**").excludePathPatterns();
registry.addInterceptor(new UserInterceptor());
}
}
?將以上代碼(攔截器,config,utils) 放到哪個(gè)微服務(wù)中,哪個(gè)微服務(wù)/**路徑就會(huì)有攔截功能
沒有用戶信息的請(qǐng)求將會(huì)被攔截
給所有有feign的請(qǐng)求,將用戶信息添加請(qǐng)求頭:
package com.hmall.order.config;
import feign.RequestInterceptor;
import feign.RequestTemplate;
public class MyFeignInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
requestTemplate.header("authorization","2");
}
}
?配置好后通過路徑訪問的將被攔截,但是通過網(wǎng)關(guān)的就可以訪問:
http://localhost:8083/order/123865420?被攔截?
http://localhost:10010/order/123865420?可訪問
如果想不從網(wǎng)關(guān),實(shí)現(xiàn)服務(wù)之間調(diào)用服務(wù), 將controller層的?被調(diào)用的方法開放一個(gè)接口到feign.
?
然后再調(diào)用者的啟動(dòng)類添加?:上面的包路徑要填寫一致
注:(記得導(dǎo)feign的包到服務(wù)的pom.xml中)?
這樣因?yàn)閒eign請(qǐng)求頭有用戶信息通過feign之間調(diào)用服務(wù)就不會(huì)收攔截?
?文章來源地址http://www.zghlxwxcb.cn/news/detail-491199.html
?文章來源:http://www.zghlxwxcb.cn/news/detail-491199.html
?
?
?
?
?
?
?
到了這里,關(guān)于登錄用戶信息獲取 網(wǎng)關(guān)+攔截器+feign請(qǐng)求添加請(qǐng)求頭的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!