前端请求跨域问题
来源:互联网 发布:淘宝商品id 编辑:程序博客网 时间:2024/05/18 02:57
使用微信接口时,遇到前端请求跨域问题,不用修改tomcat conf,配置过滤器即可。
过滤器
package me.ele.mercuris.svr.web.framework;import com.google.gson.Gson;//import me.ele.mercuris.coffee.dto.KarmaUser;import me.ele.mercuris.common.exception.SystemException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import javax.servlet.*;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;public class WebAPISecurityFilter implements Filter {final static Logger logger = LoggerFactory.getLogger(WebAPISecurityFilter.class);private WebAPISecurityProtocol securityProtocol = new WebAPISecurityProtocol();@Overridepublic void init(FilterConfig filterConfig) throws ServletException {}@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,ServletException {HttpServletRequest httpRequest = (HttpServletRequest) request;HttpServletResponse httpResponse = (HttpServletResponse) response;corsResponse(httpResponse);chain.doFilter(request, response);}/** * Cross-origin resource sharing (CORS) * * @param response */private void corsResponse(HttpServletResponse response) {response.setHeader("Access-Control-Allow-Origin", "*");response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");response.setHeader("Access-Control-Max-Age", "3600");response.setHeader("Access-Control-Allow-Headers", WebAPISecurityProtocol.CUSTOM_HEADERS);}@Overridepublic void destroy() {}}
context-svr.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 这是一个基础的Filter但依赖于基础认证 --> <bean id="webapi_security" class="me.ele.mercuris.svr.web.framework.WebAPISecurityFilter"> </bean> </beans>
在web.xml中添加
<filter> <filter-name>webapi_security</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <init-param> <param-name>targetFilterLifecycle</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>webapi_security</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
0 0
- 前端请求跨域问题
- 关于前端跨域 一次动作 两次请求的问题
- 前端开发跨域请求
- 前端跨域请求解决方案
- 前端JS跨域请求
- 前端请求跨域理解
- PHP解决mui前端框架中的ajax请求跨域问题
- 【前端】jquery跨域请求javaServlet
- 前端跨域请求原理及实践
- 前端跨域请求原理及实践
- 前端跨域请求原理及实践
- 前端跨域请求原理及实践
- 前端跨域请求解决方案汇总
- 前端跨域请求解决方案汇总
- 前端跨域请求原理及实践
- 前端跨域请求原理及实践
- 前端跨域请求原理及实践
- 前端面试之跨域请求
- PlayerPrefs学习
- 自己写的串口缓冲区
- Axure入门设计——模糊设计(步骤)
- 2015-07-23
- !codeforces 399C Cards-数论&贪心-(暴力枚举)
- 前端请求跨域问题
- mac ocx,android,android studio相关
- 一个关于c++数值转换的小程序的思考
- CentOS-7磁盘扩容之殇
- Python中异常处理机制
- BSON结构
- Search a 2D Matrix II
- IOS研究成果——在autolayout 如何实现 Cell 的高度自适应
- UVa 1572 拓扑排序 判断有向图是否有环