解决跨域问题 ex:Access-Control-Allow-Origin,strict-origin-when-cross-origin

 跨域配置类解决方法

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/api/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("POST", "GET", "DELETE", "PUT")
                .maxAge(3600);
    }

}

 注解解决方法

@Controller
@RequestMapping(value = "/Account", produces = { "text/html;charset=UTF-8;", "application/json;charset=UTF-8;" })
@CrossOrigin(origins = "*", maxAge = 3600)
public class AccountController {
}

 响应头设置解决方法

@RequestMapping(value = "/login", method = RequestMethod.POST)
@ResponseBody
public String signIn(@RequestParam String user, @RequestParam String password, HttpServletResponse response) {
	response.setHeader("Access-Control-Allow-Origin", "*");
	return accountService.signIn(user, password);
}

 xml配置解决方法

<mvc:cors>	
	<mvc:mapping path="/**"
        allowed-origins="*"
        allowed-methods="POST, GET, DELETE, PUT"
        allowed-headers="*"
        allow-credentials="true" />
</mvc:cors>

配置类 相关参数

 res.addHeader("Access-Control-Allow-Credentials", "true");
        res.addHeader("Access-Control-Allow-Origin", "*");
        res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
        res.addHeader("Access-Control-Allow-Headers", "Content-Type,X-CAF-Authorization-Token,sessionToken,X-TOKEN");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值