跨域是比较常见问题,比较简单的方式就是直接定义一个过滤器filter,然后在请求头里面加上一些参数。下面来看看具体的写法吧。
一、java代码
package com.hj.userapi.core.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
/**
* 功能描述:解决跨域过滤器
* 2017年9月4日
*/
public class ContextFilter implements Filter{
private final int time = 20*24*60*60;
/**
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
}
/**
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletResponse resp = (HttpServletResponse) response;
// 添加参数,允许任意domain访问
resp.setHeader("Access-Control-Allow-Origin", "*");
// 这个allow-headers要配为*,这样才能允许所有的请求头 --- update by zxy in 2018-10-19
resp.setHeader("Access-Control-Allow-Headers", "*");
resp.setHeader("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
resp.setHeader("Access-Control-Max-Age", time+"");
chain.doFilter(request, resp);
}
/**
* @see javax.servlet.Filter#destroy()
*/
@Override
public void destroy() {
// TODO Auto-generated method stub
}
}
x
1
package com.hj.userapi.core.filter;
2
3
import java.io.IOException;
4
import javax.servlet.Filter;
5
import javax.servlet.FilterChain;
6
import javax.servlet.FilterConfig;
7
import javax.servlet.ServletException;
8
import javax.servlet.ServletRequest;
9
import javax.servlet.ServletResponse;
10
import javax.servlet.http.HttpServletResponse;
11
12
/**
13
* 功能描述:解决跨域过滤器
14
* 2017年9月4日
15
*/
16
public class ContextFilter implements Filter{
17
private final int time = 20*24*60*60;
18
/**
19
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
20
*/
21
22
public void init(FilterConfig filterConfig) throws ServletException {
23
// TODO Auto-generated method stub
24
25
}
26
27
/**
28
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
29
*/
30
31
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
32
HttpServletResponse resp = (HttpServletResponse) response;
33
// 添加参数,允许任意domain访问
34
resp.setHeader("Access-Control-Allow-Origin", "*");
35
// 这个allow-headers要配为*,这样才能允许所有的请求头 --- update by zxy in 2018-10-19
36
resp.setHeader("Access-Control-Allow-Headers", "*");
37
resp.setHeader("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
38
resp.setHeader("Access-Control-Max-Age", time+"");
39
chain.doFilter(request, resp);
40
}
41
42
/**
43
* @see javax.servlet.Filter#destroy()
44
*/
45
46
public void destroy() {
47
// TODO Auto-generated method stub
48
49
}
50
51
}
二、web.xml中的配置
<filter>
<filter-name>contextFilter</filter-name>
<filter-class>com.hj.userapi.core.filter.ContextFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>contextFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
1
<filter>
2
<filter-name>contextFilter</filter-name>
3
<filter-class>com.hj.userapi.core.filter.ContextFilter</filter-class>
4
</filter>
5
<filter-mapping>
6
<filter-name>contextFilter</filter-name>
7
<url-pattern>/*</url-pattern>
8
</filter-mapping>
三、小结
通过自定义过滤器,在请求头上添加参数实现允许容易域名访问来解决跨域问题,实现起来比较简单。
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/15530.html