ant-design / ant-design-pro

👨🏻‍💻👩🏻‍💻 Use Ant Design like a Pro!
https://pro.ant.design
MIT License
36.46k stars 8.14k forks source link

springboot 融合 #2781

Closed linyongfu2013 closed 5 years ago

linyongfu2013 commented 5 years ago

build 好之后文件放到 springboot 应用的 static 目录下无法访问,静态资源跨域等都配置了

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        super.addResourceHandlers(registry);
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }

    @Bean
    public InternalResourceViewResolver setupViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        /** 设置视图路径的后缀 */
        resolver.setSuffix(".html");
        return resolver;
    }

    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*"); // 允许任何域名使用
        corsConfiguration.addAllowedHeader("*"); // 允许任何头
        corsConfiguration.addAllowedMethod("*"); // 允许任何方法(post、get等)
        return corsConfiguration;
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig()); // 对接口配置跨域设置
        return new CorsFilter(source);
    }

  mvc:
    view:
      suffix: .html
    static-path-pattern: /**

  resources:
    static-locations: classpath:/static/
MrHeer commented 5 years ago

我把build后的文件直接放在static下就可以了,没有配置任何东西

xiaohuoni commented 5 years ago

https://github.com/ant-design/ant-design-pro/issues/2709

linyongfu2013 commented 5 years ago

@MrHeer 你的 config.js 可以发上来看看吗?你的springboot 需要写一个指定根路径【/】的controller 吗?

MinHongwei commented 5 years ago

是你的跨越文件写错了, 而且你可以发完整。你这是网上的吧。你只是要跨越,其他的不要动springboot的不就可以了嘛???

MinHongwei commented 5 years ago

你用了那些包 继承了那些包什么的,你发出来代码 没发现有价值的错误。

linyongfu2013 commented 5 years ago

@MrHeer @MinHongwei 浏览器地址栏里请求地址是不是应该按前端的路由写?如 http://localhost:8102/user/login

我访问 http://localhost:8102/ 报404

linyongfu2013 commented 5 years ago

@MinHongwei 我把上面那些跨域的配置项全部删除了,还是不行,另外,我的是restful 的接口,没有mvc 的,这个有影响吗?

linyongfu2013 commented 5 years ago

@MinHongwei @MrHeer

@EnableWebMvc
@SpringBootApplication
//@EnableAutoConfiguration
public class InnerAdminApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(InnerAdminApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(this.getClass());
    }
}

@Configuration
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        super.addInterceptors(registry);
        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/api/**")
            .excludePathPatterns("/api/v1/global/**");
    }

}

配置相关的只有这些代码了,还是不行,访问所有路由都报404

MrHeer commented 5 years ago

@linyongfu2013 我的config.js只改了路由方式

export default {
  history: 'hash', // 默认是 browser
}
linyongfu2013 commented 5 years ago

@MrHeer 不用配置后端接口代理地址吗?

  proxy: {
    '/api/': {
      target: 'http://localhost:8102/inner-admin',
      changeOrigin: true,
      // pathRewrite: { '^/server': '' },
    },
  },
MrHeer commented 5 years ago

@linyongfu2013 反正我没有配置,是注解了的,我的全是默认的,我没有更改,只更改过路由方式

xiaohuoni commented 5 years ago

@MrHeer 前端文件放在你的服务器里面,不会存在跨域问题。前端请求路径直接请求服务器路径,不需要配置proxy

MrHeer commented 5 years ago

@MrHeer 嗯,是这样,我就是这样,放在我自己服务器的

xiaohuoni commented 5 years ago

手残老是@错人 @linyongfu2013

linyongfu2013 commented 5 years ago

@MrHeer @xiaohuoni 老哥们,还是不行啊,springboot 需要配置视图解释器和静态资源访问那些吗?

MrHeer commented 5 years ago

@linyongfu2013 我什么都没有配置,直接就可以使用

linyongfu2013 commented 5 years ago

@MrHeer 你的springboot 什么版本?可以分享下你的配置类代码吗?

xiaohuoni commented 5 years ago

@linyongfu2013 你在你的文件夹下,丢一个index.html。能访问就行了。

linyongfu2013 commented 5 years ago

@xiaohuoni 哪个文件夹?

xiaohuoni commented 5 years ago

http://localhost:8102/ 这个地址映射的文件夹

MrHeer commented 5 years ago

@linyongfu2013 static

linyongfu2013 commented 5 years ago

@MrHeer @xiaohuoni 不行,404

linyongfu2013 commented 5 years ago

@MrHeer @xiaohuoni

No mapping found for HTTP request with URI [/index.html] in DispatcherServlet with name 'dispatcherServlet'
linyongfu2013 commented 5 years ago

@MrHeer @xiaohuoni 需要引入模板引擎包吗?

xiaohuoni commented 5 years ago

非pro和umi问题

MrHeer commented 5 years ago

@linyongfu2013 不需要

MrHeer commented 5 years ago

@linyongfu2013 我的pom.xml如下,可以参考一下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <artifactId>myproject</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.0.RELEASE</version>
  </parent>

  <!-- Additional lines to be added here... -->
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

</project>
MrHeer commented 5 years ago

@linyongfu2013 你也可以参考一下官方文档

linyongfu2013 commented 5 years ago

@MrHeer @xiaohuoni 现在可以访问登录页面了,可是暂时还不清楚改了哪里的代码成功的。。谢谢两位

but,登录成功后跳转,出现报错,但我前后端分离调试的时候是没有问题的, 这怎么解决呢?

Cannot read property 'currentUser' of undefined