• 跨域问题的解决,前端访问不了接口(前端和后台在不同的服务器)


    package com.runmin.bajie.intercepter;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    public class AccessInterceptor implements HandlerInterceptor {
    
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
                throws Exception {
            
        }
    
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
                throws Exception {
            
        }
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
            response.setHeader("Access-Control-Allow-Credential", "true");
            response.setHeader("P3P", "CP=CAO PSA OUR");
            if(request.getHeader("Access-Control-Request-Method")!=null&&"OPTIONS".equals(request.getMethod())) {
                response.addHeader("Access-Control-Allow-Methods", "POST,GET,TRACE,OPTIONS");
                response.addHeader("Access-Control-Allow-Headers", "Content-Type,Origin,Accept");
                response.addHeader("Access-Control-Max-age", "120");
    
            }
            return true;
        }
        
    }
        <!-- 5.跨域拦截器 -->    
        <mvc:interceptors>
            <mvc:interceptor>
                <mvc:mapping path="/*/**"/>
                <bean class="com.runmin.bajie.intercepter.AccessInterceptor"></bean>
            </mvc:interceptor>
        </mvc:interceptors>

    在MVC配置文件中配置如上即可。

  • 相关阅读:
    springboot整合mybatisplus
    在layui中使用treetable.js插件
    shiro系列9:基于前端的权限控制和基于后端的权限控制
    springboot文件上传案例
    springboot配置虚拟路径访问上传到磁盘的文件
    springboot文件下载案例
    springboot跨域的处理方式
    springboot上传文件过大的解决方案
    Django与Ajax
    Django查询数据库性能优化
  • 原文地址:https://www.cnblogs.com/XJJD/p/7844331.html
Copyright © 2020-2023  润新知