CORS 注解 CrossOrigin 跨域资源共享

说明

名称 文档 说明
网关 CORS 跨域资源共享 Spring Cloud Gateway 网关 CORS 跨域资源共享
Security CORS 跨域资源共享 Spring Security CORS 跨域资源共享
MVC CORS 跨域资源共享 Spring MVC CORS 跨域资源共享
基于注解配置 本文

配置

package cn.com.xuxiaowei.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author xuxiaowei
 */
@Slf4j
@RestController
@RequestMapping("/path")
public class PathRestController {

    @CrossOrigin(origins = {"https://xuxiaowei.com.cn", "https://www.xuxiaowei.com.cn"}, allowCredentials = "true", maxAge = 1800)
    @GetMapping("/test")
    public Map<String, Object> shorten() {
        Map<String, Object> map = new HashMap<>();

        return map;
    }

}