ajax项目配置,Spring mvc环境下web项目配置ajax跨域访问

必须配置个filter,在filter设置

httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");

搞定了。

之后还报了个小错误,认证

httpServletResponse.setHeader("Access-Control-Allow-Headers", "Authentication");

搞定了。

重要的事情说三次:

必须在filter配置!!

必须在filter配置!!

必须在filter配置!!

直接在controller写和inteceptor写都是没鸟用的!!!!!!!!!!!!!!!!!

具体:

1.写个filter

1105e88f20f52ff07bbac4251a8690b8.png

2.配置在web.xml

5e4e5f5a07cca4fb03fa79ee1fe1f05f.png

问题解决。

另外Spring4.2也支持了跨域请求,直接使用@CrossOrigin注解即可。

Spring源码:

@Target({ ElementType.METHOD, ElementType.TYPE })

@Retention(RetentionPolicy.RUNTIME)

@Documented

public @interface CrossOrigin {

String[] DEFAULT_ORIGINS = { "*" };

String[] DEFAULT_ALLOWED_HEADERS = { "*" };

boolean DEFAULT_ALLOW_CREDENTIALS = true;

long DEFAULT_MAX_AGE = 1800;

/**

* 同origins属性一样

*/

@AliasFor("origins")

String[] value() default {};

/**

* 所有支持域的集合,例如"http://domain1.com"。

*

这些值都显示在请求头中的Access-Control-Allow-Origin

* "*"代表所有域的请求都支持

*

如果没有定义,所有请求的域都支持

* @see #value

*/

@AliasFor("value")

String[] origins() default {};

/**

* 允许请求头重的header,默认都支持

*/

String[] allowedHeaders() default {};

/**

* 响应头中允许访问的header,默认为空

*/

String[] exposedHeaders() default {};

/**

* 请求支持的方法,例如"{RequestMethod.GET, RequestMethod.POST}"}。

* 默认支持RequestMapping中设置的方法

*/

RequestMethod[] methods() default {};

/**

* 是否允许cookie随请求发送,使用时必须指定具体的域

*/

String allowCredentials() default "";

/**

* 预请求的结果的有效期,默认30分钟

*/

long maxAge() default -1;

}

在Controller上使用@CrossOrigin注解

@CrossOrigin(origins = "http://domain2.com", maxAge = 3600)

@RestController

@RequestMapping("/account")

public class AccountController {

@RequestMapping("/{id}")

public Account retrieve(@PathVariable Long id) {

// ...

}

@RequestMapping(method = RequestMethod.DELETE, path = "/{id}")

public void remove(@PathVariable Long id) {

// ...

}

}

这里指定当前的AccountController中所有的方法可以处理http://domain2.com域上的请求,

在方法上使用@CrossOrigin注解

@CrossOrigin(maxAge = 3600)

@RestController

@RequestMapping("/account")

public class AccountController {

@CrossOrigin("http://domain2.com")

@RequestMapping("/{id}")

public Account retrieve(@PathVariable Long id) {

// ...

}

@RequestMapping(method = RequestMethod.DELETE, path = "/{id}")

public void remove(@PathVariable Long id) {

// ...

}

}

在这个例子中,AccountController类上也有@CrossOrigin注解,retrieve方法上也有注解,Spring会合并两个注解的属性一起使用。

CORS全局配置

除了细粒度基于注解的配置,你可能会想定义一些全局CORS的配置。这类似于使用过滤器,但可以在Spring MVC中声明,并结合细粒度@CrossOrigin配置。默认情况下所有的域名和GET、HEAD和POST方法都是允许的。

基于JAVA的配置

看下面例子:

@Configuration

@EnableWebMvc

public class WebConfig extends WebMvcConfigurerAdapter {

@Override

public void addCorsMappings(CorsRegistry registry) {

registry.addMapping("/**");

}

}

@Configuration

@EnableWebMvc

public class WebConfig extends WebMvcConfigurerAdapter {

@Override

public void addCorsMappings(CorsRegistry registry) {

registry.addMapping("/api/**")

.allowedOrigins("http://domain2.com")

.allowedMethods("PUT", "DELETE")

.allowedHeaders("header1", "header2", "header3")

.exposedHeaders("header1", "header2")

.allowCredentials(false).maxAge(3600);

}

}

如果你使用Spring Boot,你可以通过这种方式方便的进行配置。

基于XML的配置

这个配置和上面Java方式的第一种作用一样。

同样,你可以做更复杂的配置:

allowed-origins="http://domain1.com, http://domain2.com"

allowed-methods="GET, PUT"

allowed-headers="header1, header2, header3"

exposed-headers="header1, header2" allow-credentials="false"

max-age="123" />

allowed-origins="http://domain1.com" />

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值