SpringBoot+restFul+filter+threadLocal實現多租戶的專案
阿新 • • 發佈:2018-12-23
本片部落格參考了
https://blog.csdn.net/gebitan505/article/details/51614805
threadLocal的目的是:
實現變數的全域性話,在當前的專案當中是實現了使用者名稱的多租戶狀態下的全域性話
fiter:進行token的攔截,
ThreadLocal的程式碼
package com.zwl.util; import java.util.HashMap; import java.util.Map; public class ThreadVariable { public static final String USER_ID = "userId"; private static ThreadLocal<Object> threadLocal = new ThreadLocal<Object>(); /** * 獲得執行緒中儲存的屬性. * * @param attribute 屬性名稱 * @return 屬性值 */ public static Object get(String attribute) { Map map = (Map) threadLocal.get(); if (map == null) { return null; } return map.get(attribute); } /** * report * 獲得執行緒中儲存的登入使用者資訊. */ public static String getUserID() { Map map = (Map) threadLocal.get(); if (map == null) { return null; } Object obj = map.get(USER_ID); if (obj != null) { return (String) obj; } return null; } public static void setUserID(String userID) { Map map = (Map) threadLocal.get(); if (map == null) { map = new HashMap(); threadLocal.set(map); } map.put(USER_ID, userID); } /** * 清除執行緒中儲存的資料 */ public static void clearThreadVariable() { threadLocal.remove(); } }
filter中的程式碼:
package com.zwl.filter; import com.alibaba.fastjson.JSON; import com.zwl.model.baseresult.Result; import com.zwl.model.baseresult.ResultCodeEnum; import com.zwl.model.po.TokenModel; import com.zwl.serviceimpl.RedisTokenManagerImpl; import com.zwl.util.ThreadVariable; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.annotation.Order; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import java.io.IOException; /** * @author wb-zjp283121 * @Title: TokenFilter * @ProjectName parent * @Description: token過濾器 * @date 2018/7/615:26 */ @Order(1) // 重點 @WebFilter(filterName = "tokenFilter", urlPatterns = "/wx/*") @Slf4j public class TokenFilter implements Filter { @Autowired private RedisTokenManagerImpl manager; @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest h_request = (HttpServletRequest) request; String token = request.getParameter("token"); String requestURL = h_request.getRequestURL().toString(); log.info("<<token>>請求url:" + requestURL + " token:" + token); ThreadVariable.clearThreadVariable(); // 註冊、登入、註冊簡訊、首頁、回撥 不需要token if (requestURL.contains("/pay_notify.do") || requestURL.contains("/information/getInformationList") || requestURL.contains("/wx/gift/getGiftQrCode")) { chain.doFilter(request, response); return; } if (requestURL.contains("/information/getInformationInfo")) { chain.doFilter(request, response); return; } if (requestURL.contains("/user/authorization")) { chain.doFilter(request, response); return; } if (requestURL.contains("/task/task")) { chain.doFilter(request, response); return; } if (requestURL.contains("/qr/getQRCode") || requestURL.contains("/qr/getH5QrCode")) { chain.doFilter(request, response); return; } if (requestURL.contains(" /wx/gzh/sendFormId") || requestURL.contains(" /wx/gzh/getFormId")) { chain.doFilter(request, response); return; } //測試圖片上傳 if (requestURL.contains("/wx/file/upload")) { chain.doFilter(request, response); return; } //H5支付 if (requestURL.contains("/wx/product/H5Buy") || requestURL.contains("/wx/pay/auth/pay.do")) { chain.doFilter(request, response); return; } // 獲取微信js api 許可權 跟 h5登入許可權 if (requestURL.contains("/wx/gzh/getGzhJsApiToken") || requestURL.contains("/wx/user/h5WeChatLogin")) { chain.doFilter(request, response); return; } //傳送驗證碼 if (requestURL.contains("/wx/user/sendMsgCode") || requestURL.contains("/wx/user/checkCode")) { chain.doFilter(request, response); return; } //獲取視訊列表 if (requestURL.contains("/wx/video/getVideoList")) { chain.doFilter(request, response); return; } //根據id獲取視訊 if (requestURL.contains("/wx/video/getVideoInfoById")) { chain.doFilter(request, response); return; } //獲取banner列表 if (requestURL.contains("/wx/banner/getBannerList")) { chain.doFilter(request, response); return; } //獲取圖示列表 if (requestURL.contains("/wx/icon/getIconList")) { chain.doFilter(request, response); return; } //套課程 if (requestURL.contains("/classset/getPageAllClass") || requestURL.contains("/classset/setpAddBrowseCount") || requestURL.contains("/classset/getById")) { chain.doFilter(request, response); return; } //節課程 if (requestURL.contains("/classinfo/getPageByClassSetId") || requestURL.contains("/classinfo/getById") || requestURL.contains("/classinfo/setpAddBrowseCount")) { chain.doFilter(request, response); return; } //分享繫結上下級關係 if (requestURL.contains("/user/shareRelation")) { chain.doFilter(request, response); return; } //獲取商品列表 ||根據id獲取商品詳情 if (requestURL.contains("/product/getProductList") || requestURL.contains("/product/getProductById")) { chain.doFilter(request, response); return; } //微信頁面輪播圖 if (requestURL.contains("/wx/banner/selectBanner")) { chain.doFilter(request, response); return; } //線下活動簽到 if (requestURL.contains("/wx/offlineActivity/signIn") || requestURL.contains("/wx/salon/signIn")) { chain.doFilter(request, response); return; } //線下、沙龍主題詳情購買 if (requestURL.contains("/wx/offlineActivity/getOfflineActivityThemeList") || requestURL.contains("/wx/salon/getSalonThemeList")) { chain.doFilter(request, response); return; } //線下、沙龍主題詳情購買 if (requestURL.contains("/wx/offlineActivity/getOfflineActivityThemeDetailByThemeId")) { chain.doFilter(request, response); return; } //操作員登陸 if (requestURL.contains("/wx/offlineActivity/offlineLogin") || requestURL.contains("/wx/salon/offlineLogin")) { chain.doFilter(request, response); return; } //線下、沙龍主題詳情介紹頁 if (requestURL.contains("/wx/offlineActivity/getActivityCodeDetail") || requestURL.contains("/wx/salon/getSalonThemeDetailByThemeId")) { chain.doFilter(request, response); return; } //獲取書籍list if (requestURL.contains("/wx/gift/getGiftList")||requestURL.contains("/wx/gift/getGiftDetailById")) { chain.doFilter(request, response); return; } //是否可分享 if (requestURL.contains("/wx/user/isShare")) { chain.doFilter(request, response); return; } // 驗證token // 這裡token如果接收有空格的地方,,那就是+號沒有處理好。。可以考慮變成%2B if (StringUtils.isBlank(token)) { Result result = new Result(); result.setCode("900"); response.setCharacterEncoding("UTF-8"); response.getWriter().println(JSON.toJSONString(result)); return; } token = token.replaceAll(" ", "+"); TokenModel model = manager.getToken(token); if (manager.checkToken(model)) { // 如果token驗證成功,將token對應的使用者id存在request中,便於之後注入 // request.setAttribute(Constants.CURRENT_USER_ID, model.getName()); // app請求就一次,所有session沒有用處 除非pc // session.setAttribute(Constants.CURRENT_USER_ID, // model.getuserId()); chain.doFilter(request, response); } else { // 如果驗證token失敗 Result result = new Result(); result.setCode("900"); response.setCharacterEncoding("UTF-8"); response.getWriter().println(JSON.toJSONString(result)); return; } } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } }