|
沙发
楼主 |
发表于 2018-11-1 17:12:52
|
只看该作者
Tomcat 跨域处理
创建全局过滤器:
CorssFilter.java
- package com.airmyth.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;
- public class CorssFilter implements Filter {
- @Override
- public void destroy() {
- // TODO Auto-generated method stub
-
- }
- @Override
- public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
- HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
- httpResponse.addHeader("Access-Control-Allow-Origin", "*");
- httpResponse.setHeader("Access-Control-Allow-Headers", "*");
- httpResponse.setHeader("Access-Control-Allow-Methods", "GET, PUT, DELETE, POST");
- filterChain.doFilter(servletRequest, servletResponse);
- }
- @Override
- public void init(FilterConfig arg0) throws ServletException {
- // TODO Auto-generated method stub
-
- }
- }
复制代码
把这个打成jar包 放到 Tomcat 的 lib 文件夹下,然后修改 conf/web.xml
加入过滤器配置:
- <filter>
- <filter-name>corssFilter</filter-name>
- <filter-class>com.airmyth.filter.CorssFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>corssFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
复制代码
然后重启 Tomcat
jar包见附件:
lib.rar
(1.21 KB, 下载次数: 1)
|
|