当前使用spring版本是4.3.9import org.springframework.stereotype.Component;import javax.servlet.*;import javax.servlet.Http.https
当前使用spring版本是4.3.9
import org.springframework.stereotype.Component;import javax.servlet.*;import javax.servlet.Http.httpservletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;@Componentpublic class CorsFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; response.setHeader("Access-Control-Allow-Origin", request.getHeader("origin"));// 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,Cache-Control,Pragma,Content-Type,Token"); response.setHeader("Access-Control-Allow-Credentials","true");//是否允许请求带有验证信息 filterChain.doFilter(servletRequest, servletResponse); } @Override public void destroy() { }}
--结束END--
本文标题: Spring Boot实现跨域访问实现代码
本文链接: https://lsjlt.com/news/224705.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0