[TOC]

server.port=8080
pom

<?xml version=”1.0” encoding=”UTF-8”?>
xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd”>
4.0.0

org.springframework.boot
spring-boot-starter-parent
2.3.1.RELEASE


com.lushunde
cors
0.0.1-SNAPSHOT
cors
Demo project for Spring Boot

1.8



org.springframework.boot
spring-boot-starter-web


org.projectlombok
lombok
true


org.springframework.boot
spring-boot-starter-test
test


org.junit.vintage
junit-vintage-engine







org.springframework.boot
spring-boot-maven-plugin



main

package com.lushunde.cors;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CorsApplication {

public static void main(String[] args) {<br />  SpringApplication.run(CorsApplication.class, args);<br />    }

}

controller

/
Copyright © 2020, lushunde or www.lushunde.com site. All rights reserved.
@package cors com.lushunde.cors
@author bellus
@node
*/
package com.lushunde.cors;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/

@node
@package cors com.lushunde.cors
@version 1.0
@date 2020年7月22日
@author bellus
@since JDK1.8
*/
@RestController
@RequestMapping(“/cors”)
public class Controller {

@RequestMapping(“getuser”)
public @ResponseBody Map getUser( ){

Map user = new HashMap<>();
user.put(“name”, “zhangsan”);
user.put(“rname”, “张三”);
user.put(“age”, 29);
user.put(“data”, new Date());

return user;

}


@RequestMapping(“adduser”)
public @ResponseBody Map addUser(@RequestBody Map user ){

// user.put(“name”, user.get(“name”));
user.put(“rname”, “张三”);
user.put(“age”, 29);
user.put(“data”, new Date());

return user;

}

@RequestMapping(“updateuser”)
public @ResponseBody Map updateUser(@RequestBody Map user ){

// user.put(“name”, user.get(“name”));
user.put(“rname”, “张三”);
user.put(“age”, 29);
user.put(“data”, new Date());

return user;

}

@RequestMapping(“deleteuser”)
public @ResponseBody Map deleteUser(@RequestBody Map user ){

// user.put(“name”, user.get(“name”));
user.put(“rname”, “张三”);
user.put(“age”, 29);
user.put(“data”, new Date());

return user;

}

}

cors

/

Copyright © 2020, lushunde or www.lushunde.com site. All rights reserved.
@package cors com.lushunde.cors
@author bellus
@node
/
package com.lushunde.cors;
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 org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
@node 使用这个过滤器实现跨域调用
@package cors com.lushunde.cors
@version 1.0
@date 2020年7月22日
@author bellus
@since JDK1.8
/
@Component
@Order(0)
public class CorsFilter implements Filter {




@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
// logger.info(“corsfilter———————-“);
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request = (HttpServletRequest) req;
String allowOrigin = getAllowOrigin(request);
response.setHeader(“Access-Control-Allow-Origin”, allowOrigin);
response.setHeader(“Access-Control-Allow-Credentials”, “true”);
response.setHeader(“Access-Control-Allow-Methods”, ““);
response.setHeader(“Access-Control-Max-Age”, “3600”);
response.setHeader(“Access-Control-Allow-Headers”, “
“);
// response.setHeader(“Access-Control-Allow-Headers”, “Authorization,Origin,X-Requested-With,Content-Type,Accept,”
// + “content-Type,origin,x-requested-with,content-type,accept,authorization,token,id,X-Custom-Header,X-Cookie,Connection,User-Agent,Cookie,“);
// response.setHeader(“Access-Control-Request-Headers”, “Authorization,Origin, X-Requested-With,content-Type,Accept”);
response.setHeader(“Access-Control-Expose-Headers”, “
“);

if (“OPTIONS”.equals(request.getMethod().toUpperCase())) {
return;
}
chain.doFilter(req, res);
}
/*
返回为空,则表示不符合要求的远程连接

@param request
@return
@throws OperBusinException
@throws Exception
/
private String getAllowOrigin(HttpServletRequest request) {
try {
String allowIP = “localhost,0:0:0:0:0:0:0:1,39.98.132.29”;
String remoteAddr = request.getRemoteAddr();
String requestOrigin = request.getHeader(“Origin”);
String[] arrWhiteList = allowIP.split(“,”);
for (String ip : arrWhiteList) {
if (ip.equals(remoteAddr))
return requestOrigin;
}
} catch (Exception e) {
System.out.println(“计算可跨域访问的地址时发生错误”);
e.printStackTrace();
}
return “”;
}
@Override
public void destroy() {
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}

}

index

<!DOCTYPE html>







8080-cors | 9090-nocors

8080-cors
<!DOCTYPE html>












内容:



9090-nocors
<!DOCTYPE html>












内容: