您的位置:首页 > 编程语言 > Java开发

SpringBoot2.x系列:整合SpringMVC之CORS跨域访问处理(下)

2020-04-26 18:35 726 查看

上一章节中,我给大家讲解了同源策略,跨域访问,以及CORS跨域访问的解决方案。接下来我就要讲解一下SpringBoot中如何实现跨域访问。

SpringBoot中跨域访问实现方案

  • 全局配置实现方案
  • 基于过滤器的实现方案
  • @CrossOrigin注解实现方案

一. 全局配置实现方案

这是一种全局配置的实现方式,一般都是这种解决办法。

[code]package com.yyb.boot.config;

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

/**
* @Description Description
* @Author 一一哥Sun
* @Date Created in 2020/3/28
*
* 注意: WebMvcConfigurerAdapter这个类,@deprecated as of 5.0 {@link WebMvcConfigurer} has default methods (made
*  possible by a Java 8 baseline) and can be implemented directly without the
*  need for this adapter.
*  WebMvcConfigurerAdapter这个类从5.0以后就过失了,5.0之后直接实现WebMvcConfigurer接口就行.
*
*  这是一种全局配置的实现方式,一般都是这种解决办法.
*/
@Configuration
public class CorsConfig implements WebMvcConfigurer {

@Override
public void addCorsMappings(CorsRegistry registry) {
//针对的映射
registry.addMapping("/**")
//针对的origin域名
.allowedOrigins("*")
//针对的方法
.allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
//是否允许发送Cookie
.allowCredentials(true)
//从预检请求得到相应的最大时间,默认30分钟
.maxAge(3600)
//针对的请求头
.allowedHeaders("*");
}

}

二. 基于过滤器的实现方案

该方式基于过滤器来实现,简单明了。

[code]package com.yyb.boot.filter;

import org.springframework.context.annotation.Configuration;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @Description Description
* @Author 一一哥Sun
* @Date Created in 2020/3/28
*
* 基于过滤器的实现方式,简单明了.
*/
@Configuration
@WebFilter(filterName = "CorsFilter")
public class CorsFilter implements Filter {

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) servletResponse;
response.setHeader("Access-Control-Allow-Origin","*");
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, PATCH, DELETE, PUT");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
filterChain.doFilter(servletRequest, servletResponse);
}

}

三. @CrossOrigin注解实现方案

@CrossOrigin注解来实现,更为简单。

[code]package com.yyb.boot.web;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @Description Description
* @Author 一一哥Sun
* @Date Created in 2020/3/28
*
* 以@CrossOrigin注解方式实现跨域访问.
*/
@RestController
public class UserController {

@CrossOrigin(origins = "http://localhost:8088")
@GetMapping("/msg")
public String showMsg() throws Exception {

return "success";
}

}

注意:

以上三种实现方法都可以解决跨域问题,最常用的是第一种和第二种两种方式。

如果三种方式都用了的话,则采用就近原则。

  • 点赞
  • 收藏
  • 分享
  • 文章举报
千锋教育官方博客 企业博客 发布了412 篇原创文章 · 获赞 264 · 访问量 93万+ 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: