码迷,mamicode.com
首页 > 移动开发 > 详细

JAVA后台配置,APP请求跨域问提

时间:2019-05-10 12:47:22      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:log   after   stat   ted   ati   inter   div   访问   max   

package com.szx.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.szx.service.SzxService;

/**
 * 拦截器(注:)解决跨域问题 还需要在配置文件中配置
 * 
 * @author Strvie
 *
 */
@Component
public class CORSInterceptor implements HandlerInterceptor {
    private static final Logger log = LogManager.getLogger(CORSInterceptor.class.getName());

    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {

    }

    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
            throws Exception {
        

    }

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with");

        return true;
    }

}

上边代码是 拦截器类 

 

2 在配置文件中  添加配置

技术图片

  

<!-- 跨域访问 -->
<mvc:interceptors>
<bean class="com.szx.util.CORSInterceptor" />
</mvc:interceptors>

 

这样H5 写app中请求ajax访问就解决了跨域问题

JAVA后台配置,APP请求跨域问提

标签:log   after   stat   ted   ati   inter   div   访问   max   

原文地址:https://www.cnblogs.com/strvie/p/10843713.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!