SpringBoot2.x系列:整合SpringMVC之CORS跨域訪問處理(下)

上一章節中,我給大家講解了同源策略,跨域訪問,以及CORS跨域訪問的解決方案。接下來我就要講解一下SpringBoot中如何實現跨域訪問。

SpringBoot中跨域訪問實現方案

  • 全局配置實現方案
  • 基於過濾器的實現方案
  • @CrossOrigin註解實現方案

一. 全局配置實現方案

這是一種全局配置的實現方式,一般都是這種解決辦法。

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("*");
    }

}

二. 基於過濾器的實現方案

該方式基於過濾器來實現,簡單明瞭。

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註解來實現,更爲簡單。

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";
    }

}

注意:

以上三種實現方法都可以解決跨域問題,最常用的是第一種和第二種兩種方式。

如果三種方式都用了的話,則採用就近原則。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章