如何在Spring Restfull WebService中正确启用CORS?
我想要实现的目标是在另一个域中消耗spring rest的web服务,但是当我执行生成JSON值的json url时,我的JavaScript控制台会生成此错误:
what i trying to achieve is consume my spring restful web service in another domain, but when i excute the json url that generated JSON value, my javascript console generate this error :
跨域请求被阻止:同源策略禁止读取 http://上的远程资源本地主机:8080/SpringServiceJsonSample/service/updatepool/1 .(原因:CORS标头"Access-Control-Allow-Origin"缺失).跨域请求被阻止:同源策略禁止读取 http://localhost:8080上的远程资源/SpringServiceJsonSample/service/updatepool/1 .(原因:CORS请求失败).
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8080/SpringServiceJsonSample/service/updatepool/1. (Reason: CORS header 'Access-Control-Allow-Origin' missing). Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8080/SpringServiceJsonSample/service/updatepool/1. (Reason: CORS request failed).
这是这些链接 http://localhost:8080/SpringServiceJsonSample/service/updatepool/1
{用户名":1,名字":品牌",姓氏":便当",电子邮件":"benjie@gmail.com"}
{"userid":1,"firstName":"brand","lastName":"bennet","email":"benjie@gmail.com"}
这是我的SpringController.java:
Here is my SpringController.java :
@RestController
@RequestMapping("/service/updatepool/")
public class SpringServiceController {
UserService userService = new UserService();
@RequestMapping(value = "/{id}", method = RequestMethod.GET,headers="Accept=application/json")
public User getUser(@PathVariable int id) {
User user=userService.getUserById(id);
return user;
}
}
然后我创建基于spring.io的SimpleCORSFilter教程,所以这是我的SimpleCORSFilter.java类
Then i create SimpleCORSFilter based spring.io tutorial, so here is my SimpleCORSFilter.java class
@Component
public class SimpleCORSFIlter implements Filter{
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
chain.doFilter(req, res);
}
public void init(FilterConfig filterConfig) {}
public void destroy() {}
}
这是我使用JSON值的方式,首先,我创建了index.html文件,该文件看起来像index.html
Here is how i consume the JSON value, first of all i create index.html file which is looked like index.html
<html ng-app>
<head>
<title>Angular Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body>
<div ng-controller="Hello">
<p>User ID : {{greeting.userid}}</p>
<p>firstName : {{greeting.firstName}}</p>
<p>lastName : {{greeting.lastName}}</p>
<p>email : {{greeting.email}}</p>
</div>
</body>
</html>
这是我的app.js
and here is my app.js
function Hello($scope, $http) {
$http.get('http://localhost:8080/SpringServiceJsonSample/service/updatepool/1').
success(function(data) {
$scope.greeting = data;
});
}
我已经创建了CORS过滤器类,为什么它仍然给我CORS阻止的错误?我错过了吗?我认为该课程无法正常工作,我该如何跟踪错误?
i already create the CORS filter class why it still give me CORS blocked error? which i missed? i don't think that class is working properly, how do i have to trace the error?
就像@Bill Bilal所说的那样,我必须将其注册到我的web.xml中,所以这是我在我的web.xml中注册它的方式
just like @Bill Bilal said, i have to register it to my web.xml, so here is how i register it in my web.xml
<filter>
<filter-name>cors</filter-name>
<filter-class>com.myapp.springservice.utility.SimpleCORSFIlter</filter-class>
</filter>
<filter-mapping>
<filter-name>cors</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>