1.网址

2.部署前端项目

image.png
image.png

3. 启动tomcat并测试

image.png
目录名就是路径
image.png

4.设置跨域配置实现前后端联调

image.png
image.png

  1. package com.lv.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.web.cors.CorsConfiguration;
  5. import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
  6. import org.springframework.web.filter.CorsFilter;
  7. @Configuration
  8. public class CorsConfig {
  9. public CorsConfig(){
  10. }
  11. @Bean
  12. public CorsFilter corsFilter(){
  13. //1.添加cors配置信息
  14. CorsConfiguration corsConfiguration =new CorsConfiguration();
  15. corsConfiguration.addAllowedOrigin("http://localhost:8080");
  16. //设置是否发送cookie信息
  17. corsConfiguration.setAllowCredentials(true);
  18. //设置允许请求的方式
  19. corsConfiguration.addAllowedMethod("*");
  20. //设置允许的header
  21. corsConfiguration.addAllowedHeader("*");
  22. //2.为url添加映射信息,就是请求的url
  23. UrlBasedCorsConfigurationSource configurationSource=new UrlBasedCorsConfigurationSource();
  24. configurationSource.registerCorsConfiguration("/**",corsConfiguration);
  25. //3.返回重新定义好的UrlBasedCorsConfigurationSource
  26. return new CorsFilter(configurationSource);
  27. }
  28. }