java使用filter设置跨域访问_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java使用filter设置跨域访问

java使用filter设置跨域访问

 2017/11/14 19:03:51  柳絮飞祭奠  程序员俱乐部  我要评论(0)
  • 摘要:importjava.io.IOException;importjavax.servlet.Filter;importjavax.servlet.FilterChain;importjavax.servlet.FilterConfig;importjavax.servlet.ServletException;importjavax.servlet.ServletRequest;importjavax.servlet.ServletResponse;importjavax.servlet
  • 标签:使用 Java
class="java" name="code">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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AuthorityFilter implements Filter {

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
		HttpServletResponse nHttpServletResponse = (HttpServletResponse) response;

		HttpServletRequest nHttpServletRequest = (HttpServletRequest) request;

		String nAppKey = nHttpServletRequest.getHeader("Accept-Key");
		if (nAppKey == null || "".equals(nAppKey)) {
			nHttpServletResponse.setStatus(403);
			return;
		} else {
			// 存在则验证准确性
//			if (!nAppKey.toString().equals(XSetting.ZTOKEN_KEY)) {
//				nHttpServletResponse.setStatus(403);
//				return;
//			}
		}

		nHttpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
		nHttpServletResponse.setHeader("Access-Control-Allow-Headers","Accept-Key");
		chain.doFilter(request, nHttpServletResponse);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}
上一篇: iOS-获取通讯录联系人信息 下一篇: 没有下一篇了!
发表评论
用户名: 匿名