相关文章:
Windows 中 安装 Redis 桌面连接工具(RedisDesktopManager)
在 IntelliJ IDEA 2018.2.5 创建 Maven 项目
Spring Boot 使用 Redis 进行 Session 共享
Spring Boot 使用 Redis 进行 Session 共享(子域使用 Cookie 共享 Session)
前言:上一篇文章介绍了 Spring Boot 使用 Redis 进行 Session 共享,但缺少 Ajax 跨域设置,本文介绍将如何设置 Ajax 跨域。
- 未设置 Ajax 跨域时,显示错误如下:
- 在 Ajax 目标项目(项目二)的 cn.com.xuxiaowei.demo2.config 包下新建类 WebMvcConfigurerConfig,内容如下:
package cn.com.xuxiaowei.demo2.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * Ajax 跨域 * * @author xuxiaowei */ @Configuration public class WebMvcConfigurerConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { // Ajax 请求指定域,需要提供协议、端口 String[] origins = {"http://127.0.0.1:10001"}; registry.addMapping("/**") // 域 .allowedOrigins(origins) // 默认为 GET, HEAD, POST // 可以为 GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE; .allowedMethods("GET", "HEAD", "POST") // 默认为 1800秒(30分钟) .maxAge(1800) // 浏览器是否应将凭据(如Cookie和跨域请求)发送到带注释的端点。 .allowCredentials(true); } }
- 在 Ajax 请求项目(项目一)页面中修改为:
<!DOCTYPE html> <html lang="zh" xmlns:th="https://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <!--<title>Domo1-10001</title>--> <title>Domo2-10002</title> </head> <body> <!--<h2>项目名:Domo1</h2>--> <!--<h2>端口号:10001</h2>--> <h2>项目名:Domo2</h2> <h2>端口号:10002</h2> 域:<input name="domain" placeholder="域名需要有协议与端口"> <button id="ajax">ajax</button> <br><br> <!--<h2>项目名:Domo2</h2>--> <!--<h2>端口号:10002</h2>--> [[${session.userInfo}]] </body> <script type="text/javascript" th:src="@{/jquery-3.4.0.min.js}"></script> <script th:inline="javascript" type="text/javascript"> var $body = $("body"); $body.on("click", "#ajax", function () { var domain = $('input[name="domain"]').val(); $.ajax({ url: domain + "/index.do", type: "GET", data: {}, dataType: "json", success: function (resp) { console.log("success:", resp); }, fail: function (resp) { console.log("fail:", resp); }, complete: function (xhr, textStatus) { console.log(textStatus); } }); }); </script> </html>
- 在 Ajax 目标项目(项目二)的 cn.com.xuxiaowei.demo2.controller 下新建类 IndexRestController,用于响应 Ajax 请求,内容如下:
package cn.com.xuxiaowei.demo2.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.HashMap; import java.util.Map; /** * @author xuxiaowei */ @RestController public class IndexRestController { @RequestMapping("/index.do") public Map<String,Object> indexDo(HttpServletRequest request, HttpServletResponse response) { Map<String,Object> map = new HashMap<>(4); StringBuffer requestURL = request.getRequestURL(); map.put("data",requestURL); return map; } }
- 重新运行项目,在 项目一 中使用 Ajax 访问 项目二,测试结果为:
- 注意:使用项目二中的Ajax访问项目一时,同上。
- 项目源码下载地址:Spring Boot Redis Session 共享(Ajax 跨域)