zoukankan      html  css  js  c++  java
  • springboot2.x设置跨域的方式

    SpringBoot2以后设置跨域

    实现方式:实现WebMvcConfigurer接口,这个接口里面有很多方法,不用实现所有方法(每个方法都默认实现了,所以implements之后只需要实现自己需要的方法)
    这里我们实现addCorsMappings方法

    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity;
    import org.springframework.validation.BindException;
    import org.springframework.validation.ObjectError;
    import org.springframework.web.method.HandlerMethod;
    import org.springframework.web.servlet.HandlerExceptionResolver;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.NoHandlerFoundException;
    import org.springframework.web.servlet.config.annotation.*;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.util.List;
    
    /**
     * @author xuxs
     * @create 2020-09-07
     */
    @Configuration
    public class MyWebMvcConfigurer implements  WebMvcConfigurer {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
                    .allowedOrigins("*")
                    .allowCredentials(true)
                    .allowedMethods("GET", "POST", "DELETE", "PUT","PATCH")
                    .maxAge(3600);
        }
    }
    
  • 相关阅读:
    springboot---Shiro
    spring MVC 使用 modelAndView.setViewName("forward:*.action") 发送重定向
    数据库负载均衡、读写分离技术
    redis详解
    分布式--锁
    springboot---数据整合篇
    hadoop安装单机
    NIO系列之MINA
    JAVA中IO流模型BIO,NIO,AIO
    CPU-bound(计算密集型) 和I/O bound(I/O密集型)
  • 原文地址:https://www.cnblogs.com/bcde/p/13632495.html
Copyright © 2011-2022 走看看