uniapp打包h5到springboot 3.x无法访问问题

文章讲述了作者在SpringBoot项目中遇到静态资源401访问问题,通过排查发现是Interceptor拦截器导致的。通过调整Interceptor排除静态资源路径,配置CORS解决跨域问题,以及使用WebMvcConfigurer管理静态资源和视图控制器,最终解决了访问问题。
摘要由CSDN通过智能技术生成

我最终的效果是要127.0.0.1:8080/h5/#/就可以访问客户端应用了;

但是放置到resources下后,401无法访问;



不管我在浏览器输入resources什么静态文件都被反应401,我开始思索是否被拦截需要被放行;

就开始从配置文件开始排查,是否疏漏了哪些配置了;

花了点时间baidu,准备将static-locations和static-path-pattern放行项目目录

spring:
  web:
    resources:
      static-locations: classpath:/META-INF/resources/, classpath:/resources/,             
  classpath:/static/, classpath:/public/
    mvc:
      static-path-pattern: /**

经过以上配置修改和增加,回到浏览器访问客户端应用还是401。后面经过翻阅博客等内容后,发现确实spring原先默认配置就是这些的,所以其实这些没有实际用途。

后面经过朋友的指点,我恍然大悟回忆起了我的Interceptor拦截器,貌似对静态资源拦截有所作用

@Override
    public void addInterceptors(InterceptorRegistry registry) {
        //登录接口和注册接口不拦截
//        registry.addInterceptor(loginInterceptor).excludePathPatterns("/user/login","/user/regist");
        registry.addInterceptor(loginInterceptor).excludePathPatterns("/**");
    }

我注释了之前的代码,开始通过/**放行了所有

再次访问客户端地址发生了改变;

也算发现了突破口,原来是我的拦截器产生了作用,从访问401演变成了404。我开始访问其他静态资源,已经是可以访问的了;

经过朋友的点拨后,我开始查看WebMvcConfigurer接口里的内容。原来addInterceptors需要对静态资源访问路径进行放行,还有addViewControllers需要重写进行添加视图控制,这样子就可以通过访问地址+/h5实现默认跳转客户端首页;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;

    //改用过滤器CorsFilter 来配置跨域,由于Filter的位置是在Interceptor之前的,问题得到解决
    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        // 设置允许跨域请求的域名
        config.addAllowedOrigin("*");
        // 是否允许证书 不再默认开启
        // config.setAllowCredentials(true);
        // 设置允许的方法
        config.addAllowedMethod("*");
        // 允许任何头
        config.addAllowedHeader("*");
        config.addExposedHeader("token");
        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration("/**", config);
        return new CorsFilter(configSource);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //登录接口和注册接口不拦截
        registry.addInterceptor(loginInterceptor).excludePathPatterns("/user/login","/user/regist", "/h5/**")
                .excludePathPatterns("/static/**", "/static/h5/**");
//        registry.addInterceptor(loginInterceptor).excludePathPatterns("/**");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/h5/").setViewName("forward:/h5/index.html");
        WebMvcConfigurer.super.addViewControllers(registry);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //1、添加资源处理器路径 即每次访问静态资源都得添加"/static/",例如localhost:8080/static/j1.jpg
        //若registry.addResourceHandler("/s/**") 则必须访问localhost:8080/s/j1.jpg
        registry.addResourceHandler("/static/**", "/static/h5/**")
                //2、添加了资源处理器路径后对应的映射资源路径
                .addResourceLocations("classpath:/static/", "classpath:/static/h5");
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值