跨域配置类解决方法
@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");