怎样建设一个自己的网站首页,微信wordpress插件,福彩hao123网址导航,博客系统做网站ThreadLocal为每一线程提供一份单独的存储空间#xff0c;具有线程隔离的作用 PageHelper.startPage()方法使用ThreadLocal来保存分页参数#xff0c;保证线程安全性。PageHelper通过集成MyBatis的拦截器机制来实现对SQL语句的拦截和修改 项目中使用了ThreadLocal保存每个线程…ThreadLocal为每一线程提供一份单独的存储空间具有线程隔离的作用 PageHelper.startPage()方法使用ThreadLocal来保存分页参数保证线程安全性。PageHelper通过集成MyBatis的拦截器机制来实现对SQL语句的拦截和修改 项目中使用了ThreadLocal保存每个线程的用户id
//封装操作ThreadLocal的工具
public class BaseContext {public static ThreadLocalLong threadLocal new ThreadLocal();public static void setCurrentId(Long id) {threadLocal.set(id);}public static Long getCurrentId() {return threadLocal.get();}public static void removeCurrentId() {threadLocal.remove();}
} Component
Slf4j
public class JwtTokenAdminInterceptor implements HandlerInterceptor {Autowiredprivate JwtProperties jwtProperties;/*** 校验jwt*/public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {//判断当前拦截到的是Controller的方法还是其他资源if (!(handler instanceof HandlerMethod)) {//当前拦截到的不是动态方法直接放行return true;}//1、从请求头中获取令牌String token request.getHeader(jwtProperties.getAdminTokenName());//2、校验令牌try {Claims claims JwtUtil.parseJWT(jwtProperties.getAdminSecretKey(), token);Long id Long.valueOf(claims.get(id).toString());/**如果每次需要使用当前登录用户id的地方都需要重新解析token*会造成一定的性能开销。而将当前登录用户信息存储在ThreadLocal中*可以提高访问效率避免了重复解析的过程。*/BaseContext.setCurrentId(id);//3、通过放行return true;} catch (Exception ex) {//4、不通过响应401状态码response.setStatus(401);return false;}}
}