• webmvc 拦截器 允许跨域 跨域问题 sessionid不一样


    package cn.com.yitong.ares.filter;

    import java.io.IOException;

    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.ws.rs.HttpMethod;

    import org.apache.http.HttpStatus;

    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 httpResponse = (HttpServletResponse) servletResponse;
    HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
    httpResponse.addHeader("Access-Control-Allow-Origin", httpRequest.getHeader("Origin"));
    httpResponse.setHeader("Access-Control-Max-Age", "3600");
    httpResponse.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
    httpResponse.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    httpResponse.setHeader("Access-Control-Allow-Credentials", "true");
    if (HttpMethod.OPTIONS.equals(httpRequest.getMethod())) {
    httpResponse.setStatus(HttpStatus.SC_NO_CONTENT);
    return;
    }
    filterChain.doFilter(servletRequest, httpResponse);
    }

    @Override
    public void destroy() {

    }

    }

    package cn.com.yitong.ares;

    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.CorsRegistry;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

    /**
    * 跨域配置
    *
    *
    * @date 2020-04-20 16:46
    * @since 1.0.0
    */
    @Configuration
    @EnableWebMvc
    public class CorsConfiguration implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
    // 设置允许跨域的路径
    registry.addMapping("/**")
    // 设置允许跨域请求的域名
    .allowedOrigins("*")
    // 是否允许证书 不再默认开启
    .allowCredentials(true)
    // 设置允许的方法
    .allowedMethods("*")
    // 跨域允许时间
    .maxAge(3600);
    }
    }

  • 相关阅读:
    ubuntu16.04截图工具Shutter安装,设置快捷键
    cd命令和roscd命令的区别,并解决环境变量问题
    ubuntu16.04+kinetic 环境下进行turtlebot2安装+gazebo7.16
    【转载】windows 下TexLive的安装和配置
    Linux 环境变量 /etc/profile 和 ~/.bashrc的区别和使用
    rotors无人机仿真(古月居)——问题记录
    操作select
    #if DEBUG
    (网上看的)asp.net使用uploadify上传出现的IO Error问题
    sqlserver2008附加sqlserver2005数据库目录出错,需要设置mdf后缀文件夹“管理员取得所有权”,并用windows管理权限登录数据库不要用sa
  • 原文地址:https://www.cnblogs.com/jishumonkey/p/12738736.html
Copyright © 2020-2023  润新知