SpringBoot 解决 Ajax 跨域之 session 问题

问题描述:

由于使用前后端分离写网站,不可以避免的会使用 ajax 进行跨域请求,但发现两次请求都会创建两个 session ,无法达到将数据存入 session 实现数据共享的效果。

问题解决:

前端:最主要的是下面这行代码?:

xhrFields: {
         withCredentials: true
}
$.ajax({
        type: 'POST',
        url: rootPath + '/manager/login',
        dataType: 'JSON',
        xhrFields: {
               withCredentials: true
        },
        data: {
              username: username,
              password: password,
              verifyCode: verifyCode
        },
        success: function(result) {
                 console.log(result)         
        },
        error: function(error) {
               console.log(error)
        }
});

后端:最主要的是 setAllowCredentials(true);这行代码?:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class CustomCORSConfiguration {
    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.setAllowCredentials(true);
        return corsConfiguration;
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig());
        return new CorsFilter(source);
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
前后端分离开发是现代Web应用开发的一种趋势,多数情况下前端和后端是独立开发的,也是独立部署和运行。由于浏览器的同源策略限制,前端应用不允许跨域访问不同源的资源,也就是说前端应用和后端应用必须部署在同一个域名下。但是,在实际应用开发,例如前端和后端部署在不同的服务器上,我们就需要解决跨域问题SpringBoot是一个快速开发框架,它大大简化了Spring应用的开发过程,作为SpringBoot的一部分,它提供了解决全局跨域问题的方法。SpringBoot 改进了 SpringMVC 的 WebMvcConfigurerAdapter 这个类的功能,提供了 addCorsMappings 方法。使用 CorsFilter 自定义过滤器则是在 Spring Boot 2.x 之前的做法。 我们可以通过在SpringBoot的实例添加如下全局跨域配置方法来解决全局跨域问题: ``` @Configuration public class CorsConfig { @Bean public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurerAdapter() { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") .allowedMethods("GET", "POST","PUT", "DELETE") .allowCredentials(true) .maxAge(3600); } }; } } ``` 以上代码的作用是为SpringBoot实例添加一个名为CorsConfig的类,并在其定义一个名为corsConfigurer的方法,该方法返回一个继承自WebMvcConfigurerAdapter的匿名类实例,并重载 addCorsMappings 方法。在该方法,我们使用 registry.addMapping("/**") 为所有的路径添加跨域配置;allowedOrigins("*") 允许所有的来源;allowedMethods("GET", "POST","PUT", "DELETE") 允许所有的HTTP方法;allowCredentials(true) 允许发送cookie;maxAge(3600) 缓存响应时间为1小时。 在使用SpringBoot解决跨域问题后,前端应用就可以访问不同的后端服务器资源了。这大大提高了应用的灵活性和可维护性,使得我们可以在实际应用更好地实现前后端分离的开发和部署。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值