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: (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|