CorsFilter 过滤器解决跨域的处理

网友投稿 720 2023-01-13

CorsFilter 过滤器解决跨域的处理

CorsFilter 过滤器解决跨域的处理

CorsFilter 过滤器如何解决跨域

不多逼逼,直接贴代码,前端所有请求头都放在headers里面就行

@Component

public class CorsFilter {

//这里是前端请求头,那个跨域就添加到这里就行了

private static final String headers = "User-Agent,Cache-Control,Content-type,Date,Server,withCredentials," +

"appversion";

@Override

public void init(FilterConfig filterConfig)

throws ServletException {

}

@Override

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)

throws IOException, ServletException {

HttpServletResponse httpServletResponse = (HttpServletResponse) response;

httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");

httpServletResponse.setHeader("Access-Control-Allow-Headers", headers);

httpServletResponse.setHeader("Access-Control-Allow-Credentials", "true");

httpServletResponse.setHeader("Access-Control-Allow-Methods",

"GET, POST, PUT, DELETE, OPTIONS, HEAD");

httpServletResponse.setHeader("Access-Control-Max-Age", "3600");

httpServletResponse.setHeader("Access-Control-Expose-Headers", "usertoken");

httpServletResponse.setHeader("Access-Control-Request-Headers", "usertoken");

httpServletResponse.setHeader("Expires", "-1");

httpServletResponse.setHeader("Cache-Control", "no-cache");

httpServletResponse.setHeader("pragma", "no-cache");

ServletRequest requestWrapper = null;

if (request instanceof HttpServletRequest) {

requestWrapper = new RequestWrapper((HttpServletRequest) request);

}

if (null == requestWrapper) {

chain.doFilter(request, response);

} else {

chain.doFilter(requestWrapper, response);

}

}

@Override

public void destroy() {

}

}

一个CORS实现跨域的过滤器

package com.grain.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.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class CORSFilter implements Filter {

@Override

public void init(FilterConfig arg0) throws ServletException {

// TODO Auto-generated method stub

}

@Override

public void destroy() {

// TODO Auto-generated method stub

}

@Override

public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain)

throws IOException, ServletException {

HttpServletRequest request = (HttpServletRequest) req;

HttpServletResponse response = (HttpServletResponse) res;

response.addHeader("Access-Control-Allow-Origin", "*");

response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");

response.addHeader("Access-Control-Allow-Headers", "Content-Type");

response.addHeader("Access-Control-Max-Age", "1800");//30 min

filterChain.doFilter(request, response);

}

}

web.xml配置

CORSFilter

com.grain.filter.CORSFilter

CORSFilter

/*

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我EZplGnKzBP们。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:企业app开发哪家靠谱(有没有做app平台的公司)
下一篇:校园跑腿app运营模式(校园跑腿app运营模式)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~