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

Posted strvie

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA后台配置,APP请求跨域问提相关的知识,希望对你有一定的参考价值。

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请求跨域问提的主要内容,如果未能解决你的问题,请参考以下文章

Ajax 跨域问题及其解决方案

java解决前后台跨域问题

java解决前后台跨域问题

uni-app跨域请求接口解决办法

React中跨域问题的完美解决方案

java后端解决跨域问题