SpringBoot 2.0 @CrossOrigin 无法跨域问题

在spring boot 1.5中,配置跨域一般是直接在controller或是在某一个方法上添加 @CrossOrigin 注解即可,如下代码

 

/**
 * @author chenws
 * @decription
 * @date 2018/10/18
 */
@RestController
@RequestMapping(value = "xxx")
@CrossOrigin(maxAge = 3600)
public class TestController {

	@ApiOperation("xxx")
	@RequestMapping(value = "/xxx",method = RequestMethod.POST)
	public ResponseVO<List<xxx>> test(@RequestBody xxx xxx){}
}

但是在spring boot 2.0中(springframework5.0.2后),以上方法行不通,后来查看@CrossOrigin源码

springframework4.3.12:

/**
	 * Whether the browser should include any cookies associated with the
	 * domain of the request being annotated.
	 * <p>Set to {@code "false"} if such cookies should not included.
	 * An empty string ({@code ""}) means <em>undefined</em>.
	 * {@code "true"} means that the pre-flight response will include the header
	 * {@code Access-Control-Allow-Credentials=true}.
	 * <p>If undefined, credentials are allowed.
	 */
	String allowCredentials() default "";

springframework5.0.2

/**
	 * Whether the browser should send credentials, such as cookies along with
	 * cross domain requests, to the annotated endpoint. The configured value is
	 * set on the {@code Access-Control-Allow-Credentials} response header of
	 * preflight requests.
	 * <p><strong>NOTE:</strong> Be aware that this option establishes a high
	 * level of trust with the configured domains and also increases the surface
	 * attack of the web application by exposing sensitive user-specific
	 * information such as cookies and CSRF tokens.
	 * <p>By default this is not set in which case the
	 * {@code Access-Control-Allow-Credentials} header is also not set and
	 * credentials are therefore not allowed.
	 */
	String allowCredentials() default "";

By default this is not set in which case the {@code Access-Control-Allow-Credentials} header is also not set and credentials are therefore not allowed.

5.0.2后,allowCredentials默认为false了,再看 DefaultCorsProcessor

if (Boolean.TRUE.equals(config.getAllowCredentials())) {

	responseHeaders.setAccessControlAllowCredentials(true);

}

allowCredentials为true时,返回的响应头AccessControlAllowCredentials属性才设置为true,允许客户端携带验证消息。

解决办法:

在注解中设置allowCredentials为true即可。

@CrossOrigin(allowCredentials="true",maxAge = 3600)

 

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
在Spring Boot 1.5.9中,可以使用@CrossOrigin注解来解决跨域问题。引用中的代码片段展示了如何在Spring Boot中使用@CrossOrigin注解。在使用@RestController注解的控制器类中,可以在@RequestMapping注解之前使用@CrossOrigin注解来允许跨域请求。在@CrossOrigin注解中,可以使用origins属性指定允许的请求来源,使用maxAge属性来指定缓存持续的最大时间(以秒为单位)。例如,@CrossOrigin(origins = "http://localhost:8088", maxAge = 3600)表示允许来自"http://localhost:8088"的请求,并且该请求的响应将在缓存中保持最多3600秒。中的文章也提到了使用@CrossOrigin注解来解决跨域问题。在@RequestMapping注解的方法上使用@CrossOrigin注解,可以在特定的方法上启用跨域请求。例如,@CrossOrigin注解可以添加到@RequestMapping注解的方法上,如@RequestMapping(value = "/insertUser2", method = RequestMethod.POST)所示,以允许来自任何来源的POST请求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [springboot下跨域解决的四种方法](https://blog.csdn.net/qq_15351167/article/details/89284397)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [SpringBoot教程(七) | SpringBoot解决跨域问题](https://blog.csdn.net/lsqingfeng/article/details/122614630)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

碩果

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值