导读热词
Spark是一个微型的Java Web框架,它的灵感来自于Sinatra,它的目的是让你以最小的代价创建出一个Java Web应用。
Implement CORS in Spark -spark中如何处理跨域资源共享问题
代码如下:
// Enables CORS on requests. This method is an initialization method and should be called once.
private static void enableCORS(final String origin,final String methods,final String headers) {
options("/*",(request,response) -> {
String accessControlRequestHeaders = request.headers("Access-Control-Request-Headers");
if (accessControlRequestHeaders != null) {
response.header("Access-Control-Allow-Headers",accessControlRequestHeaders);
}
String accessControlRequestMethod = request.headers("Access-Control-Request-Method");
if (accessControlRequestMethod != null) {
response.header("Access-Control-Allow-Methods",accessControlRequestMethod);
}
return "OK";
});
before((request,response) -> {
response.header("Access-Control-Allow-Origin",origin);
response.header("Access-Control-Request-Method",methods);
response.header("Access-Control-Allow-Headers",headers);
// Note: this may or may not be necessary in your particular application
response.type("application/json");
});
}
说明: before
Before-filters are evaluated before each request,and can read the request and read/modify the response.
除此之外还有一个after
After-filters are evaluated after each request,and can read the request and read/modify the response:
相关文章
总结
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250