瀏覽器無法載入網頁靜態資源
阿新 • • 發佈:2018-12-14
瀏覽器開啟網頁一直報錯Resource interpreted as Document but transferred with MIME type application/json,以及瘋狂報錯Uncaught SyntaxError: Unexpected token <
最近做的一個專案springboot的框架加thymeleaf,網頁開啟某些靜態資源總是不能有效載入,實在很是鬱悶,差了好多,有人說是攔截器的設定問題,應該在實現HandleInteceptor這個介面,重寫preHandle方法時應該不攔截該網頁,我也是反覆檢查確實沒有攔截管理登入頁面,但是問題還是沒有解決,附上攔截器的程式碼塊
@Component public class BaseInterceptor implements HandlerInterceptor { private static final Logger LOGGE = LoggerFactory.getLogger(BaseInterceptor.class); private static final String USER_AGENT = "user-agent"; @Resource private UserService userService; @Resource private OptionService optionService; private MapCache cache = MapCache.single(); @Resource private Commons commons;、 @Resource private AdminCommons adminCommons; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception { String contextPath = request.getContextPath(); System.out.println(contextPath); String uri = request.getRequestURI(); LOGGE.info("UserAgent: {}", request.getHeader(USER_AGENT)); LOGGE.info("使用者訪問地址: {}, 來路地址: {}", uri, IPKit.getIpAddrByRequest(request)); //請求攔截處理 User user = TaleUtils.getLoginUser(request); if (null == user) { Integer uid = TaleUtils.getCookieUid(request); if (null != uid) { //這裡還是有安全隱患,cookie是可以偽造的 user = userService.queryUserById(uid); request.getSession().setAttribute(Const.LOGIN_SESSION_KEY, user); } } if (uri.startsWith(contextPath + "/admin") && !uri.startsWith(contextPath + "/admin/login") && null == user) { response.sendRedirect(request.getContextPath() + "/admin/login"); return false; } //設定get請求的token if (request.getMethod().equals("GET")) { String csrf_token = UUID.UU64(); // 預設儲存30分鐘 cache.hset(Types.CSRF_TOKEN.getType(), csrf_token, uri, 30 * 60); request.setAttribute("_csrf_token", csrf_token); } return true; } @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { Options ov = optionService.getOptionByName("site_record"); httpServletRequest.setAttribute("commons", commons);//一些工具類和公共方法 httpServletRequest.setAttribute("option", ov); httpServletRequest.setAttribute("adminCommons", adminCommons); } @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { } }
但是就是怎麼改admin/login.html的靜態資源總是不能順利載入,後來發現也許是Springmvc的配置類有有問題,附上程式碼
@Component public class WebMvcConfig extends WebMvcConfigurerAdapter { @Resource private BaseInterceptor baseInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(baseInterceptor); } /** * 新增靜態資原始檔,外部可以直接訪問地址 * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/upload/**"). addResourceLocations("file:"+ TaleUtils.getUploadFilePath()+"upload/"); super.addResourceHandlers(registry); } }
其實在spring-webmvc 5.0.x以後的版本,對於上面WebMvcConfig的介面設定為已經過期了,而自己當時maven依賴版本就是5.0.6,如圖 其實是WebMvcConfigurerAdapter 在Spring5.0已被廢棄,這真的是尷尬,我找了兩天結果得到這個東西,果然是坑不斷,於是將版本降到5.0.x以下,果然好多了。所以基本出現這種問題就兩種情況,1、攔截器寫的有瑕疵 2、jar包版本號的問題