浏览器无法加载网页静态资源

浏览器打开网页一直报错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包版本号的问题

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值