一、jeeSite 4.0以上版本
1.springMVC使用注解@CrossOrigin(origins = “*”, maxAge = 3600)
1)导入import org.springframework.web.bind.annotation.CrossOrigin;
2)方法必须声明使用什么请求方式
列如@RequestMapping(value={"/sss"},method = RequestMethod.POST)
2.jeesite框架中,需要免登录才能跨域成功
在配置文件----spring-context-shiro.xml中找到
<bean name="shiroFilterChainDefinitions" class="java.lang.String">
在需要跨域的路径中,使得 ${adminPath}/sss/** = anon
二、jeeSite 4.0版本以下
因为不存在正在@CrossOrigin
在后台写一个过滤器过滤器来改写请求头头
CorsFilter.java
public 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 {
HttpServletResponse response = (HttpServletResponse) servletResponse;
HttpServletRequest request = (HttpServletRequest)servletRequest;
String origin = request.getHeader("Origin");
response.setHeader("Access-Control-Allow-Origin", 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,Authorization");
response.setHeader("Access-Control-Allow-Credentials", "true");
String method = request.getMethod();
if(method.equalsIgnoreCase("OPTIONS")){
servletResponse.getOutputStream().write("Success".getBytes("utf-8"));
}else{
filterChain.doFilter(servletRequest, servletResponse);
}
}
@Override
public void destroy() {
}
}
备注:
在web.xml文件中添加代码
corsFilter xxx.xxx.CorsFilter corsFilter /*2.jeesite框架中,需要免登录才能跨域成功
在配置文件----spring-context-shiro.xml中找到
<bean name="shiroFilterChainDefinitions" class="java.lang.String">
在需要跨域的路径中,使得 ${adminPath}/sss/** = anon